Skip to content

Commit

Permalink
fix: make the code clearer for postServiceAction
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Dec 7, 2023
1 parent b04bbe8 commit b764518
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,13 @@ func (c *Client) postServiceAction(resPath string, req interface{}) error {
if resErr != nil && resp == nil {
return fmt.Errorf("postResource: %w", resErr)
}
msg := api.MetaMessage{}

if err := json.Unmarshal(resp, &msg); err != nil {
// if the response is not a MetaMessage, it's probably a success message
// Therefore we just return the original error from the request
return resErr
msg := api.MetaMessage{}
if err := json.Unmarshal(resp, &msg); err == nil {
return Error{Message: msg}
}
return Error{Message: msg}

// if the response is not a MetaMessage, it's probably a success message
// Therefore we just return the original error from the request which is nil on success
return resErr
}
4 changes: 2 additions & 2 deletions sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_SDK_Client_GetResource_HTTPError(t *testing.T) {

client := NewClient(mockServer.URL)
res, err := client.getResource("/error")
assert.Nil(t, res)
assert.Empty(t, res)
assert.Error(t, err)
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func Test_SDK_Client_PostResource_HTTPError(t *testing.T) {
client := NewClient(mockServer.URL)
res, err := client.postResource("/error", nil)

assert.Nil(t, res)
assert.Empty(t, res)
assert.Error(t, err)
}
}
Expand Down

0 comments on commit b764518

Please sign in to comment.