-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patherrors_api.go
134 lines (117 loc) · 2.43 KB
/
errors_api.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
package errors
import (
"fmt"
"net/http"
"golang.org/x/xerrors"
)
func newError(code string, msg string) *appError {
e := &appError{
code: code,
infoMessage: msg,
}
return e
}
func newBadRequest(code, msg string) *appError {
e := newError(code, msg)
e.status = http.StatusBadRequest
e.Info()
return e
}
func newUnauthorized(code, msg string) *appError {
e := newError(code, msg)
e.status = http.StatusUnauthorized
e.Info()
return e
}
func newForbidden(code, msg string) *appError {
e := newError(code, msg)
e.status = http.StatusForbidden
e.Info()
return e
}
func newConflict(code, msg string) *appError {
e := newError(code, msg)
e.status = http.StatusConflict
e.Info()
return e
}
func newNotFound(code, msg string) *appError {
e := newError(code, msg)
e.status = http.StatusNotFound
e.Info()
return e
}
func newInternalServerError(code, msg string) *appError {
e := newError(code, msg)
e.status = http.StatusInternalServerError
e.Crit()
return e
}
func (e appError) new(a string) *appError {
e.message = a
e.frame = xerrors.Caller(2)
return &e
}
func (e appError) New(msg ...string) AppError {
var m string
if len(msg) == 0 {
m = e.Code()
} else {
m = msg[0]
}
return e.new(m)
}
func (e appError) Errorf(format string, args ...interface{}) AppError {
return e.new(fmt.Sprintf(format, args...))
}
func (e appError) Wrap(err error, msg ...string) AppError {
var m string
if len(msg) == 0 {
m = e.Code()
} else {
m = msg[0]
}
ne := e.new(m)
ne.next = err
return ne
}
func (e appError) Wrapf(err error, format string, args ...interface{}) AppError {
ne := e.new(fmt.Sprintf(format, args...))
ne.next = err
return ne
}
// Messagef: ユーザー向けメッセージのフォーマットにパラメータを適用する
func (e appError) Messagef(args ...interface{}) AppError {
e.infoMessage = fmt.Sprintf(e.infoMessage, args...)
return &e
}
func (e *appError) Code() string {
if e.code != `` {
return e.code
}
next := AsAppError(e.next)
if next != nil {
return next.Code()
}
return `not_defined`
}
func (e *appError) Status() int {
if e.status != 0 {
return e.status
}
next := AsAppError(e.next)
if next != nil {
return next.Status()
}
return http.StatusInternalServerError
}
func (e *appError) InfoMessage() string {
if e.infoMessage != `` {
return e.infoMessage
}
next := AsAppError(e.next)
if next != nil {
return next.InfoMessage()
}
return "unknown info message"
}