Skip to content

Commit

Permalink
Merge branch 'master' into MF-1864
Browse files Browse the repository at this point in the history
  • Loading branch information
drasko committed Jul 28, 2023
2 parents 593d2b6 + e925ca7 commit 6c11118
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
27 changes: 13 additions & 14 deletions pkg/errors/sdk_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ package errors

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
)

const err = "error"
const errorKey = "error"

var (
// ErrJSONErrKey indicates response body did not contain erorr message.
errJSONKey = New("response body expected error message json key not found")

// ErrUnknown indicates that an unknown error was found in the response body.
errUnknown = New("unknown error")
// Failed to read response body.
errRespBody = New("failed to read response body")
)

// SDKError is an error type for Mainflux SDK.
Expand Down Expand Up @@ -79,17 +76,19 @@ func CheckError(resp *http.Response, expectedStatusCodes ...int) SDKError {
}
}

var content map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&content); err != nil {
return NewSDKErrorWithStatus(err, resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
return NewSDKErrorWithStatus(Wrap(errRespBody, err), resp.StatusCode)

Check warning on line 81 in pkg/errors/sdk_errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/errors/sdk_errors.go#L79-L81

Added lines #L79 - L81 were not covered by tests
}
var content map[string]interface{}
_ = json.Unmarshal(body, &content)

Check warning on line 84 in pkg/errors/sdk_errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/errors/sdk_errors.go#L83-L84

Added lines #L83 - L84 were not covered by tests

if msg, ok := content[err]; ok {
if msg, ok := content[errorKey]; ok {

Check warning on line 86 in pkg/errors/sdk_errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/errors/sdk_errors.go#L86

Added line #L86 was not covered by tests
if v, ok := msg.(string); ok {
return NewSDKErrorWithStatus(errors.New(v), resp.StatusCode)
return NewSDKErrorWithStatus(New(v), resp.StatusCode)

Check warning on line 88 in pkg/errors/sdk_errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/errors/sdk_errors.go#L88

Added line #L88 was not covered by tests
}
return NewSDKErrorWithStatus(errUnknown, resp.StatusCode)
return NewSDKErrorWithStatus(fmt.Errorf("%v", msg), resp.StatusCode)

Check warning on line 90 in pkg/errors/sdk_errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/errors/sdk_errors.go#L90

Added line #L90 was not covered by tests
}

return NewSDKErrorWithStatus(errJSONKey, resp.StatusCode)
return NewSDKErrorWithStatus(New(string(body)), resp.StatusCode)

Check warning on line 93 in pkg/errors/sdk_errors.go

View check run for this annotation

Codecov / codecov/patch

pkg/errors/sdk_errors.go#L93

Added line #L93 was not covered by tests
}
6 changes: 2 additions & 4 deletions pkg/sdk/go/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/assert"
)

const eof = "EOF"

func newMessageService(cc policies.AuthServiceClient) adapter.Service {
pub := mocks.NewPublisher()

Expand Down Expand Up @@ -72,7 +70,7 @@ func TestSendMessage(t *testing.T) {
chanID: chanID,
msg: msg,
auth: invalidToken,
err: errors.NewSDKErrorWithStatus(errors.New(eof), http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.New(""), http.StatusUnauthorized),
},
"publish message with wrong content type": {
chanID: chanID,
Expand All @@ -90,7 +88,7 @@ func TestSendMessage(t *testing.T) {
chanID: chanID,
msg: msg,
auth: "invalid-token",
err: errors.NewSDKErrorWithStatus(errors.New(eof), http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(errors.New(""), http.StatusUnauthorized),
},
}
for desc, tc := range cases {
Expand Down

0 comments on commit 6c11118

Please sign in to comment.