- Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathjetstream_errors.go
102 lines (84 loc) · 2.06 KB
/
jetstream_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
package server
import (
"fmt"
)
typeerrOptsstruct {
errerror
}
// ErrorOption configures a NATS Error helper
typeErrorOptionfunc(*errOpts)
// Unless ensures that if err is a ApiErr that err will be returned rather than the one being created via the helper
funcUnless(errerror) ErrorOption {
returnfunc(opts*errOpts) {
opts.err=err
}
}
funcparseOpts(opts []ErrorOption) *errOpts {
eopts:=&errOpts{}
for_, opt:=rangeopts {
opt(eopts)
}
returneopts
}
typeErrorIdentifieruint16
// IsNatsErr determines if an error matches ID, if multiple IDs are given if the error matches any of these the function will be true
funcIsNatsErr(errerror, ids...ErrorIdentifier) bool {
iferr==nil {
returnfalse
}
ce, ok:=err.(*ApiError)
if!ok||ce==nil {
returnfalse
}
for_, id:=rangeids {
ae, ok:=ApiErrors[id]
if!ok||ae==nil {
continue
}
ifce.ErrCode==ae.ErrCode {
returntrue
}
}
returnfalse
}
// ApiError is included in all responses if there was an error.
typeApiErrorstruct {
Codeint`json:"code"`
ErrCodeuint16`json:"err_code,omitempty"`
Descriptionstring`json:"description,omitempty"`
}
// ErrorsData is the source data for generated errors as found in errors.json
typeErrorsDatastruct {
Constantstring`json:"constant"`
Codeint`json:"code"`
ErrCodeuint16`json:"error_code"`
Descriptionstring`json:"description"`
Commentstring`json:"comment"`
Helpstring`json:"help"`
URLstring`json:"url"`
Deprecatesstring`json:"deprecates"`
}
func (e*ApiError) Error() string {
returnfmt.Sprintf("%s (%d)", e.Description, e.ErrCode)
}
func (e*ApiError) toReplacerArgs(replacements []any) []string {
var (
ra []string
keystring
)
fori, replacement:=rangereplacements {
ifi%2==0 {
key=replacement.(string)
continue
}
switchv:=replacement.(type) {
casestring:
ra=append(ra, key, v)
caseerror:
ra=append(ra, key, v.Error())
default:
ra=append(ra, key, fmt.Sprintf("%v", v))
}
}
returnra
}