-
Notifications
You must be signed in to change notification settings - Fork 1
/
throw.go
84 lines (65 loc) · 1.65 KB
/
throw.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
package errors
import (
"strings"
)
// ApplicationError an appliction error with predifined error variable and detail message
type ApplicationError struct {
// Inner inner error
Inner error
// MsgList detail message
MsgList []string
traceid string
}
// Error implement error.Error
func (e *ApplicationError) Error() string {
return e.String()
}
// Error implement Stringer
func (e *ApplicationError) String() string {
if e == nil {
return ""
}
output := ""
if e.traceid != "" {
output = "[traceid-" + e.traceid + "] "
}
if e.Inner != nil && e.MsgList != nil {
output += e.Inner.Error() + ": " + strings.Join(e.MsgList, "\r\n")
} else if e.Inner != nil {
output += e.Inner.Error()
} else {
output += strings.Join(e.MsgList, " ")
}
return output
}
// Unwrap implement error.Unwrap
func (e *ApplicationError) Unwrap() error {
if e == nil {
return nil
}
if e.Inner != nil {
return e.Inner
}
return e
}
// Throw create an application error with prefinded error variable and message
// example
// errors.Throw(ErrInvalidParameter, "bloober_id is missing")
func Throw(inner error, msgList ...string) error {
return &ApplicationError{
Inner: inner,
MsgList: msgList,
}
}
// ThrowLog create an application error with prefinded error variable and message, log critical error in logging system for debugging.
// example
// errors.ThrowLog(err.Error(), ErrInvalidParameter, "bloober_id is missing")
func ThrowLog(raw string, inner error, msgList ...string) error {
callerName := getCallerName(1)
traceid := generateTraceID(callerName, raw)
return &ApplicationError{
traceid: traceid,
Inner: inner,
MsgList: msgList,
}
}