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:
Blake Mizerany 2024-03-31 09:30:01 -07:00
parent 112ffed189
commit cd5df121a5
2 changed files with 4 additions and 7 deletions

View File

@ -74,11 +74,10 @@ func (c *Client) Run(ctx context.Context, ref string, messages []apitype.Message
}
type Error struct {
Status int `json:"-"`
Status int `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
Field string `json:"field,omitempty"`
RawBody []byte `json:"-"`
}
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()
if res.StatusCode/100 != 2 {
var b bytes.Buffer
body := io.TeeReader(res.Body, &b)
e, err := decodeJSON[Error](body)
e, err := decodeJSON[Error](res.Body)
if err != nil {
return nil, err
}
e.RawBody = b.Bytes()
return nil, e
}

View File

@ -55,7 +55,8 @@ func Serve(h HandlerFunc, w http.ResponseWriter, r *http.Request) {
if !errors.As(err, &oe) {
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 {
log.Printf("error encoding error: %v", err)
}