-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_with_key.go
More file actions
43 lines (38 loc) · 935 Bytes
/
error_with_key.go
File metadata and controls
43 lines (38 loc) · 935 Bytes
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
package errorz
// ErrorWithKey is a struct compatible error to be extended or used in other errors
type ErrorWithKey struct {
Key string
HTTPCode int
Code string
Cause error
}
// NewErrorWithKey creates a new ErrorWithKey
func NewErrorWithKey(code, key string, httpCode int, cause error) *ErrorWithKey {
return &ErrorWithKey{
Key: key,
HTTPCode: httpCode,
Code: code,
Cause: cause,
}
}
// Error ...
func (e ErrorWithKey) Error() string {
s := e.Key
if e.Cause != nil {
s += ": " + e.Cause.Error()
}
return s
}
// Is is used by the standard "errors" package to identify an error as ErrorWithKey
func (e *ErrorWithKey) Is(err error) bool {
_, ok := err.(*ErrorWithKey)
return ok
}
// As is used by the standard "errors" package to identify an error as ErrorWithKey
func (e *ErrorWithKey) As(err interface{}) bool {
err2, ok := err.(*ErrorWithKey)
if ok {
*err2 = *e
}
return ok
}