added 404 error
This commit is contained in:
parent
6da132106f
commit
d130ea4e2b
2 changed files with 11 additions and 10 deletions
|
|
@ -28,6 +28,10 @@ type Client struct {
|
|||
insecure bool
|
||||
}
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
)
|
||||
|
||||
type ClientOption func(*Client)
|
||||
|
||||
func WithEndpoint(endpoint string) ClientOption {
|
||||
|
|
@ -158,8 +162,6 @@ func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Re
|
|||
return req, nil
|
||||
}
|
||||
|
||||
var HTTPErr = errors.New("http error")
|
||||
|
||||
func (c *Client) Do(r *http.Request, v interface{}) (resp *http.Response, err error) {
|
||||
if c.debugWriter != nil {
|
||||
dumpReq, err := httputil.DumpRequestOut(r, true)
|
||||
|
|
@ -189,12 +191,5 @@ func (c *Client) Do(r *http.Request, v interface{}) (resp *http.Response, err er
|
|||
}
|
||||
|
||||
err = decodeResp(resp, v)
|
||||
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("%w: %s: %s", HTTPErr, errors.New(resp.Status), err.Error())
|
||||
return
|
||||
}
|
||||
err = fmt.Errorf("%w: %s", HTTPErr, errors.New(resp.Status))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,10 +22,16 @@ func decodeResp(resp *http.Response, v interface{}) error {
|
|||
}
|
||||
return json.NewDecoder(resp.Body).Decode(&v)
|
||||
}
|
||||
if resp.StatusCode == 404 {
|
||||
return ErrNotFound
|
||||
}
|
||||
var e Error
|
||||
err := json.NewDecoder(resp.Body).Decode(&e)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
|
||||
return errors.New(e.Message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue