-
Notifications
You must be signed in to change notification settings - Fork 2
/
responseerrors.go
133 lines (110 loc) · 2.78 KB
/
responseerrors.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package lib
import (
"errors"
"fmt"
"io"
"math"
"net/http"
"strings"
)
type ResponseError struct {
StatusCode int
err error
}
func (r ResponseError) Error() string {
return fmt.Sprintf("status code: %v - %v", r.StatusCode, r.err.Error())
}
func ResponseErrors(res *http.Response, errorFunc ...func(res *http.Response) error) error {
for _, errFunc := range errorFunc {
if err := errFunc(res); err != nil {
return err
}
}
return nil
}
func IsNonOkStatus(res *http.Response) bool {
return !IsOkStatus(res)
}
func IsOkStatus(res *http.Response) bool {
if res.StatusCode < 300 && res.StatusCode > 199 {
return true
}
return false
}
func NonOkError(res *http.Response) error {
if IsNonOkStatus(res) {
return errorFromBodyDefault(res)
}
return nil
}
func NonOkErrorCustom(callbacks ...func(error) error) func(res *http.Response) error {
return func(res *http.Response) error {
if IsNonOkStatus(res) {
return errorFromBody(res, callbacks)
}
return nil
}
}
func NotStatus(status int) func(res *http.Response) error {
return func(res *http.Response) error {
if res.StatusCode != status {
return errorFromBodyDefault(res)
}
return nil
}
}
func IsStatus(status int) func(res *http.Response) error {
return func(res *http.Response) error {
if res.StatusCode == status {
return errorFromBodyDefault(res)
}
return nil
}
}
func IsHTML(res *http.Response) bool {
return strings.Contains(res.Header.Get("Content-type"), "text/html")
}
func IsJSON(res *http.Response) bool {
return strings.Contains(res.Header.Get("Content-type"), "application/json")
}
func NonJSONError(res *http.Response) error {
if !IsJSON(res) {
if res.StatusCode == 204 {
return nil
}
return errorFromBodyDefault(res)
}
return nil
}
func CloseBody(res *http.Response) {
if res != nil && res.Body != nil {
res.Body.Close()
}
}
func errorFromBodyDefault(res *http.Response) error {
var callbacks []func(error) error
return errorFromBody(res, callbacks)
}
func errorFromBody(res *http.Response, callbacks []func(error) error) error {
if IsHTML(res) {
return ResponseError{StatusCode: res.StatusCode, err: fmt.Errorf(http.StatusText(res.StatusCode))}
}
var body []byte
var err error
if res.ContentLength == -1 {
body = make([]byte, 512)
} else {
body = make([]byte, int(math.Min(float64(res.ContentLength), float64(512))))
}
_, err = io.ReadFull(res.Body, body)
defer CloseBody(res)
if err == nil || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
err = ResponseError{StatusCode: res.StatusCode, err: fmt.Errorf(strings.ReplaceAll(string(body), "\n", " "))}
} else {
err = ResponseError{StatusCode: res.StatusCode, err: fmt.Errorf(http.StatusText(res.StatusCode))}
}
for _, callback := range callbacks {
err = callback(err)
}
return err
}