This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errorcode.go
201 lines (162 loc) · 4.03 KB
/
errorcode.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
Copyright © 2020 Henry Huang <hhh@rutcode.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package errors
import (
"fmt"
"strings"
"github.com/google/uuid"
"github.com/iTrellis/common/json"
)
const (
defaultNamespace = "T:E"
)
// ErrorCode Error functions
type ErrorCode interface {
SimpleError
Code() uint64
Context() ErrorContext
Append(err ...error) ErrorCode
WithContext(k string, v interface{}) ErrorCode
}
// OptionFunc set params
type OptionFunc func(*ErrorOptions)
// ErrorOptions error parmas
type ErrorOptions struct {
namespace string
id string
code uint64
message string
errs []error
ctx map[string]interface{}
}
// NewSimpleError new simple errors by options
func (p *ErrorOptions) NewSimpleError() SimpleError {
return &Error{namespace: p.namespace, id: p.id, message: p.message}
}
// OptionID set id into options
func OptionID(id string) OptionFunc {
return func(p *ErrorOptions) {
p.id = id
}
}
// OptionCode set error code into options
func OptionCode(code uint64) OptionFunc {
return func(p *ErrorOptions) {
p.code = code
}
}
// OptionNamespace set error code into options
func OptionNamespace(ns string) OptionFunc {
return func(p *ErrorOptions) {
p.namespace = ns
}
}
// OptionMesssage set error message into options
func OptionMesssage(msg string) OptionFunc {
return func(p *ErrorOptions) {
p.message = msg
}
}
// OptionErrs append errs
func OptionErrs(errs ...error) OptionFunc {
return func(p *ErrorOptions) {
p.errs = append(p.errs, errs...)
}
}
// OptionContext set error context into options
func OptionContext(ctx map[string]interface{}) OptionFunc {
return func(p *ErrorOptions) {
p.ctx = ctx
}
}
type errorCode struct {
err SimpleError
code uint64
context map[string]interface{}
errors []error
}
// NewErrorCode get a new error code
func NewErrorCode(ofs ...OptionFunc) ErrorCode {
opts := &ErrorOptions{}
for _, o := range ofs {
o(opts)
}
if opts.namespace == "" {
opts.namespace = defaultNamespace
}
if opts.id == "" {
opts.id = uuid.New().String()
}
if opts.ctx == nil {
opts.ctx = make(map[string]interface{})
}
ec := &errorCode{
err: opts.NewSimpleError(),
context: opts.ctx,
}
ec.errors = append(ec.errors, opts.errs...)
return ec
}
func (p *errorCode) Append(errs ...error) ErrorCode {
p.errors = append(p.errors, errs...)
return p
}
func (p *errorCode) Code() uint64 {
return p.code
}
func (p *errorCode) Message() string {
return p.err.Message()
}
func (p *errorCode) Context() ErrorContext {
return p.context
}
func (p *errorCode) Error() string {
var errs = []string{p.err.Error()}
for _, err := range p.errors {
errs = append(errs, err.Error())
}
return strings.Join(errs, "; ")
}
func (p *errorCode) FullError() string {
return strings.Join(
append([]string{},
fmt.Sprintf("ID:%s#%s", genErrorCodeKey(p.Namespace(), p.Code()), p.ID()),
"Error:", p.Error(),
"Context:", p.Context().Error(),
), "\n")
}
func (p *errorCode) ID() string {
return p.err.ID()
}
func (p *errorCode) Namespace() string {
return p.err.Namespace()
}
func (p *errorCode) WithContext(key string, value interface{}) ErrorCode {
p.context[key] = value
return p
}
// ErrorContext map contexts
type ErrorContext map[string]interface{}
func (p ErrorContext) Error() string {
if p == nil {
return ""
}
if bs, e := json.Marshal(p); e == nil {
return string(bs)
}
return ""
}
func genErrorCodeKey(namespace string, code uint64) string {
return fmt.Sprintf("%s:%d", namespace, code)
}