-
-
Notifications
You must be signed in to change notification settings - Fork 161
/
errors.go
55 lines (45 loc) · 1.13 KB
/
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
package internal
import (
"fmt"
)
// Error represents an error that could be wrapping another error, it includes a code for determining what
// triggered the error.
type Error struct {
orig error
msg string
code ErrorCode
}
// ErrorCode defines supported error codes.
type ErrorCode uint
const (
ErrorCodeUnknown ErrorCode = iota
ErrorCodeNotFound
ErrorCodeInvalidArgument
)
// WrapErrorf returns a wrapped error.
func WrapErrorf(orig error, code ErrorCode, format string, a ...interface{}) error {
return &Error{
code: code,
orig: orig,
msg: fmt.Sprintf(format, a...),
}
}
// NewErrorf instantiates a new error.
func NewErrorf(code ErrorCode, format string, a ...interface{}) error {
return WrapErrorf(nil, code, format, a...)
}
// Error returns the message, when wrapping errors the wrapped error is returned.
func (e *Error) Error() string {
if e.orig != nil {
return fmt.Sprintf("%s: %v", e.msg, e.orig)
}
return e.msg
}
// Unwrap returns the wrapped error, if any.
func (e *Error) Unwrap() error {
return e.orig
}
// Code returns the code representing this error.
func (e *Error) Code() ErrorCode {
return e.code
}