forked from sanity-io/client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
34 lines (28 loc) · 833 Bytes
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package sanity
import (
"fmt"
"net/http"
)
// RequestError is returned for API requests that fail with a non-successful HTTP status code.
type RequestError struct {
// Request is the attempted HTTP request that failed.
Request *http.Request
// Response is the HTTP response. Note that the body will no longer be valid.
Response *http.Response
// Body is the body of the response.
Body []byte
}
// Error implements the error interface.
func (e *RequestError) Error() string {
maxBody := 500
body := string(e.Body)
if len(body) > maxBody {
body = fmt.Sprintf("%s [... and %d more bytes]", body[0:maxBody], len(body)-maxBody)
}
msg := fmt.Sprintf("HTTP request [%s %s] failed with status %d",
e.Request.Method, e.Request.URL.String(), e.Response.StatusCode)
if body != "" {
msg += ": " + body
}
return msg
}