-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
54 lines (45 loc) · 1.17 KB
/
error.go
File metadata and controls
54 lines (45 loc) · 1.17 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
package failure
import (
"fmt"
)
type Errno int32
type Error struct {
inner error
frame *Frame
Msg string
Code Errno
}
func New(code Errno, msg string) *Error {
frames := traceback(0, 2)
var frame *Frame
if len(frames) > 1 {
frame = frames[1]
}
return &Error{Code: code, Msg: msg, frame: frame}
}
func (e *Error) Error() string {
if e.inner != nil {
if e.frame != nil {
return fmt.Sprintf("location: [%s] code: %d, msg: %s, err: %s", e.frame.Location, e.Code, e.Msg, e.inner)
}
return fmt.Sprintf("code: %d, msg: %s, err: %s", e.Code, e.Msg, e.inner)
} else {
if e.frame != nil {
return fmt.Sprintf("location: [%s] code: %d, msg: %s", e.frame.Location, e.Code, e.Msg)
}
return fmt.Sprintf("code: %d, msg: %s", e.Code, e.Msg)
}
}
func (e *Error) Unwrap() error { return e.inner }
func (e *Error) WithError(err error) *Error {
return &Error{inner: err, frame: e.Frame(), Msg: e.Msg, Code: e.Code}
}
func (e *Error) WithFrame(f *Frame) *Error {
return &Error{inner: e.inner, frame: f, Msg: e.Msg, Code: e.Code}
}
func (e *Error) Frame() *Frame {
if e.frame == nil {
return nil
}
return &Frame{Location: e.frame.Location, FuncName: e.frame.FuncName}
}