generated from WillAbides/goproject-tmpl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
109 lines (97 loc) · 2.73 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package internal
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/willabides/octo-go/components"
"github.com/willabides/octo-go/requests"
)
// NewResponseError returns a new *ResponseError
func NewResponseError(msg string, resp *http.Response) *ResponseError {
data, err := unmarshalErrorData(resp)
if err != nil {
data = nil
}
return &ResponseError{
resp: resp,
msg: msg,
data: data,
}
}
// ResponseError implements errors.ResponseError
type ResponseError struct {
resp *http.Response
msg string
data *components.ResponseErrorData
}
// HttpResponse implements errors.ResponseError
func (r *ResponseError) HttpResponse() *http.Response {
return r.resp
}
func (r *ResponseError) Error() string {
msg := r.msg
if r.data != nil && r.data.Message != "" {
msg += ": " + r.data.Message
}
return msg
}
// Data implements errors.ResponseError
func (r *ResponseError) Data() *components.ResponseErrorData {
return r.data
}
// IsClientError implements errors.ResponseError
func (r *ResponseError) IsClientError() bool {
return r.resp != nil && r.resp.StatusCode >= 400 && r.resp.StatusCode < 500
}
// IsServerError implements errors.ResponseError
func (r *ResponseError) IsServerError() bool {
return r.resp != nil && r.resp.StatusCode >= 500 && r.resp.StatusCode < 600
}
// NewRequestError returns a new RequestError
func newRequestError(msg string) error {
return &requests.RequestError{
Message: msg,
}
}
// ResponseErrorCheck checks for error responses
func ResponseErrorCheck(resp *http.Response, validStatuses []int) error {
code := resp.StatusCode
for _, wantStatus := range validStatuses {
if code == wantStatus {
return nil
}
}
switch {
case code >= 400 && code < 600:
return NewResponseError(fmt.Sprintf("client error %d", resp.StatusCode), resp)
case code >= 500 && code < 600:
return NewResponseError(fmt.Sprintf("server error %d", resp.StatusCode), resp)
}
if isRedirectOnly(validStatuses) && code < 300 {
return nil
}
msg := fmt.Sprintf("received unexpected http status code %d, expected codes are %v", code, validStatuses)
return NewResponseError(msg, resp)
}
func unmarshalErrorData(resp *http.Response) (*components.ResponseErrorData, error) {
if resp.Body == nil {
return nil, fmt.Errorf("no body")
}
var nextBody bytes.Buffer
bodyReader := io.TeeReader(resp.Body, &nextBody)
//nolint:errcheck // If there's an error draining the response body, there was probably already an error reported.
defer func() {
_, _ = ioutil.ReadAll(bodyReader)
_ = resp.Body.Close()
resp.Body = ioutil.NopCloser(&nextBody)
}()
var errorData components.ResponseErrorData
err := json.NewDecoder(bodyReader).Decode(&errorData)
if err != nil {
return nil, err
}
return &errorData, nil
}