forked from smartystreets/smartystreets-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_status_error.go
47 lines (40 loc) · 1.06 KB
/
http_status_error.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
package sdk
import (
"fmt"
"net/http"
)
func NewHTTPStatusError(statusCode int, content []byte) *HTTPStatusError {
return &HTTPStatusError{
statusCode: statusCode,
content: string(content),
}
}
// HTTPStatusError stands in for the error type but also provides convenience methods
// for accessing the status code and content of the request that caused the error.
// Instances of this type are returned by sdk.HTTPSender.Send(). When nil, the methods
// of this type behave as if called on a non-nil instance instantiated with http.StatusOK (200).
type HTTPStatusError struct {
statusCode int
content string
}
func (e *HTTPStatusError) Error() string {
if e == nil {
return statusText(http.StatusOK)
}
return statusText(e.statusCode) + "\n" + e.content
}
func statusText(code int) string {
return fmt.Sprintf("HTTP %d %s", code, http.StatusText(code))
}
func (e *HTTPStatusError) StatusCode() int {
if e == nil {
return http.StatusOK
}
return e.statusCode
}
func (e *HTTPStatusError) Content() string {
if e == nil {
return ""
}
return e.content
}