-
Notifications
You must be signed in to change notification settings - Fork 16
/
http.go
55 lines (50 loc) · 1.53 KB
/
http.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package aptos
import (
"fmt"
"io"
"net/http"
"net/url"
)
// HttpErrSummaryLength is the maximum length of the body to include in the error message
const HttpErrSummaryLength = 1000
// HttpError is an error type that represents an error from a http request
type HttpError struct {
Status string // HTTP status e.g. "200 OK"
StatusCode int // HTTP status code e.g. 200
Header http.Header // HTTP headers
Method string // HTTP method e.g. "GET"
RequestUrl url.URL // URL of the request
Body []byte // Body of the response
}
// NewHttpError creates a new HttpError from a http.Response
func NewHttpError(response *http.Response) *HttpError {
body, _ := io.ReadAll(response.Body)
_ = response.Body.Close()
return &HttpError{
Status: response.Status,
StatusCode: response.StatusCode,
Header: response.Header,
Body: body,
Method: response.Request.Method,
RequestUrl: *response.Request.URL,
}
}
// Error returns a string representation of the HttpError
//
// Implements:
// - [Error]
func (he *HttpError) Error() string {
if len(he.Body) < HttpErrSummaryLength {
return fmt.Sprintf("HttpError %s %#v -> %#v %#v",
he.Method, he.RequestUrl.String(), he.Status,
string(he.Body),
)
} else {
// Trim if the error is too long
return fmt.Sprintf("HttpError %s %#v -> %#v %s %#v...[+%d]",
he.Method, he.RequestUrl.String(), he.Status,
he.Header.Get("Content-Type"),
string(he.Body)[:HttpErrSummaryLength-10], len(he.Body)-(HttpErrSummaryLength-10),
)
}
}