-
Notifications
You must be signed in to change notification settings - Fork 251
/
error.go
163 lines (141 loc) · 3.7 KB
/
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
160
161
162
163
package xerr
import (
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
)
type Status int
const (
StatusBadRequest = http.StatusBadRequest
StatusInternalServerError = http.StatusInternalServerError
StatusForbidden = http.StatusForbidden
StatusNotFound = http.StatusNotFound
)
type ErrorType uint
const (
NoType ErrorType = iota
BadParam
NoRecord
Forbidden
DB
Email
)
type customError struct {
errorType ErrorType
cause error
httpStatus int
// msg used to return to the response
msg string
errMsg string
}
func (errorType ErrorType) New(errMsg string, args ...interface{}) *customError {
err := &customError{errorType: errorType, cause: errors.Errorf(errMsg, args...), httpStatus: -1}
return err
}
func (errorType ErrorType) Wrapf(err error, errMsg string, args ...interface{}) *customError {
return &customError{errorType: errorType, cause: errors.Wrapf(err, errMsg, args...), httpStatus: -1}
}
func (errorType ErrorType) Wrap(err error) *customError {
return &customError{errorType: errorType, cause: errors.Wrap(err, ""), httpStatus: -1}
}
func (ce *customError) Error() string {
if ce.errMsg != "" {
if ce.cause != nil {
return ce.errMsg + " : " + ce.cause.Error()
} else {
return ce.errMsg
}
}
if ce.cause != nil {
return ce.cause.Error()
}
return ce.msg
}
func (ce *customError) Cause() error {
return ce.cause
}
func (ce *customError) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", ce.Cause())
_, _ = io.WriteString(s, ce.msg)
return
}
fallthrough
case 's', 'q':
_, _ = io.WriteString(s, ce.Error())
}
}
func WithStatus(err error, status int) *customError {
//nolint:errorlint
ee, ok := err.(*customError)
if ok {
return &customError{errorType: ee.errorType, cause: ee, httpStatus: status}
}
return &customError{errorType: NoType, cause: err, httpStatus: status}
}
func WithMsg(err error, msg string) *customError {
//nolint:errorlint
ee, ok := err.(*customError)
if ok {
return &customError{errorType: ee.errorType, cause: ee, httpStatus: -1, msg: msg}
}
return &customError{errorType: NoType, cause: err, httpStatus: -1, msg: msg}
}
func WithErrMsgf(err error, errMsg string, args ...interface{}) *customError {
//nolint:errorlint
ee, ok := err.(*customError)
if ok {
return &customError{errorType: ee.errorType, cause: err, httpStatus: -1, errMsg: fmt.Sprintf(errMsg, args...)}
}
return &customError{errorType: NoType, cause: err, httpStatus: -1, errMsg: fmt.Sprintf(errMsg, args...)}
}
func (ce *customError) WithErrMsgf(errMsg string, args ...interface{}) *customError {
return &customError{errorType: ce.errorType, cause: ce, httpStatus: -1, errMsg: fmt.Sprintf(errMsg, args...)}
}
func (ce *customError) WithStatus(status int) *customError {
return &customError{errorType: ce.errorType, cause: ce, httpStatus: status}
}
func (ce *customError) WithMsg(msg string) *customError {
return &customError{errorType: ce.errorType, cause: ce, httpStatus: ce.httpStatus, msg: msg}
}
// GetType returns the error type
func GetType(err error) ErrorType {
//nolint:errorlint
if ee, ok := err.(*customError); ok {
return ee.errorType
}
return NoType
}
func GetHTTPStatus(err error) int {
for err != nil {
//nolint:errorlint
if e, ok := err.(*customError); ok {
if e.httpStatus != -1 {
return e.httpStatus
} else {
err = e.cause
}
} else {
break
}
}
return StatusInternalServerError
}
func GetMessage(err error) string {
for err != nil {
//nolint:errorlint
if e, ok := err.(*customError); ok {
if e.msg != "" {
return e.msg
} else {
err = e.cause
}
} else {
break
}
}
return http.StatusText(http.StatusInternalServerError)
}