-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
111 lines (89 loc) · 1.72 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
package aselerror
import (
"fmt"
)
type AselError interface {
GetCode() int
GetLevel() int8
GetMessage() string
Error() string
SetMessage(msg string) AselError
SetCause(err error) AselError
}
type aerr struct {
cause error
code int
msg string
// level Degree of disaster
level int8
}
type AselOption func(*aerr)
func WithCode(code int) AselOption {
return func(c *aerr) {
c.code = code
}
}
func WithCause(err error) AselOption {
return func(c *aerr) {
c.cause = err
}
}
func WithLevel(level int8) AselOption {
return func(c *aerr) {
c.level = level
}
}
func New(message string, opts ...AselOption) AselError {
var err aerr
if len(message) != 0 {
err.msg = message
} else {
err.msg = "unknow error"
}
for _, opt := range opts {
opt(&err)
}
return err
}
func Wrap(err error, message string) AselError {
if err == nil {
return nil
}
return aerr{
cause: err,
msg: message,
}
}
func (e aerr) GetCode() int {
return e.code
}
func (e aerr) GetLevel() int8 {
return e.level
}
func (e aerr) SetCause(err error) AselError {
e.cause = err
return e
}
// SetMessage is like update message
func (e aerr) SetMessage(msg string) AselError {
e.msg = msg
return e
}
// SetFMessage is like update message, with format string
func (e aerr) SetFMessage(format string, args ...interface{}) AselError {
e.msg = fmt.Sprintf(format, args...)
return e
}
func (e aerr) GetMessage() string {
return e.msg
}
func (e aerr) Unwrap() error {
return e.cause
}
func (e aerr) Error() string {
if e.cause != nil {
return fmt.Sprintf("aselerror cause: %s, message: %s, level: %d", e.cause, e.msg, e.level)
} else {
return fmt.Sprintf("aselerror cause: <nil>, message: %s, level: %d", e.msg, e.level)
}
}