1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-26 13:48:53 +08:00

CLI bug fixes (#1866)

fix: comment formt



fix: CI error



change to mf errors



change to lowercase in error first word



fix: send message test case



remove: unused const

Signed-off-by: Arvindh <arvindh91@gmail.com>
This commit is contained in:
Arvindh 2023-07-28 18:35:43 +05:30 committed by GitHub
parent 7cccba91c9
commit e925ca7e65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 18 deletions

View File

@ -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.
@ -79,17 +76,19 @@ func CheckError(resp *http.Response, expectedStatusCodes ...int) SDKError {
}
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return NewSDKErrorWithStatus(Wrap(errRespBody, err), resp.StatusCode)
}
var content map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&content); err != nil {
return NewSDKErrorWithStatus(err, resp.StatusCode)
}
_ = json.Unmarshal(body, &content)
if msg, ok := content[err]; ok {
if msg, ok := content[errorKey]; ok {
if v, ok := msg.(string); ok {
return NewSDKErrorWithStatus(errors.New(v), resp.StatusCode)
return NewSDKErrorWithStatus(New(v), resp.StatusCode)
}
return NewSDKErrorWithStatus(errUnknown, resp.StatusCode)
return NewSDKErrorWithStatus(fmt.Errorf("%v", msg), resp.StatusCode)
}
return NewSDKErrorWithStatus(errJSONKey, resp.StatusCode)
return NewSDKErrorWithStatus(New(string(body)), resp.StatusCode)
}

View File

@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/assert"
)
const eof = "EOF"
func newMessageService(cc policies.AuthServiceClient) adapter.Service {
pub := mocks.NewPublisher()
@ -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,
@ -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 {