/
error.go
50 lines (43 loc) · 874 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
45
46
47
48
49
50
package errors
type GoError interface {
error
Cause() error
}
func New(message string) GoError {
return &GoErr{&message, nil}
}
func NewWithCause(message string, cause error) GoError {
return &GoErr{&message, cause}
}
func From(err error) GoError {
// If nil, just return back a nil so that
// it can easily be composed without having
// to check for nil first
if err == nil {
return nil
}
// Avoid a potentially wasteful allocation,
// if it's already the same error with the
// same error props
if gerr, ok := err.(GoError); ok {
return gerr
}
return &GoErr{nil, err}
}
type GoErr struct {
Msg *string
Inner error
}
func (e *GoErr) Error() string {
if e.Msg != nil {
return *e.Msg
}
cause := e.Cause()
if cause != nil && cause != e {
return cause.Error()
}
return "unknown error"
}
func (e *GoErr) Cause() error {
return e.Inner
}