Skip to content

Commit

Permalink
fix(client): supported custom error payload
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jun 9, 2024
1 parent 52f724e commit 98f2a6d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions pkg/courierhttp/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type HttpTransport = func(rt http.RoundTripper) http.RoundTripper

type Client struct {
Endpoint string `flag:""`
NewError func() error
HttpTransports []HttpTransport
}

Expand All @@ -51,6 +52,7 @@ func (c *Client) Do(ctx context.Context, req any, metas ...courier.Metadata) cou
r, err := c.newRequest(ctx, req, metas...)
if err != nil {
return &result{
c: c,
err: statuserror.Wrap(err, http.StatusInternalServerError, "RequestFailed"),
}
}
Expand All @@ -66,16 +68,19 @@ func (c *Client) Do(ctx context.Context, req any, metas ...courier.Metadata) cou
if err != nil {
if errors.Unwrap(err) == context.Canceled {
return &result{
c: c,
err: statuserror.Wrap(err, 499, "ClientClosedRequest"),
}
}

return &result{
c: c,
err: statuserror.Wrap(err, http.StatusInternalServerError, "RequestFailed"),
}
}

return &result{
c: c,
Response: resp,
}
}
Expand Down Expand Up @@ -107,6 +112,7 @@ func (c *Client) newRequest(ctx context.Context, r any, metas ...courier.Metadat
}

type result struct {
c *Client
*http.Response
err error
}
Expand Down Expand Up @@ -139,10 +145,14 @@ func (r *result) Into(body any) (courier.Metadata, error) {
meta := courier.Metadata(r.Response.Header)

if !isOk(r.Response.StatusCode) {
body = &statuserror.StatusErr{
Code: r.Response.StatusCode,
Msg: r.Response.Status,
Sources: []string{r.Response.Request.Host},
if r.c.NewError != nil {
body = r.c.NewError()
} else {
body = &statuserror.StatusErr{
Code: r.Response.StatusCode,
Msg: r.Response.Status,
Sources: []string{r.Response.Request.Host},
}
}
}

Expand Down

0 comments on commit 98f2a6d

Please sign in to comment.