-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_error.go
66 lines (54 loc) · 986 Bytes
/
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
package httpe
import (
"encoding/json"
"fmt"
"net/http"
)
type StatusError interface {
json.Marshaler
StatusCode() int
GetError() error
Unwrap() error
error
}
type statusError struct {
Err error
Status int
}
func (se statusError) GetError() error {
return se.Err
}
func (se statusError) MarshalJSON() ([]byte, error) {
return json.Marshal(se.Err.Error())
}
func (e statusError) Unwrap() error {
return e.Err
}
func (e statusError) StatusCode() int {
if e.Status == 0 {
return http.StatusInternalServerError
}
return e.Status
}
func (e statusError) Error() string {
return e.Err.Error()
}
type Error struct {
baseError error
httpError StatusError
}
func (e Error) Error() string {
return e.baseError.Error()
}
func (e Error) Unwrap() error {
return fmt.Errorf("%w %w", e.baseError, e.httpError)
}
func NewError(base error, status int) Error {
return Error{
baseError: base,
httpError: statusError{
Err: base,
Status: status,
},
}
}