-
Notifications
You must be signed in to change notification settings - Fork 1
/
error.go
75 lines (64 loc) · 1.1 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
package cerrors
import (
"fmt"
)
func New(msg string) error {
return &cerror{
msg: msg,
frame: Caller(1),
}
}
func Newf(msg string, args ...interface{}) error {
return &cerror{
msg: fmt.Sprintf(msg, args...),
frame: Caller(1),
}
}
func Wrap(err error, msg string) error {
return &cerror{
msg: msg,
next: err,
frame: Caller(1),
}
}
func Wrapf(err error, msg string, args ...interface{}) error {
return &cerror{
msg: fmt.Sprintf(msg, args...),
next: err,
frame: Caller(1),
}
}
type cerror struct {
msg string
next error
frame Frame
}
func (c *cerror) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') || s.Flag('#') {
Format(c, s, s.Flag('#'))
return
}
fallthrough
case 's':
fmt.Fprint(s, c.Error())
default:
fmt.Fprintf(s, "%%!%s(cerror)", string(verb))
}
}
func (c *cerror) OwnMessage() string {
return c.msg
}
func (c *cerror) Frame() Frame {
return c.frame
}
func (c *cerror) Error() string {
if c.next != nil {
return c.msg + ": " + c.next.Error()
}
return c.msg
}
func (c *cerror) Unwrap() error {
return c.next
}