Skip to content

Commit

Permalink
better wise errors
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Sep 26, 2023
1 parent 7a5a48d commit 0520e65
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ func unmarshalError(statusCode int, body io.ReadCloser) *modulrError {
_ = json.NewDecoder(body).Decode(&ces)

if len(ces) == 0 {
return &modulrError{}
return &modulrError{
StatusCode: statusCode,
}
}

return &modulrError{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func unmarshalError(statusCode int, body io.ReadCloser) *moneycorpError {
_ = json.NewDecoder(body).Decode(&ces)

if len(ces.Errors) == 0 {
return &moneycorpError{}
return &moneycorpError{
StatusCode: statusCode,
}
}

return &moneycorpError{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (w *Client) GetBalances(ctx context.Context, profileID uint64) ([]*Balance,
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
return nil, unmarshalError(res.StatusCode, res.Body).Error()
}

var balances []*Balance
Expand Down
42 changes: 42 additions & 0 deletions components/payments/internal/app/connectors/wise/client/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package client

import (
"encoding/json"
"fmt"
"io"
)

type wiseErrors struct {
Errors []*wiseError `json:"errors"`
}

type wiseError struct {
StatusCode int `json:"-"`
Code string `json:"code"`
Message string `json:"message"`
}

func (me *wiseError) Error() error {
if me.Message == "" {
return fmt.Errorf("unexpected status code: %d", me.StatusCode)
}

return fmt.Errorf("%d: %s", me.Code, me.Message)
}

func unmarshalError(statusCode int, body io.ReadCloser) *wiseError {
var ces wiseErrors
_ = json.NewDecoder(body).Decode(&ces)

if len(ces.Errors) == 0 {
return &wiseError{
StatusCode: statusCode,
}
}

return &wiseError{
StatusCode: statusCode,
Code: ces.Errors[0].Code,
Message: ces.Errors[0].Message,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,17 @@ func (w *Client) GetPayout(ctx context.Context, payoutID string) (*Payout, error
if err != nil {
return nil, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, unmarshalError(res.StatusCode, res.Body).Error()
}

body, err := io.ReadAll(res.Body)
if err != nil {
res.Body.Close()

return nil, fmt.Errorf("failed to read response body: %w", err)
}

if err = res.Body.Close(); err != nil {
return nil, fmt.Errorf("failed to close response body: %w", err)
}

var payout Payout
err = json.Unmarshal(body, &payout)
if err != nil {
Expand All @@ -106,11 +105,10 @@ func (w *Client) CreatePayout(quote Quote, targetAccount uint64, transactionID s
if err != nil {
return nil, err
}

defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
return nil, unmarshalError(res.StatusCode, res.Body).Error()
}

var response Payout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (w *Client) GetProfiles() ([]Profile, error) {
}

if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode)
return nil, unmarshalError(res.StatusCode, res.Body).Error()
}

err = json.Unmarshal(body, &profiles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math/big"
"net/http"

"github.com/google/uuid"
)
Expand All @@ -30,9 +31,12 @@ func (w *Client) CreateQuote(profileID string, currency string, amount *big.Floa
if err != nil {
return response, err
}

defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return response, unmarshalError(res.StatusCode, res.Body).Error()
}

body, err := io.ReadAll(res.Body)
if err != nil {
return response, fmt.Errorf("failed to read response body: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,17 @@ func (w *Client) GetRecipientAccounts(ctx context.Context, profileID uint64) ([]
if err != nil {
return recipientAccounts, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, unmarshalError(res.StatusCode, res.Body).Error()
}

body, err := io.ReadAll(res.Body)
if err != nil {
res.Body.Close()

return nil, fmt.Errorf("failed to read response body: %w", err)
}

if err = res.Body.Close(); err != nil {
return nil, fmt.Errorf("failed to close response body: %w", err)
}

err = json.Unmarshal(body, &recipientAccounts)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal transfers: %w", err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,17 @@ func (w *Client) GetTransfers(ctx context.Context, profile *Profile) ([]Transfer
if err != nil {
return transfers, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
return nil, unmarshalError(res.StatusCode, res.Body).Error()
}

body, err := io.ReadAll(res.Body)
if err != nil {
res.Body.Close()

return nil, fmt.Errorf("failed to read response body: %w", err)
}

if err = res.Body.Close(); err != nil {
return nil, fmt.Errorf("failed to close response body: %w", err)
}

var transferList []Transfer

err = json.Unmarshal(body, &transferList)
Expand Down

0 comments on commit 0520e65

Please sign in to comment.