Skip to content

Commit 27e5281

Browse files
committed
Return structured error w/HTTP Status
1 parent 5a90301 commit 27e5281

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

error.go

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,32 @@ func (e Errors) Error() string {
8383
return strings.Join(msgs, ", ")
8484
}
8585

86-
// HandleError makes an error from http.Response.
87-
// It is the caller's responsibility to close resp.Body.
86+
type APIError struct {
87+
Status string
88+
StatusCode int
89+
inner error
90+
}
91+
92+
func (e APIError) Error() string {
93+
return fmt.Sprintf("%s: %s", e.Status, e.inner)
94+
}
95+
96+
func (e APIError) Unwrap() error {
97+
return e.inner
98+
}
99+
100+
func NewAPIError(status string, code int, inner error) error {
101+
return APIError{
102+
Status: status,
103+
StatusCode: code,
104+
inner: inner,
105+
}
106+
}
107+
108+
// HandleError makes an error from http.Response. It is the caller's
109+
// responsibility to close resp.Body.
88110
func HandleError(resp *http.Response) error {
89-
return fmt.Errorf("%s: %w", resp.Status, newErrorFromBody(resp))
111+
return NewAPIError(resp.Status, resp.StatusCode, newErrorFromBody(resp))
90112
}
91113

92114
func newErrorFromBody(resp *http.Response) error {

0 commit comments

Comments
 (0)