-
Notifications
You must be signed in to change notification settings - Fork 53
/
tag.go
65 lines (55 loc) · 1.3 KB
/
tag.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
package zerror
type TagKind string
const (
// Empty error
None TagKind = ""
// Internal errors, This means that some invariants expected by the underlying system have been broken
Internal TagKind = "INTERNAL"
// The operation was cancelled, typically by the caller
Cancelled TagKind = "CANCELLED"
// The client specified an invalid argument
InvalidInput TagKind = "INVALID_INPUT"
// Some requested entity was not found
NotFound TagKind = "NOT_FOUND"
// The caller does not have permission to execute the specified operation
PermissionDenied TagKind = "PERMISSION_DENIED"
// The request does not have valid authentication credentials for the operation
Unauthorized TagKind = "UNAUTHORIZED"
)
func (t TagKind) Wrap(err error, text string) error {
return With(err, text, WrapTag(t))
}
type withTag struct {
wrapErr error
tag TagKind
}
func (e *withTag) Error() string {
return e.wrapErr.Error()
}
func WrapTag(tag TagKind) External {
return func(err error) error {
return &withTag{
wrapErr: err,
tag: tag,
}
}
}
func GetTag(err error) TagKind {
if err == nil {
return None
}
for err != nil {
if f, ok := err.(*withTag); ok {
return f.tag
}
if e, ok := err.(*Error); ok {
err = e.wrapErr
if err == nil {
err = e.err
}
} else {
break
}
}
return None
}