forked from gookit/goutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
154 lines (128 loc) · 2.45 KB
/
errors.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package errorx
import (
"fmt"
"strconv"
"strings"
)
// ErrorCoder interface
type ErrorCoder interface {
error
Code() int
}
// ErrorR useful for web service replay/response.
// code == 0 is successful. otherwise, is failed.
type ErrorR interface {
ErrorCoder
fmt.Stringer
IsSuc() bool
IsFail() bool
}
// error reply struct
type errorR struct {
code int
msg string
}
// NewR code with error response
func NewR(code int, msg string) ErrorR {
return &errorR{code: code, msg: msg}
}
// Fail code with error response
func Fail(code int, msg string) ErrorR {
return &errorR{code: code, msg: msg}
}
// Failf code with error response
func Failf(code int, tpl string, v ...any) ErrorR {
return &errorR{code: code, msg: fmt.Sprintf(tpl, v...)}
}
// Suc success response reply
func Suc(msg string) ErrorR {
return &errorR{code: 0, msg: msg}
}
// IsSuc code value check
func (e *errorR) IsSuc() bool {
return e.code == 0
}
// IsFail code value check
func (e *errorR) IsFail() bool {
return e.code != 0
}
// Code value
func (e *errorR) Code() int {
return e.code
}
// Error string
func (e *errorR) Error() string {
return e.msg
}
// String get
func (e *errorR) String() string {
return e.msg + "(code: " + strconv.FormatInt(int64(e.code), 10) + ")"
}
// GoString get.
func (e *errorR) GoString() string {
return e.String()
}
// ErrorM multi error map
type ErrorM map[string]error
// ErrMap alias of ErrorM
type ErrMap = ErrorM
// Error string
func (e ErrorM) Error() string {
var sb strings.Builder
for name, err := range e {
sb.WriteString(name)
sb.WriteByte(':')
sb.WriteString(err.Error())
sb.WriteByte('\n')
}
return sb.String()
}
// ErrorOrNil error
func (e ErrorM) ErrorOrNil() error {
if len(e) == 0 {
return nil
}
return e
}
// IsEmpty error
func (e ErrorM) IsEmpty() bool {
return len(e) == 0
}
// One error
func (e ErrorM) One() error {
for _, err := range e {
return err
}
return nil
}
// Errors multi error list
type Errors []error
// ErrList alias for Errors
type ErrList = Errors
// Error string
func (es Errors) Error() string {
var sb strings.Builder
for _, err := range es {
sb.WriteString(err.Error())
sb.WriteByte('\n')
}
return sb.String()
}
// ErrorOrNil error
func (es Errors) ErrorOrNil() error {
if len(es) == 0 {
return nil
}
return es
}
// IsEmpty error
func (es Errors) IsEmpty() bool {
return len(es) == 0
}
// First error
func (es Errors) First() error {
if len(es) > 0 {
return es[0]
}
return nil
}