Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-go): return a nil result when a channel write fails

Yudhi Armyndharis0c4488566535
diff --git a/sdk/go/channels.go b/sdk/go/channels.go
index 86713ea42..0ece20366 100644
--- a/sdk/go/channels.go
+++ b/sdk/go/channels.go
@@ -38,7 +38,10 @@ func (s *ChannelsService) Messages(ctx context.Context, sessionID, channelID str
func (s *ChannelsService) Create(ctx context.Context, sessionID string, body CreateChannelRequest) (*ChannelRecord, error) {
var out ChannelRecord
err := s.client.do(ctx, "POST", s.base(sessionID), nil, body, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// Delete destroys a channel this account owns. Irreversible, and every subscriber loses it.
@@ -48,14 +51,20 @@ func (s *ChannelsService) Create(ctx context.Context, sessionID string, body Cre
func (s *ChannelsService) Delete(ctx context.Context, sessionID, channelID string) (*SuccessResult, error) {
var out SuccessResult
err := s.client.do(ctx, "POST", s.base(sessionID)+"/"+pathEscape(channelID)+"/delete", nil, nil, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// Mute mutes or unmutes a channel's notifications. The subscription is untouched either way.
func (s *ChannelsService) Mute(ctx context.Context, sessionID, channelID string, body MuteChannelRequest) (*SuccessResult, error) {
var out SuccessResult
err := s.client.do(ctx, "POST", s.base(sessionID)+"/"+pathEscape(channelID)+"/mute", nil, body, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// Subscribe subscribes to a channel by invite code. Requires an OPERATOR-level key.
diff --git a/sdk/go/channels_error_result_test.go b/sdk/go/channels_error_result_test.go
new file mode 100644
index 000000000..534240172
--- /dev/null
+++ b/sdk/go/channels_error_result_test.go
@@ -0,0 +1,23 @@
+package openwa
+
+import (
+ "context"
+ "testing"
+)
+
+// A failed call must not also return a zero-valued record: a caller that checks the result reads
+// an empty channel or Success=false as if the server had answered it.
+func TestChannelsWritesReturnNilResultOnError(t *testing.T) {
+ c := newTestClient(t, &recordTransport{status: 409, body: `{"statusCode":409,"message":"busy","error":"Conflict"}`})
+ ctx := context.Background()
+
+ if res, err := c.Channels.Create(ctx, "s1", CreateChannelRequest{}); err == nil || res != nil {
+ t.Errorf("Create = %+v, %v; want nil and an error", res, err)
+ }
+ if res, err := c.Channels.Delete(ctx, "s1", "c1"); err == nil || res != nil {
+ t.Errorf("Delete = %+v, %v; want nil and an error", res, err)
+ }
+ if res, err := c.Channels.Mute(ctx, "s1", "c1", MuteChannelRequest{}); err == nil || res != nil {
+ t.Errorf("Mute = %+v, %v; want nil and an error", res, err)
+ }
+}