Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-go): report every timeout as a TimeoutError with its real budget
Yudhi Armyndharis05029efd7606
diff --git a/sdk/go/client.go b/sdk/go/client.goindex 131d25bbb..baaafe26d 100644--- a/sdk/go/client.go+++ b/sdk/go/client.go@@ -277,7 +277,7 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.Valueresp, err := c.httpClient.Do(req)if err != nil {if isTimeout(err) {- return nil, "", &TimeoutError{Timeout: c.timeout, Err: err}+ return nil, "", c.timeoutErr(ctx, err)}return nil, "", fmt.Errorf("openwa: %s %s: %w", method, path, err)}@@ -285,6 +285,9 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.Valuedata, err := io.ReadAll(resp.Body)if err != nil {+ if isTimeout(err) {+ return nil, "", c.timeoutErr(ctx, err)+ }return nil, "", fmt.Errorf("openwa: reading response body: %w", err)}@@ -295,6 +298,16 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.Valuereturn data, resp.Header.Get("Content-Type"), nil}+// timeoutErr names the client timeout only when it is the budget that ran out.+// The client timeout never cancels ctx, so an expired ctx means the caller's own+// deadline fired first and the error carries no duration.+func (c *Client) timeoutErr(ctx context.Context, err error) *TimeoutError {+ if ctx.Err() != nil {+ return &TimeoutError{Err: err}+ }+ return &TimeoutError{Timeout: c.timeout, Err: err}+}+func isTimeout(err error) bool {if errors.Is(err, context.DeadlineExceeded) {return truediff --git a/sdk/go/client_test.go b/sdk/go/client_test.goindex 510069f64..0e3f4d630 100644--- a/sdk/go/client_test.go+++ b/sdk/go/client_test.go@@ -7,6 +7,7 @@ import ("errors""io""net/http"+ "net/http/httptest""strings""sync/atomic""testing"@@ -1640,3 +1641,57 @@ func TestDoPathWithoutLeadingSlashRefused(t *testing.T) {t.Fatalf("request sent to %s, want none", rt.lastReq.URL.Host)}}++// stallServer stalls every request before answering; with flush set it sends+// the headers first and stalls the body instead. It blocks until the request is+// cancelled or the test ends.+func stallServer(t *testing.T, flush bool) *httptest.Server {+ t.Helper()+ release := make(chan struct{})+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if flush {+ w.Header().Set("Content-Type", "application/octet-stream")+ w.WriteHeader(http.StatusOK)+ _, _ = w.Write([]byte("partial"))+ w.(http.Flusher).Flush()+ }+ select {+ case <-r.Context().Done():+ case <-release:+ }+ }))+ t.Cleanup(srv.Close)+ t.Cleanup(func() { close(release) })+ return srv+}++func TestTimeoutReportsTheBudgetThatExpired(t *testing.T) {+ for _, flush := range []bool{false, true} {+ srv := stallServer(t, flush)++ // The client timeout fired: the error names it.+ c, err := New(srv.URL, "k", WithTimeout(100*time.Millisecond))+ if err != nil {+ t.Fatalf("New: %v", err)+ }+ _, err = c.Messages.Media(context.Background(), "s1", "c1", "m1")+ var te *TimeoutError+ if !errors.As(err, &te) || te.Timeout != 100*time.Millisecond {+ t.Errorf("flush=%v client timeout: err = %v, want a *TimeoutError after 100ms", flush, err)+ }++ // The caller's shorter deadline fired: the 30s client timeout is not the+ // budget that ran out, so the error does not name it.+ c, err = New(srv.URL, "k")+ if err != nil {+ t.Fatalf("New: %v", err)+ }+ ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)+ _, err = c.Messages.Media(ctx, "s1", "c1", "m1")+ cancel()+ te = nil+ if !errors.As(err, &te) || te.Timeout != 0 || err.Error() != "openwa: request timed out" {+ t.Errorf("flush=%v caller deadline: err = %v, want a *TimeoutError without a duration", flush, err)+ }+ }+}diff --git a/sdk/go/errors.go b/sdk/go/errors.goindex 89c96eeb8..7a5aac40c 100644--- a/sdk/go/errors.go+++ b/sdk/go/errors.go@@ -110,8 +110,10 @@ func (e *APIError) Is(target error) bool {}// TimeoutError is returned when a request exceeds the configured timeout (or the-// caller's context deadline).+// caller's context deadline), whether waiting for the response or reading its body.type TimeoutError struct {+ // Timeout is the client timeout that ran out, or zero when the caller's+ // context deadline fired first.Timeout time.Duration// Err is the underlying cause (context.DeadlineExceeded or a net timeout).Err error