/
apperrors.go
66 lines (52 loc) · 1.44 KB
/
apperrors.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 apperrors
import "fmt"
const (
CodeInternal = 1
CodeNotFound = 2
CodeAlreadyExists = 3
CodeWrongInput = 4
CodeUpstreamServerCallFailed = 5
)
type AppError interface {
Append(string, ...interface{}) AppError
Code() int
Error() string
}
type appError struct {
code int
message string
}
func errorf(code int, format string, a ...interface{}) AppError {
return appError{code: code, message: fmt.Sprintf(format, a...)}
}
func Internal(format string, a ...interface{}) AppError {
return errorf(CodeInternal, format, a...)
}
func NotFound(format string, a ...interface{}) AppError {
return errorf(CodeNotFound, format, a...)
}
func AlreadyExists(format string, a ...interface{}) AppError {
return errorf(CodeAlreadyExists, format, a...)
}
func WrongInput(format string, a ...interface{}) AppError {
return errorf(CodeWrongInput, format, a...)
}
func UpstreamServerCallFailed(format string, a ...interface{}) AppError {
return errorf(CodeUpstreamServerCallFailed, format, a...)
}
func (ae appError) Append(additionalFormat string, a ...interface{}) AppError {
format := additionalFormat + ", " + ae.message
return errorf(ae.code, format, a...)
}
func (ae appError) Code() int {
return ae.code
}
func (ae appError) Error() string {
return ae.message
}
func AppendError(wrapped AppError, new AppError) AppError {
if wrapped == nil {
return new
}
return wrapped.Append("", new)
}