-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrors.go
More file actions
74 lines (64 loc) · 1.1 KB
/
errors.go
File metadata and controls
74 lines (64 loc) · 1.1 KB
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 client
import (
"fmt"
"io"
"strings"
vocab "github.com/go-ap/activitypub"
)
type cerr struct {
err error
msg string
i vocab.IRI
st int
}
func (e cerr) annotate(err error) cerr {
e.err = err
return e
}
func (e cerr) iri(i vocab.IRI) cerr {
e.i = i
return e
}
func (e cerr) status(st int) cerr {
e.st = st
return e
}
func errf(msg string, p ...interface{}) cerr {
return cerr{
msg: fmt.Sprintf(msg, p...),
}
}
// Error returns the formatted error
func (e cerr) Error() string {
s := strings.Builder{}
s.WriteString(e.msg)
if e.i != "" {
s.WriteString(" (loading: \"")
s.WriteString(string(e.i))
s.WriteString("\")")
}
if e.err != nil {
if msg := e.err.Error(); len(msg) > 0 {
s.WriteString(": ")
s.WriteString(msg)
}
}
return s.String()
}
func (e cerr) Unwrap() error {
return e.err
}
func (e cerr) 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))
}
}
}