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
|
type DetailError struct {
msg, detail string
err error
}
func (e *DetailError) Unwrap() error { return e.err }
func (e *DetailError) Error() string {
if e.err == nil {
return e.msg
}
return e.msg + ": " + e.err.Error()
}
func (e *DetailError) Format(s fmt.State, c rune) {
if s.Flag('#') && c == 'v' {
type nomethod DetailError
fmt.Fprintf(s, "%#v", (*nomethod)(e))
return
}
if !s.Flag('+') || c != 'v' {
fmt.Fprintf(s, spec(s, c), e.Error())
return
}
fmt.Fprintln(s, e.msg)
if e.detail != "" {
io.WriteString(s, "\t")
fmt.Fprintln(s, e.detail)
}
if e.err != nil {
if ferr, ok := e.err.(fmt.Formatter); ok {
ferr.Format(s, c)
} else {
fmt.Fprintf(s, spec(s, c), e.err)
io.WriteString(s, "\n")
}
}
}
func spec(s fmt.State, c rune) string {
buf := []byte{'%'}
for _, f := range []int{'+', '-', '#', ' ', '0'} {
if s.Flag(f) {
buf = append(buf, byte(f))
}
}
if w, ok := s.Width(); ok {
buf = strconv.AppendInt(buf, int64(w), 10)
}
if p, ok := s.Precision(); ok {
buf = append(buf, '.')
buf = strconv.AppendInt(buf, int64(p), 10)
}
buf = append(buf, byte(c))
return string(buf)
}
|