Skip to content

Commit

Permalink
chore: add better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmuhia committed Jul 17, 2024
1 parent 76a5e46 commit 00cb56a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
38 changes: 30 additions & 8 deletions sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ type CommsLib struct {
senderID string
}

// APIErrorResponse is the representation of an error response
type APIErrorResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Data map[string]interface{} `json:"data"`
}

// NewSILCommsLib initializes a new implementation of the SIL Comms SDK
func NewSILCommsLib() (*CommsLib, error) {
client, err := newClient()
Expand Down Expand Up @@ -69,7 +76,12 @@ func (l CommsLib) SendBulkSMS(ctx context.Context, message string, recipients []
}

if response.StatusCode != http.StatusAccepted {
err := fmt.Errorf("invalid send bulk sms response code, got: %d", response.StatusCode)
var apiErr APIErrorResponse
if err := json.NewDecoder(response.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("invalid send premium sms response code, got: %d", response.StatusCode)
}

err := fmt.Errorf("invalid send bulk sms response code, got: %d, error detail: %s", response.StatusCode, apiErr.Data)
return nil, err
}

Expand Down Expand Up @@ -110,7 +122,12 @@ func (l CommsLib) SendPremiumSMS(ctx context.Context, message, msisdn, subscript
}

if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("invalid send premium sms response code, got: %d", response.StatusCode)
var apiErr APIErrorResponse
if err := json.NewDecoder(response.Body).Decode(&apiErr); err != nil {
return nil, fmt.Errorf("invalid send premium sms response code, got: %d", response.StatusCode)
}

return nil, fmt.Errorf("invalid send premium sms response code, got: %d, error detail: %s", response.StatusCode, apiErr.Data)
}

var resp APIResponse
Expand All @@ -135,12 +152,12 @@ func (l CommsLib) SendPremiumSMS(ctx context.Context, message, msisdn, subscript
func (l CommsLib) ActivateSubscription(ctx context.Context, offer string, msisdn string, activate bool) (bool, error) {
path := "/v1/sms/subscriptions/"
payload := struct {
Offer string `json:"offer"`
Msisdn string `json:"msisdn"`
Activate bool `json:"activate"`
Offer string `json:"offer"`
Msisdn string `json:"msisdn"`
Activate bool `json:"activate"`
}{
Offer: offer,
Msisdn: msisdn,
Offer: offer,
Msisdn: msisdn,
Activate: activate,
}

Expand All @@ -150,7 +167,12 @@ func (l CommsLib) ActivateSubscription(ctx context.Context, offer string, msisdn
}

if response.StatusCode != http.StatusOK {
return false, fmt.Errorf("invalid activate subscription response code, got: %d", response.StatusCode)
var apiErr APIErrorResponse
if err := json.NewDecoder(response.Body).Decode(&apiErr); err != nil {
return false, fmt.Errorf("invalid send premium sms response code, got: %d", response.StatusCode)
}

return false, fmt.Errorf("invalid activate subscription response code, got: %d, error detail: %s", response.StatusCode, apiErr.Data)
}

return true, nil
Expand Down
12 changes: 6 additions & 6 deletions sms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ func TestSILCommsLib_SendPremiumSMS(t *testing.T) {
func TestSILCommsLib_ActivateSubscription(t *testing.T) {
ctx := context.Background()
type args struct {
ctx context.Context
offer string
msisdn string
ctx context.Context
offer string
msisdn string
activate bool
}
tests := []struct {
Expand All @@ -276,9 +276,9 @@ func TestSILCommsLib_ActivateSubscription(t *testing.T) {
{
name: "Happy case: activate subscription bypass sdp",
args: args{
ctx: ctx,
offer: "01262626626",
msisdn: gofakeit.Phone(),
ctx: ctx,
offer: "01262626626",
msisdn: gofakeit.Phone(),
activate: false,
},
wantErr: false,
Expand Down

0 comments on commit 00cb56a

Please sign in to comment.