-
Notifications
You must be signed in to change notification settings - Fork 1
/
richerror.go
66 lines (55 loc) · 1.64 KB
/
richerror.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
/*
© 2021–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package errorglue
import (
"fmt"
"io"
"strconv"
)
// RichError is an error chain that behaves like fmt.Formatter.
// this allows for custom print-outs using %+v and %-v
// RichError has publics Error() Unwrap() Format()
type RichError struct {
ErrorChain
}
var _ error = &RichError{} // RichError behaves like an error
var _ Wrapper = &RichError{} // RichError is an error chain
var _ fmt.Formatter = &RichError{} // RichError has features for fmt.Printf
func newRichError(err error) *RichError {
return &RichError{*newErrorChain(err)}
}
// Format provides the fmt.Formatter function
func (e *RichError) Format(s fmt.State, verb rune) {
if IsValueVerb(verb) {
if format := PrintfFormat(s); format != DefaultFormat {
ChainString(e, format)
}
io.WriteString(s, e.Error())
} else if IsStringVerb(verb) {
io.WriteString(s, e.Error())
} else if IsQuoteVerb(verb) {
io.WriteString(s, strconv.Quote(e.Error()))
}
}
// IsPlusFlag determines if fmt.State has the '+' flag
func IsPlusFlag(s fmt.State) bool {
return s.Flag('+')
}
// IsMinusFlag determines if fmt.State has the '-' flag
func IsMinusFlag(s fmt.State) bool {
return s.Flag('-')
}
// IsValueVerb determines if the rune corresponds to the %v value verb
func IsValueVerb(r rune) bool {
return r == 'v'
}
// IsStringVerb determines if the rune corresponds to the %s string verb
func IsStringVerb(r rune) bool {
return r == 's'
}
// IsQuoteVerb determines if the rune corresponds to the %q quote verb
func IsQuoteVerb(r rune) bool {
return r == 'q'
}