-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherrors.go
65 lines (56 loc) · 953 Bytes
/
errors.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
package client
import (
"fmt"
"io"
"strings"
vocab "github.com/go-ap/activitypub"
)
type err struct {
err error
msg string
i vocab.IRI
}
func (e err) annotate(err error) err {
e.err = err
return e
}
func (e err) iri(i vocab.IRI) err {
e.i = i
return e
}
func errf(msg string, p ...interface{}) err {
return err{
msg: fmt.Sprintf(msg, p...),
}
}
// Error returns the formatted error
func (e err) Error() string {
s := strings.Builder{}
s.WriteString(e.msg)
if e.i != "" {
s.WriteString(": ")
s.WriteString(e.i.String())
}
if e.err != nil {
s.WriteString(": ")
s.WriteString(e.err.Error())
}
return s.String()
}
func (e err) Unwrap() error {
return e.err
}
func (e err) Format(s fmt.State, verb rune) {
switch verb {
case 's', 'v':
io.WriteString(s, e.msg)
switch {
case s.Flag('+'):
if e.err == nil {
return
}
io.WriteString(s, ": ")
io.WriteString(s, fmt.Sprintf("%+s", e.err))
}
}
}