Skip to content

Commit

Permalink
NOISSUE - Create func to encode SDK errors (#1110)
Browse files Browse the repository at this point in the history
* NOISSUE - Create func to encode SDK errors

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>

* Use var declaration

Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
  • Loading branch information
manuio authored Apr 14, 2020
1 parent d8dca28 commit 5b52299
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 179 deletions.
2 changes: 0 additions & 2 deletions cli/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/spf13/cobra"
)

const thingsEP = "things"

var cmdThings = []cobra.Command{
cobra.Command{
Use: "create",
Expand Down
78 changes: 24 additions & 54 deletions sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ func (sdk mfSDK) CreateChannel(channel Channel, token string) (string, error) {
}

if resp.StatusCode != http.StatusCreated {
switch resp.StatusCode {
case http.StatusBadRequest:
return "", ErrInvalidArgs
case http.StatusForbidden:
return "", ErrUnauthorized
default:
return "", ErrFailedCreation
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", ErrFailedCreation
}

id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", channelsEndpoint))
Expand Down Expand Up @@ -67,27 +63,23 @@ func (sdk mfSDK) CreateChannels(channels []Channel, token string) ([]Channel, er
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
switch resp.StatusCode {
case http.StatusBadRequest:
return []Channel{}, ErrInvalidArgs
case http.StatusForbidden:
return []Channel{}, ErrUnauthorized
default:
return []Channel{}, ErrFailedCreation
if err := encodeError(resp.StatusCode); err != nil {
return []Channel{}, err
}
return []Channel{}, ErrFailedCreation
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []Channel{}, err
}

var p createChannelsRes
if err := json.Unmarshal(body, &p); err != nil {
var ccr createChannelsRes
if err := json.Unmarshal(body, &ccr); err != nil {
return []Channel{}, err
}

return p.Channels, nil
return ccr.Channels, nil
}

func (sdk mfSDK) Channels(token string, offset, limit uint64, name string) (ChannelsPage, error) {
Expand All @@ -111,14 +103,10 @@ func (sdk mfSDK) Channels(token string, offset, limit uint64, name string) (Chan
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return ChannelsPage{}, ErrInvalidArgs
case http.StatusForbidden:
return ChannelsPage{}, ErrUnauthorized
default:
return ChannelsPage{}, ErrFetchFailed
if err := encodeError(resp.StatusCode); err != nil {
return ChannelsPage{}, err
}
return ChannelsPage{}, ErrFetchFailed
}

var cp ChannelsPage
Expand Down Expand Up @@ -150,14 +138,10 @@ func (sdk mfSDK) ChannelsByThing(token, thingID string, offset, limit uint64) (C
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return ChannelsPage{}, ErrInvalidArgs
case http.StatusForbidden:
return ChannelsPage{}, ErrUnauthorized
default:
return ChannelsPage{}, ErrFetchFailed
if err := encodeError(resp.StatusCode); err != nil {
return ChannelsPage{}, err
}
return ChannelsPage{}, ErrFetchFailed
}

var cp ChannelsPage
Expand Down Expand Up @@ -189,14 +173,10 @@ func (sdk mfSDK) Channel(id, token string) (Channel, error) {
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusForbidden:
return Channel{}, ErrUnauthorized
case http.StatusNotFound:
return Channel{}, ErrNotFound
default:
return Channel{}, ErrFetchFailed
if err := encodeError(resp.StatusCode); err != nil {
return Channel{}, err
}
return Channel{}, ErrFetchFailed
}

var c Channel
Expand Down Expand Up @@ -227,16 +207,10 @@ func (sdk mfSDK) UpdateChannel(channel Channel, token string) error {
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
default:
return ErrFailedUpdate
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
}

return nil
Expand All @@ -257,14 +231,10 @@ func (sdk mfSDK) DeleteChannel(id, token string) error {
}

if resp.StatusCode != http.StatusNoContent {
switch resp.StatusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusForbidden:
return ErrUnauthorized
default:
return ErrFailedUpdate
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
}

return nil
Expand Down
22 changes: 7 additions & 15 deletions sdk/go/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ func (sdk mfSDK) SendMessage(chanName, msg, token string) error {
}

if resp.StatusCode != http.StatusAccepted {
switch resp.StatusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusForbidden:
return ErrUnauthorized
default:
return ErrFailedPublish
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedPublish
}

return nil
Expand Down Expand Up @@ -74,17 +70,13 @@ func (sdk mfSDK) ReadMessages(chanName, token string) (MessagesPage, error) {
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return MessagesPage{}, ErrInvalidArgs
case http.StatusForbidden:
return MessagesPage{}, ErrUnauthorized
default:
return MessagesPage{}, ErrFailedRead
if err := encodeError(resp.StatusCode); err != nil {
return MessagesPage{}, err
}
return MessagesPage{}, ErrFailedRead
}

mp := MessagesPage{}
var mp MessagesPage
if err := json.Unmarshal(body, &mp); err != nil {
return MessagesPage{}, err
}
Expand Down
15 changes: 15 additions & 0 deletions sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,18 @@ func createURL(baseURL, prefix, endpoint string) string {

return fmt.Sprintf("%s/%s/%s", baseURL, prefix, endpoint)
}

func encodeError(statusCode int) error {
switch statusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
case http.StatusConflict:
return ErrConflict
default:
return nil
}
}
98 changes: 30 additions & 68 deletions sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ func (sdk mfSDK) CreateThing(thing Thing, token string) (string, error) {
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
switch resp.StatusCode {
case http.StatusBadRequest:
return "", ErrInvalidArgs
case http.StatusForbidden:
return "", ErrUnauthorized
default:
return "", ErrFailedCreation
if err := encodeError(resp.StatusCode); err != nil {
return "", err
}
return "", ErrFailedCreation
}

id := strings.TrimPrefix(resp.Header.Get("Location"), fmt.Sprintf("/%s/", thingsEndpoint))
Expand Down Expand Up @@ -70,27 +66,23 @@ func (sdk mfSDK) CreateThings(things []Thing, token string) ([]Thing, error) {
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
switch resp.StatusCode {
case http.StatusBadRequest:
return []Thing{}, ErrInvalidArgs
case http.StatusForbidden:
return []Thing{}, ErrUnauthorized
default:
return []Thing{}, ErrFailedCreation
if err := encodeError(resp.StatusCode); err != nil {
return []Thing{}, err
}
return []Thing{}, ErrFailedCreation
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []Thing{}, err
}

var p createThingsRes
if err := json.Unmarshal(body, &p); err != nil {
var ctr createThingsRes
if err := json.Unmarshal(body, &ctr); err != nil {
return []Thing{}, err
}

return p.Things, nil
return ctr.Things, nil
}

func (sdk mfSDK) Things(token string, offset, limit uint64, name string) (ThingsPage, error) {
Expand All @@ -114,14 +106,10 @@ func (sdk mfSDK) Things(token string, offset, limit uint64, name string) (Things
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return ThingsPage{}, ErrInvalidArgs
case http.StatusForbidden:
return ThingsPage{}, ErrUnauthorized
default:
return ThingsPage{}, ErrFetchFailed
if err := encodeError(resp.StatusCode); err != nil {
return ThingsPage{}, err
}
return ThingsPage{}, ErrFetchFailed
}

var tp ThingsPage
Expand Down Expand Up @@ -153,14 +141,10 @@ func (sdk mfSDK) ThingsByChannel(token, chanID string, offset, limit uint64) (Th
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return ThingsPage{}, ErrInvalidArgs
case http.StatusForbidden:
return ThingsPage{}, ErrUnauthorized
default:
return ThingsPage{}, ErrFetchFailed
if err := encodeError(resp.StatusCode); err != nil {
return ThingsPage{}, err
}
return ThingsPage{}, ErrFetchFailed
}

var tp ThingsPage
Expand Down Expand Up @@ -192,14 +176,10 @@ func (sdk mfSDK) Thing(id, token string) (Thing, error) {
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusForbidden:
return Thing{}, ErrUnauthorized
case http.StatusNotFound:
return Thing{}, ErrNotFound
default:
return Thing{}, ErrFetchFailed
if err := encodeError(resp.StatusCode); err != nil {
return Thing{}, err
}
return Thing{}, ErrFetchFailed
}

var t Thing
Expand Down Expand Up @@ -230,16 +210,10 @@ func (sdk mfSDK) UpdateThing(thing Thing, token string) error {
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
return ErrInvalidArgs
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
default:
return ErrFailedUpdate
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedUpdate
}

return nil
Expand All @@ -260,14 +234,10 @@ func (sdk mfSDK) DeleteThing(id, token string) error {
}

if resp.StatusCode != http.StatusNoContent {
switch resp.StatusCode {
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusBadRequest:
return ErrInvalidArgs
default:
return ErrFailedRemoval
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedRemoval
}

return nil
Expand All @@ -291,14 +261,10 @@ func (sdk mfSDK) Connect(connIDs ConnectionIDs, token string) error {
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
default:
return ErrFailedConnection
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedConnection
}

return nil
Expand All @@ -319,14 +285,10 @@ func (sdk mfSDK) DisconnectThing(thingID, chanID, token string) error {
}

if resp.StatusCode != http.StatusNoContent {
switch resp.StatusCode {
case http.StatusForbidden:
return ErrUnauthorized
case http.StatusNotFound:
return ErrNotFound
default:
return ErrFailedDisconnect
if err := encodeError(resp.StatusCode); err != nil {
return err
}
return ErrFailedDisconnect
}

return nil
Expand Down
Loading

0 comments on commit 5b52299

Please sign in to comment.