forked from pasztorpisti/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnano_error.go
59 lines (48 loc) · 1007 Bytes
/
nano_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
package util
import "fmt"
type NanoError interface {
error
Code() string
}
var ErrMsgChainSeparator = " :: "
func ErrCode(cause error, code, msg string) NanoError {
e := &nanoError{
cause: cause,
code: code,
msg: msg,
}
if e.code == "" && cause != nil {
e.code = GetErrCode(cause)
}
return e
}
func ErrCodef(cause error, code, format string, a ...interface{}) NanoError {
return ErrCode(cause, code, fmt.Sprintf(format, a))
}
func Err(cause error, msg string) NanoError {
return ErrCode(cause, "", msg)
}
func Errf(cause error, format string, a ...interface{}) NanoError {
return ErrCodef(cause, "", format, a)
}
func GetErrCode(err error) string {
if e, ok := err.(NanoError); ok {
return e.Code()
}
return ""
}
type nanoError struct {
cause error
code string
msg string
}
func (p *nanoError) Error() string {
msg := p.msg
if p.cause != nil {
msg += ErrMsgChainSeparator + p.cause.Error()
}
return msg
}
func (p *nanoError) Code() string {
return p.code
}