Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-go): treat a final 1xx response as an error

Yudhi Armyndharis0419b135fadf
diff --git a/sdk/go/client.go b/sdk/go/client.go
index 2edbbcb64..8673777b6 100644
--- a/sdk/go/client.go
+++ b/sdk/go/client.go
@@ -275,7 +275,7 @@ func isIdempotentSendPath(path string) bool {
}
// doRaw performs the request and returns the response body and Content-Type.
-// Any non-2xx status (including an unfollowed 3xx) is an error.
+// Any non-2xx status (including an unfollowed 3xx or a final 1xx) is an error.
//
// A path segment that is "." or ".." is refused before sending: a proxy that
// resolves dot segments would route the request to the parent resource. A
@@ -345,8 +345,8 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.Value
return nil, "", fmt.Errorf("openwa: reading response body: %w", err)
}
- // Any non-2xx (including an unfollowed 3xx) is an error.
- if resp.StatusCode >= 300 {
+ // Any non-2xx (including an unfollowed 3xx or a final 1xx) is an error.
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, "", parseAPIError(resp.StatusCode, data, method+" "+path, resp.Header)
}
return data, resp.Header.Get("Content-Type"), nil
diff --git a/sdk/go/client_test.go b/sdk/go/client_test.go
index 5b90c971d..d1d8f33b5 100644
--- a/sdk/go/client_test.go
+++ b/sdk/go/client_test.go
@@ -2033,3 +2033,43 @@ func TestReservedCharactersStayInTypedPathSegment(t *testing.T) {
t.Errorf("the ID moved into query or fragment: %s", rt.lastReq.URL)
}
}
+
+func TestNon2xxResponsesDoNotDecodeSuccess(t *testing.T) {
+ for _, status := range []int{101, 199, 200, 204, 299, 300, 503} {
+ for _, kind := range []string{"json", "bytes"} {
+ t.Run(fmt.Sprintf("%d/%s", status, kind), func(t *testing.T) {
+ body := `{"ok":true}`
+ if status == 204 {
+ body = ""
+ }
+ c := newTestClient(t, &recordTransport{status: status, body: body})
+ var parsed map[string]any
+ var raw []byte
+ var out any = &parsed
+ if kind == "bytes" {
+ out = &raw
+ }
+ err := c.Do(context.Background(), "GET", "/api/x", nil, nil, out)
+ if status < 200 || status >= 300 {
+ var apiErr *APIError
+ if !errors.As(err, &apiErr) || apiErr.StatusCode != status {
+ t.Errorf("error = %v, want APIError with status %d", err, status)
+ }
+ if parsed != nil || raw != nil {
+ t.Error("an error response decoded a success body")
+ }
+ } else if err != nil {
+ t.Fatal(err)
+ } else if status == 204 {
+ if parsed != nil || len(raw) != 0 {
+ t.Error("204 should keep an empty body")
+ }
+ } else if kind == "json" && parsed["ok"] != true {
+ t.Errorf("decoded JSON = %v", parsed)
+ } else if kind == "bytes" && string(raw) != body {
+ t.Errorf("decoded bytes = %q, want %q", raw, body)
+ }
+ })
+ }
+ }
+}