-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_error.go
110 lines (93 loc) · 2.26 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
package errcode
import (
"fmt"
"net/http"
"strconv"
"strings"
)
var errCodes = map[int]*Error{}
// Error error
type Error struct {
code int
msg string
details []string
}
// NewError create a new error message
func NewError(code int, msg string) *Error {
if v, ok := errCodes[code]; ok {
panic(fmt.Sprintf("http error code = %d already exists, please replace with a new error code, old msg = %s", code, v.Msg()))
}
e := &Error{code: code, msg: msg}
errCodes[code] = e
return e
}
// Err covert to standard error
func (e *Error) Err() error {
if len(e.details) == 0 {
return fmt.Errorf("code = %d, msg = %s", e.code, e.msg)
}
return fmt.Errorf("code = %d, msg = %s, details = %v", e.code, e.msg, e.details)
}
// Code get error code
func (e *Error) Code() int {
return e.code
}
// Msg get error code message
func (e *Error) Msg() string {
return e.msg
}
// Details get error code details
func (e *Error) Details() []string {
return e.details
}
// WithDetails add error details
func (e *Error) WithDetails(details ...string) *Error {
newError := *e
newError.details = []string{}
newError.details = append(newError.details, details...)
return &newError
}
// ToHTTPCode convert to http error code
func (e *Error) ToHTTPCode() int {
switch e.Code() {
case Success.Code():
return http.StatusOK
case InternalServerError.Code():
return http.StatusInternalServerError
case InvalidParams.Code():
return http.StatusBadRequest
case Unauthorized.Code():
return http.StatusUnauthorized
case TooManyRequests.Code(), LimitExceed.Code():
return http.StatusTooManyRequests
case Forbidden.Code():
return http.StatusForbidden
case NotFound.Code():
return http.StatusNotFound
case AlreadyExists.Code():
return http.StatusConflict
case Timeout.Code():
return http.StatusRequestTimeout
}
return e.Code()
}
// ParseError parsing out error codes from error messages
func ParseError(err error) *Error {
if err == nil {
return Success
}
unknownError := &Error{
code: -1,
msg: "unknown error",
}
splits := strings.Split(err.Error(), ", msg = ")
codeStr := strings.ReplaceAll(splits[0], "code = ", "")
code, er := strconv.Atoi(codeStr)
if er != nil {
return unknownError
}
if e, ok := errCodes[code]; ok {
return e
}
return unknownError
}