forked from dedis/cothority
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
74 lines (63 loc) · 1.56 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
package cothority
import (
"fmt"
"golang.org/x/xerrors"
)
// Error is a wrapper around an standard error that allows
// to print the stack trace from the call of the constructor.
type Error struct {
err error
msg string
frame xerrors.Frame
}
// ErrorOrNil returns the error if any with the stack trace
// beginning at the call of the function.
func ErrorOrNil(err error, msg string) error {
return ErrorOrNilSkip(err, msg, 1)
}
// ErrorOrNilSkip returns the error if any with the stack trace
// beginning at the call of the skip-nth caller.
func ErrorOrNilSkip(err error, msg string, skip int) error {
if err == nil {
return nil
}
return &Error{
err: err,
msg: msg,
frame: xerrors.Caller(skip),
}
}
// WrapError returns a wrapper of the error is it can be used
// for comparison.
func WrapError(err error) error {
return ErrorOrNilSkip(err, "", 2)
}
func (e *Error) Error() string {
if e.msg != "" {
return e.msg + ": " + fmt.Sprintf("%v", e.err)
}
return fmt.Sprintf("%v", e.err)
}
// Unwrap returns the next error in the chain.
func (e *Error) Unwrap() error {
return e.err
}
// Format prints the error to the formatter.
func (e *Error) Format(f fmt.State, c rune) {
xerrors.FormatError(e, f, c)
}
// FormatError prints the error to the printer. It prints
// the stack trace when the '+' is used in combination with
// 'v'.
func (e *Error) FormatError(p xerrors.Printer) error {
if e.msg != "" {
p.Printf("%s: %v", e.msg, e.err)
} else {
p.Printf("%v", e.err)
}
if p.Detail() {
e.frame.Format(p)
p.Printf("%+v", e.err)
}
return nil
}