client: include Status in json Error response for symmetry.
Also, remove RawBody from error, which was previously used for debugging.
This commit is contained in:
parent
112ffed189
commit
cd5df121a5
@ -74,11 +74,10 @@ func (c *Client) Run(ctx context.Context, ref string, messages []apitype.Message
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Error struct {
|
type Error struct {
|
||||||
Status int `json:"-"`
|
Status int `json:"status"`
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Field string `json:"field,omitempty"`
|
Field string `json:"field,omitempty"`
|
||||||
RawBody []byte `json:"-"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Error) Error() string {
|
func (e *Error) Error() string {
|
||||||
@ -110,13 +109,10 @@ func Do[Res any](ctx context.Context, method, urlStr string, in any) (*Res, erro
|
|||||||
defer res.Body.Close()
|
defer res.Body.Close()
|
||||||
|
|
||||||
if res.StatusCode/100 != 2 {
|
if res.StatusCode/100 != 2 {
|
||||||
var b bytes.Buffer
|
e, err := decodeJSON[Error](res.Body)
|
||||||
body := io.TeeReader(res.Body, &b)
|
|
||||||
e, err := decodeJSON[Error](body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
e.RawBody = b.Bytes()
|
|
||||||
return nil, e
|
return nil, e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,7 +55,8 @@ func Serve(h HandlerFunc, w http.ResponseWriter, r *http.Request) {
|
|||||||
if !errors.As(err, &oe) {
|
if !errors.As(err, &oe) {
|
||||||
oe = ErrInternal
|
oe = ErrInternal
|
||||||
}
|
}
|
||||||
w.WriteHeader(cmp.Or(oe.Status, 400))
|
oe.Status = cmp.Or(oe.Status, 400)
|
||||||
|
w.WriteHeader(oe.Status)
|
||||||
if err := EncodeJSON(w, oe); err != nil {
|
if err := EncodeJSON(w, oe); err != nil {
|
||||||
log.Printf("error encoding error: %v", err)
|
log.Printf("error encoding error: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user