MF-1115 - Improve the SDK error encoding (#1118)

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
This commit is contained in:
Manuel Imperiale 2020-04-15 23:41:31 +02:00 committed by GitHub
parent 4c970a8079
commit 6bd74575ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 42 additions and 26 deletions

View File

@ -61,7 +61,7 @@ func (sdk mfSDK) AddBootstrap(key string, cfg BoostrapConfig) (string, error) {
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", ErrFailedCreation
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
}
id := strings.TrimPrefix(resp.Header.Get("Location"), "/things/configs/")
@ -92,7 +92,7 @@ func (sdk mfSDK) ViewBoostrap(key, id string) (BoostrapConfig, error) {
if err := encodeError(resp.StatusCode); err != nil {
return BoostrapConfig{}, err
}
return BoostrapConfig{}, ErrFetchFailed
return BoostrapConfig{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var bc BoostrapConfig
@ -126,7 +126,7 @@ func (sdk mfSDK) UpdateBoostrap(key string, cfg BoostrapConfig) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
}
return nil
@ -150,7 +150,7 @@ func (sdk mfSDK) RemoveBoostrap(key, id string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedRemoval
return errors.Wrap(ErrFailedRemoval, errors.New(resp.Status))
}
return nil
@ -180,7 +180,7 @@ func (sdk mfSDK) Boostrap(key, id string) (BoostrapConfig, error) {
if err := encodeError(resp.StatusCode); err != nil {
return BoostrapConfig{}, err
}
return BoostrapConfig{}, ErrFetchFailed
return BoostrapConfig{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var bc BoostrapConfig

View File

@ -10,6 +10,8 @@ import (
"io/ioutil"
"net/http"
"strings"
"github.com/mainflux/mainflux/errors"
)
const channelsEndpoint = "channels"
@ -35,7 +37,7 @@ func (sdk mfSDK) CreateChannel(channel Channel, token string) (string, error) {
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", ErrFailedCreation
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
}
id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", channelsEndpoint))
@ -106,7 +108,7 @@ func (sdk mfSDK) Channels(token string, offset, limit uint64, name string) (Chan
if err := encodeError(resp.StatusCode); err != nil {
return ChannelsPage{}, err
}
return ChannelsPage{}, ErrFetchFailed
return ChannelsPage{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var cp ChannelsPage
@ -141,7 +143,7 @@ func (sdk mfSDK) ChannelsByThing(token, thingID string, offset, limit uint64) (C
if err := encodeError(resp.StatusCode); err != nil {
return ChannelsPage{}, err
}
return ChannelsPage{}, ErrFetchFailed
return ChannelsPage{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var cp ChannelsPage
@ -176,7 +178,7 @@ func (sdk mfSDK) Channel(id, token string) (Channel, error) {
if err := encodeError(resp.StatusCode); err != nil {
return Channel{}, err
}
return Channel{}, ErrFetchFailed
return Channel{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var c Channel
@ -210,7 +212,7 @@ func (sdk mfSDK) UpdateChannel(channel Channel, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
}
return nil
@ -234,7 +236,7 @@ func (sdk mfSDK) DeleteChannel(id, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
return errors.Wrap(ErrFailedRemoval, errors.New(resp.Status))
}
return nil

View File

@ -9,10 +9,11 @@ import (
"io/ioutil"
"net/http"
"strings"
"github.com/mainflux/mainflux/errors"
)
func (sdk mfSDK) SendMessage(chanName, msg, token string) error {
chanNameParts := strings.SplitN(chanName, ".", 2)
chanID := chanNameParts[0]
subtopicPart := ""
@ -37,7 +38,7 @@ func (sdk mfSDK) SendMessage(chanName, msg, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedPublish
return errors.Wrap(ErrFailedPublish, errors.New(resp.Status))
}
return nil
@ -73,7 +74,7 @@ func (sdk mfSDK) ReadMessages(chanName, token string) (MessagesPage, error) {
if err := encodeError(resp.StatusCode); err != nil {
return MessagesPage{}, err
}
return MessagesPage{}, ErrFailedRead
return MessagesPage{}, errors.Wrap(ErrFailedRead, errors.New(resp.Status))
}
var mp MessagesPage

View File

@ -9,6 +9,7 @@ import (
"testing"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/errors"
adapter "github.com/mainflux/mainflux/http"
"github.com/mainflux/mainflux/http/api"
"github.com/mainflux/mainflux/http/mocks"
@ -87,7 +88,7 @@ func TestSendMessage(t *testing.T) {
chanID: chanID,
msg: msg,
auth: mocks.ServiceErrToken,
err: sdk.ErrFailedPublish,
err: errors.Wrap(sdk.ErrFailedPublish, errors.New("503 Service Unavailable")),
},
}
for desc, tc := range cases {

View File

@ -67,6 +67,9 @@ var (
// ErrInvalidContentType indicates that nonexistent message content type
// was passed.
ErrInvalidContentType = errors.New("Unknown Content Type")
// ErrFetchVersion indicates that fetching of version failed.
ErrFetchVersion = errors.New("failed to fetch version")
)
// ContentType represents all possible content types.

View File

@ -10,6 +10,8 @@ import (
"io/ioutil"
"net/http"
"strings"
"github.com/mainflux/mainflux/errors"
)
const thingsEndpoint = "things"
@ -38,7 +40,7 @@ func (sdk mfSDK) CreateThing(thing Thing, token string) (string, error) {
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", ErrFailedCreation
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
}
id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", thingsEndpoint))
@ -179,7 +181,7 @@ func (sdk mfSDK) Thing(id, token string) (Thing, error) {
if err := encodeError(resp.StatusCode); err != nil {
return Thing{}, err
}
return Thing{}, ErrFetchFailed
return Thing{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var t Thing
@ -213,7 +215,7 @@ func (sdk mfSDK) UpdateThing(thing Thing, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
}
return nil
@ -237,7 +239,7 @@ func (sdk mfSDK) DeleteThing(id, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedRemoval
return errors.Wrap(ErrFailedRemoval, errors.New(resp.Status))
}
return nil
@ -264,7 +266,7 @@ func (sdk mfSDK) Connect(connIDs ConnectionIDs, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedConnection
return errors.Wrap(ErrFailedConnection, errors.New(resp.Status))
}
return nil
@ -288,7 +290,7 @@ func (sdk mfSDK) DisconnectThing(thingID, chanID, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedDisconnect
return errors.Wrap(ErrFailedDisconnect, errors.New(resp.Status))
}
return nil

View File

@ -8,6 +8,8 @@ import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/mainflux/mainflux/errors"
)
func (sdk mfSDK) CreateUser(user User) error {
@ -60,7 +62,7 @@ func (sdk mfSDK) User(token string) (User, error) {
if err := encodeError(resp.StatusCode); err != nil {
return User{}, err
}
return User{}, ErrFetchFailed
return User{}, errors.Wrap(ErrFetchFailed, errors.New(resp.Status))
}
var u User
@ -94,7 +96,7 @@ func (sdk mfSDK) CreateToken(user User) (string, error) {
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", ErrFailedCreation
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
}
var tr tokenRes
@ -127,7 +129,7 @@ func (sdk mfSDK) UpdateUser(user User, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
}
return nil
@ -159,7 +161,7 @@ func (sdk mfSDK) UpdatePassword(oldPass, newPass, token string) error {
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
}
return nil

View File

@ -8,6 +8,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"github.com/mainflux/mainflux/errors"
)
type version struct {
@ -29,7 +31,10 @@ func (sdk mfSDK) Version() (string, error) {
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("%d", resp.StatusCode)
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", errors.Wrap(ErrFetchVersion, errors.New(resp.Status))
}
var ver version