Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-go): return an error for a nil context instead of panicking
Yudhi Armyndharisb045a92e2323
diff --git a/sdk/go/client.go b/sdk/go/client.goindex e955fec05..2ce04489d 100644--- a/sdk/go/client.go+++ b/sdk/go/client.go@@ -277,17 +277,15 @@ func (c *Client) doRaw(ctx context.Context, method, path string, query url.ValuerawURL += sep + query.Encode()}- key, hasKey := ctx.Value(idempotencyContextKey{}).(string)- if hasKey {- if len(key) < 1 || len(key) > 255 || strings.IndexFunc(key, func(r rune) bool { return r < 0x21 || r > 0x7e }) >= 0 {- return nil, "", errors.New("openwa: idempotency key must contain 1-255 visible ASCII characters")- }- }req, err := http.NewRequestWithContext(ctx, method, rawURL, reader)if err != nil {return nil, "", fmt.Errorf("openwa: building request: %w", err)}- if hasKey {+ // Read through the request: NewRequestWithContext has just refused a nil ctx, which ctx.Value would panic on.+ if key, hasKey := req.Context().Value(idempotencyContextKey{}).(string); hasKey {+ if len(key) < 1 || len(key) > 255 || strings.IndexFunc(key, func(r rune) bool { return r < 0x21 || r > 0x7e }) >= 0 {+ return nil, "", errors.New("openwa: idempotency key must contain 1-255 visible ASCII characters")+ }req.Header.Set("Idempotency-Key", key)}if body != nil {diff --git a/sdk/go/client_test.go b/sdk/go/client_test.goindex a1a80f2e2..f6da3e906 100644--- a/sdk/go/client_test.go+++ b/sdk/go/client_test.go@@ -1792,6 +1792,25 @@ func TestSendIdempotencyKey(t *testing.T) {}}+// A nil context is the caller's mistake, and net/http names it as an error. The idempotency lookup must+// not turn it into a panic that takes the calling program down.+func TestNilContextReturnsAnErrorInsteadOfPanicking(t *testing.T) {+ rt := &recordTransport{status: 200, body: `{}`}+ c := newTestClient(t, rt)+ defer func() {+ if r := recover(); r != nil {+ t.Fatalf("a nil context panicked: %v", r)+ }+ }()+ //nolint:staticcheck // the nil context is the point of the test+ if _, err := c.Messages.SendText(nil, "s", SendTextRequest{}); err == nil {+ t.Fatal("a nil context was accepted")+ }+ if rt.lastReq != nil {+ t.Fatal("a request was sent with a nil context")+ }+}+func TestRedrivePreservesAnEmptyIDsFilter(t *testing.T) {rt := &recordTransport{status: 200, body: `{}`}c := newTestClient(t, rt)