-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.go
More file actions
117 lines (103 loc) · 2.15 KB
/
errors.go
File metadata and controls
117 lines (103 loc) · 2.15 KB
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
/*
callError usage:
type callError interface {
IsNotFound() bool
IsBadRequest() bool
Code() string
Error() string
Data() map[string]interface{}
}
perr, ok := err.(callError)
if ok {
if perr.IsNotFound() {
status = http.StatusNotFound
}
}
*/
package procapi
import (
"fmt"
)
// ErrorID
type errorID uint8
const (
errUnknown = errorID(iota)
errNotFound
errArgsMissed
errBadRequest
errNotNilDB
errNilDB
errNilMarshaller
errNotSingleRV
errArgCast
errInternal
)
type callError struct {
code errorID
data map[string]interface{}
}
// IsNotFound checks error code
func (ce callError) IsNotFound() bool {
return ce.code == errNotFound
}
// IsBadRequest checks error code
func (ce callError) IsBadRequest() bool {
return ce.code == errBadRequest || ce.code == errArgsMissed
}
// Code returns error code
func (ce callError) Code() string {
// not using stringer cause it has 114Mb distro
names := [...]string{
"Unknown",
"MethodNotFound",
"RequiredArgsMissed",
"BadRequest",
"NotNilDB",
"NilDB",
"NilMarshaller",
"NotSingleRV",
"ArgCast",
"Internal",
}
if ce.code > errInternal {
return names[errUnknown]
}
return names[ce.code]
}
// Message returns error description
func (ce callError) Message() string {
// not using stringer cause it has 114Mb distro
names := [...]string{
"Unknown",
"Method not found",
"Required arg(s) missed",
"BadRequest",
"DB opened already",
"DB must be not nil",
"Type marshaller must be not nil",
"Single row must be returned",
"Cannot parse arg",
"Internal",
}
if ce.code > errInternal {
return names[errUnknown]
}
return names[ce.code]
}
// Data returns error data map
func (ce callError) Data() map[string]interface{} {
return ce.data
}
// Error returns error message with data
func (ce callError) Error() string {
return fmt.Sprintf("%s (%s)", ce.Message(), ce.data)
}
// addContext is an internal method for setting error data
// err := (&callError{code: NotFound}).addContext("name", method)
func (ce *callError) addContext(name string, value interface{}) *callError {
if ce.data == nil {
ce.data = map[string]interface{}{}
}
ce.data[name] = value
return ce
}