-
Notifications
You must be signed in to change notification settings - Fork 17
/
error.go
44 lines (35 loc) · 1000 Bytes
/
error.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
package werror
import (
"fmt"
)
type Error struct {
Err error // the underlying error
// The error message of the underlying error, or an empty string if
// the underlying error is nil.
Code string
Message string
}
// Wrap wraps err with a new error, whose error message is inherited from msgErr.
func Wrap(err, msgErr error) *Error {
return wrap(err, msgErr.Error())
}
// Wrapf wraps err with a new error, whose error message is calculated by formatting.
func Wrapf(err error, format string, a ...interface{}) *Error {
return wrap(err, fmt.Sprintf(format, a...))
}
func wrap(err error, msg string) *Error {
code := ""
if err != nil {
code = err.Error()
}
return &Error{
Err: err,
Code: code,
Message: msg,
}
}
// Error implements the error interface.
func (e *Error) Error() string { return e.Message }
// Unwrap follows the Unwrap convention introduced in Go 1.13,
// See https://blog.golang.org/go1.13-errors
func (e *Error) Unwrap() error { return e.Err }