-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_error.go
159 lines (132 loc) · 3.12 KB
/
http_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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package kit
import (
"encoding/json"
"fmt"
"reflect"
"strings"
"github.com/neoxelox/errors"
)
type HTTPError struct {
cause error
code string
status int
}
func NewHTTPError(code string, status int) HTTPError {
return HTTPError{
cause: nil,
code: code,
status: status,
}
}
func (self HTTPError) Cause(err error) *HTTPError {
return &HTTPError{
cause: err,
code: self.code,
status: self.status,
}
}
func (self HTTPError) Unwrap() error {
return self.cause
}
func (self HTTPError) Code() string {
return self.code
}
func (self HTTPError) Status() int {
return self.status
}
func (self *HTTPError) Redact() {
self.cause = nil
}
func (self HTTPError) Is(err error) bool {
if err == nil {
return false
}
switch other := err.(type) {
case HTTPError:
return self.code == other.code && self.status == other.status
case *HTTPError:
return self.code == other.code && self.status == other.status
}
return false
}
func (self HTTPError) Has(err error) bool {
if self.Is(err) {
return true
}
if self.cause != nil {
switch cause := self.cause.(type) {
case errors.Error:
return cause.Has(err)
case *errors.Error:
return cause.Has(err)
default:
return err == cause || err.Error() == cause.Error()
}
}
return false
}
func (self HTTPError) String() string {
if self.cause != nil {
return fmt.Sprintf("%s (%d): %s", self.code, self.status, self.cause.Error())
}
return fmt.Sprintf("%s (%d)", self.code, self.status)
}
func (self HTTPError) Error() string {
return self.String()
}
func (self HTTPError) MarshalText() ([]byte, error) {
if self.cause != nil {
// nolint:nilerr
return []byte(fmt.Sprintf("code=%s status=%d message=%s", self.code, self.status, self.cause.Error())), nil
}
return []byte(fmt.Sprintf("code=%s status=%d", self.code, self.status)), nil
}
type _HTTPError struct {
Code string `json:"code"`
Message string `json:"message,omitempty"`
}
func (self HTTPError) MarshalJSON() ([]byte, error) {
if self.cause != nil {
return json.Marshal(_HTTPError{
Code: self.code,
Message: self.cause.Error(),
})
}
return json.Marshal(_HTTPError{
Code: self.code,
})
}
func (self HTTPError) Format(format fmt.State, verb rune) {
switch verb {
case 's':
format.Write([]byte(self.String()))
case 'v':
report := fmt.Sprintf(
"HTTP error \x1b[1;91m%s\x1b[0m with status \x1b[1;91m%d\x1b[0m\n", self.code, self.status)
if self.cause != nil {
report = fmt.Sprintf(
"HTTP error \x1b[1;91m%s\x1b[0m with status \x1b[1;91m%d\x1b[0m caused by the following error:\n\n",
self.code, self.status)
switch cause := self.cause.(type) {
case errors.Error:
if format.Flag('+') {
report += cause.StringReport()
} else {
report += cause.StringReport(false)
}
case *errors.Error:
if format.Flag('+') {
report += cause.StringReport()
} else {
report += cause.StringReport(false)
}
default:
report += "\x1b[0;31m" + cause.Error() + "\x1b[0m (" +
strings.TrimPrefix(reflect.TypeOf(cause).String(), "*") + ")\n"
}
}
format.Write([]byte(report))
default:
format.Write([]byte(self.String()))
}
}