forked from OneOfOne/oerrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
84 lines (67 loc) · 1.62 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package oerrs
import (
"encoding/json"
"fmt"
"golang.org/x/xerrors"
)
func New(s string) error {
err := String(s)
if AlwaysWithCaller {
return err.WithFrame(2)
}
return err
}
// String is a plain string error, it can be converted to string and compared
type String string
func (e String) Error() string { return string(e) }
func (e String) WithFrame(skip int) error {
return Wrap(e, skip+1)
}
func (e String) Is(o error) bool {
return e.Error() == string(e)
}
func Wrap(err error, skip int) error {
return &wrapped{err, Caller(skip + 1)}
}
// wrapped is a trivial implementation of error with frame information
type wrapped struct {
err error
fr *Frame
}
func (e *wrapped) Error() string {
if WrappedErrorTextIncludesFrameInfo {
return fmt.Sprintf("%s: %v", e.fr.String(), e.err)
}
return e.err.Error()
}
func (e *wrapped) Unwrap() error {
return e.err
}
func (e *wrapped) Format(s fmt.State, v rune) { xerrors.FormatError(e, s, v) }
func (e *wrapped) FormatError(p Printer) (next error) {
p.Print(e.err)
e.fr.Format(p)
return nil
}
func (e *wrapped) Is(target error) bool {
return Is(e.err, target)
}
func (e *wrapped) Frame() *Frame { return e.fr }
type JSONError struct {
wrapped `json:"-"`
Err string `json:"error,omitempty"`
Func string `json:"func,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
}
func (e *JSONError) MarshalJSON() ([]byte, error) {
type jsonError_ JSONError
if e.err == nil && e.Err != "" {
return json.Marshal((*jsonError_)(e))
}
je := &jsonError_{
Err: e.err.Error(),
}
je.Func, je.File, je.Line = e.fr.Location()
return json.Marshal(je)
}