-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
64 lines (53 loc) · 1.17 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
package zorros
import (
"fmt"
"golang.org/x/xerrors"
"strings"
)
func Trace(err error) error {
if _, ok := err.(xerrors.Formatter); ok {
return err
}
return zerror{err, xerrors.Caller(1)}
}
func Errorf(f string, a ...interface{}) error {
return zerror{fmt.Errorf(f, a...), xerrors.Caller(1)}
}
func Wrapf(err error, f string, a ...interface{}) error {
return zerror{wrapper{err, fmt.Sprintf(f, a...)}, xerrors.Caller(1)}
}
func New(message string) error {
return zerror{xerrors.New(message), xerrors.Caller(1)}
}
type zerror struct {
error
frame xerrors.Frame
}
func (e zerror) FormatError(p xerrors.Printer) error {
p.Print(e.error.Error() + " at ")
e.frame.Format(p)
return nil
}
func stringifyError(err error) (string, error) {
ep := &errorPrinter{details: true}
if f, ok := err.(xerrors.Formatter); ok {
err = f.FormatError(ep)
} else {
ep.Print(err.Error())
err = nil
}
return strings.Join(strings.Fields(ep.String()), " "), err
}
type wrapper struct {
error
message string
}
func (e wrapper) Error() string {
return e.message
}
func (e wrapper) Unwrap() error {
if w, ok := e.error.(xerrors.Wrapper); ok {
return w.Unwrap()
}
return e.error
}