Skip to content

Commit 585fa12

Browse files
authored
Fix: Better error handling from payd (#20)
* handle errors from payd better * even better error handling * fixed linter * fixed panic on 500
1 parent bdf0d8a commit 585fa12

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

data/http_client.go

+39-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"io/ioutil"
99
"net/http"
1010

11+
"github.com/libsv/go-p4"
1112
"github.com/pkg/errors"
13+
validator "github.com/theflyingcodr/govalidator"
1214
"github.com/theflyingcodr/lathos/errs"
1315
)
1416

@@ -53,16 +55,7 @@ func (c *client) Do(ctx context.Context, method, endpoint string, expStatus int,
5355
_ = resp.Body.Close()
5456
}()
5557
if resp.StatusCode != expStatus {
56-
switch resp.StatusCode {
57-
case http.StatusNotFound:
58-
return errs.NewErrNotFound("404", fmt.Sprintf("item not found for '%s' '%s'", method, endpoint))
59-
case http.StatusConflict:
60-
return errs.NewErrDuplicate("409", fmt.Sprintf("item already exists for '%s' '%s'", method, endpoint))
61-
default:
62-
body, _ := ioutil.ReadAll(resp.Body)
63-
return fmt.Errorf("error for '%s' '%s'. Status Received : '%d', Status Expected : '%d'. \nBody: %s", method, endpoint, resp.StatusCode, expStatus, body)
64-
}
65-
58+
return c.handleErr(resp, expStatus)
6659
}
6760
if out != nil {
6861
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
@@ -71,3 +64,39 @@ func (c *client) Do(ctx context.Context, method, endpoint string, expStatus int,
7164
}
7265
return nil
7366
}
67+
68+
func (c *client) handleErr(resp *http.Response, expStatus int) error {
69+
if resp.StatusCode == http.StatusBadRequest {
70+
brErr := p4.BadRequestError{
71+
Errors: make(validator.ErrValidation),
72+
}
73+
if err := json.NewDecoder(resp.Body).Decode(&brErr); err != nil {
74+
return errors.WithStack(err)
75+
}
76+
return brErr.Errors
77+
}
78+
79+
switch resp.StatusCode {
80+
case http.StatusNotFound:
81+
var msg p4.ClientError
82+
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {
83+
return errors.WithStack(err)
84+
}
85+
return errs.NewErrNotFound(msg.Code, msg.Message)
86+
case http.StatusConflict:
87+
var msg p4.ClientError
88+
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {
89+
return errors.WithStack(err)
90+
}
91+
return errs.NewErrDuplicate(msg.Code, msg.Message)
92+
case http.StatusUnprocessableEntity:
93+
var msg p4.ClientError
94+
if err := json.NewDecoder(resp.Body).Decode(&msg); err != nil {
95+
return errors.WithStack(err)
96+
}
97+
return errs.NewErrUnprocessable(msg.Code, msg.Message)
98+
default:
99+
body, _ := ioutil.ReadAll(resp.Body)
100+
return fmt.Errorf("error for '%s' '%s'. Status Received : '%d', Status Expected : '%d'. \nBody: %s", resp.Request.Method, resp.Request.RequestURI, resp.StatusCode, expStatus, body)
101+
}
102+
}

pptcl.go p4.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package p4
22

3+
import validator "github.com/theflyingcodr/govalidator"
4+
35
// ClientError defines an error type that can be returned to handle client errors.
46
type ClientError struct {
57
ID string `json:"id" example:"e97970bf-2a88-4bc8-90e6-2f597a80b93d"`
68
Code string `json:"code" example:"N01"`
79
Title string `json:"title" example:"not found"`
810
Message string `json:"message" example:"unable to find foo when loading bar"`
911
}
12+
13+
// BadRequestError defines an error type to handle validation errors.
14+
type BadRequestError struct {
15+
Errors validator.ErrValidation `json:"errors"`
16+
}

0 commit comments

Comments
 (0)