Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(sdk-go): return nil with the error from four calls

Yudhi Armyndharis80de1228399f
diff --git a/sdk/go/chats.go b/sdk/go/chats.go
index 3eb3cb66e..f12d8cd87 100644
--- a/sdk/go/chats.go
+++ b/sdk/go/chats.go
@@ -27,7 +27,10 @@ func (s *ChatsService) List(ctx context.Context, sessionID string, query *ListCh
func (s *ChatsService) SubscribePresence(ctx context.Context, sessionID string, body SubscribePresenceRequest) (*SuccessResult, error) {
var out SuccessResult
err := s.client.do(ctx, "POST", "/api/sessions/"+pathEscape(sessionID)+"/presence/subscribe", nil, body, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// GetPresence returns the last presence reported for a chat, or nil when none has been — the chat
diff --git a/sdk/go/groups.go b/sdk/go/groups.go
index 487437e3a..b3db82a58 100644
--- a/sdk/go/groups.go
+++ b/sdk/go/groups.go
@@ -38,7 +38,10 @@ func (s *GroupsService) Get(ctx context.Context, sessionID, groupID string) (*Gr
func (s *GroupsService) JoinInfo(ctx context.Context, sessionID, code string) (*GroupJoinInfo, error) {
var out GroupJoinInfo
err := s.client.do(ctx, "GET", s.base(sessionID)+"/join-info", url.Values{"code": {code}}, nil, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// Create makes a new group. It answers the group SUMMARY, not the detail shape Get returns — there is
diff --git a/sdk/go/labels.go b/sdk/go/labels.go
index 6a16a526b..46b75892e 100644
--- a/sdk/go/labels.go
+++ b/sdk/go/labels.go
@@ -46,14 +46,20 @@ func (s *LabelsService) Chats(ctx context.Context, sessionID, labelID string) ([
func (s *LabelsService) Upsert(ctx context.Context, sessionID, labelID string, body UpsertLabelRequest) (*SuccessResult, error) {
var out SuccessResult
err := s.client.do(ctx, "PUT", s.base(sessionID)+"/"+pathEscape(labelID), nil, body, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// Delete removes a label; it disappears from every chat it was on. Baileys only.
func (s *LabelsService) Delete(ctx context.Context, sessionID, labelID string) (*SuccessResult, error) {
var out SuccessResult
err := s.client.do(ctx, "DELETE", s.base(sessionID)+"/"+pathEscape(labelID), nil, nil, &out)
- return &out, err
+ if err != nil {
+ return nil, err
+ }
+ return &out, nil
}
// ForChat returns the labels applied to a chat.
diff --git a/sdk/go/nil_result_on_error_test.go b/sdk/go/nil_result_on_error_test.go
new file mode 100644
index 000000000..49370dd27
--- /dev/null
+++ b/sdk/go/nil_result_on_error_test.go
@@ -0,0 +1,26 @@
+package openwa
+
+import (
+ "context"
+ "testing"
+)
+
+// A failed call must not also return a zero-valued record: Success=false or an empty preview reads
+// as if the server had answered it.
+func TestCallsReturnNilResultOnError(t *testing.T) {
+ c := newTestClient(t, &recordTransport{status: 409, body: `{"statusCode":409,"message":"busy","error":"Conflict"}`})
+ ctx := context.Background()
+
+ if res, err := c.Chats.SubscribePresence(ctx, "s1", SubscribePresenceRequest{}); err == nil || res != nil {
+ t.Errorf("Chats.SubscribePresence = %+v, %v; want nil and an error", res, err)
+ }
+ if res, err := c.Groups.JoinInfo(ctx, "s1", "code"); err == nil || res != nil {
+ t.Errorf("Groups.JoinInfo = %+v, %v; want nil and an error", res, err)
+ }
+ if res, err := c.Labels.Upsert(ctx, "s1", "l1", UpsertLabelRequest{}); err == nil || res != nil {
+ t.Errorf("Labels.Upsert = %+v, %v; want nil and an error", res, err)
+ }
+ if res, err := c.Labels.Delete(ctx, "s1", "l1"); err == nil || res != nil {
+ t.Errorf("Labels.Delete = %+v, %v; want nil and an error", res, err)
+ }
+}