@@ -37,35 +37,47 @@ func (e *Erf) Unwrap() error {
37
37
}
38
38
39
39
// Format is implementation of fmt.Formatter.
40
- // Format formats error message and lists all StackTrace's line by line with given format.
40
+ // Format lists error messages and appends StackTrace's for underlying Erf and all of wrapped Erf's,
41
+ // line by line with given format.
41
42
//
42
- // For '%s' (also '%v'):
43
- // %s just show first error message (default: padding char '\t', padding 0, indent 1)
44
- // %+s show error message with indent, append stack trace using format '%+s'
45
- // %#s use file name as file path for StackCaller
46
- // %-s use ' ' as padding char (padding 0, indent 2)
47
- // %0s show only first error even verb '+' was given
48
- // % s exact with %0s
49
- // %4s padding 4, default indent
50
- // %.3s default padding, indent 3
51
- // %4.3s padding 4, indent 3
52
- // %4.s padding 4, indent 0
43
+ // For '%v' (also '%s'):
44
+ // %v just show the first error message without padding and indent.
45
+ //
46
+ // For '%x' and '%X':
47
+ // %x list all error messages by using indent and show StackTrace of errors by using format '%+s'.
48
+ // % x list all error messages by using indent and show StackTrace of errors by using format '% s'.
49
+ // %#x list all error messages by using indent and show StackTrace of errors by using format '%#s'.
50
+ // % #x list all error messages by using indent and show StackTrace of errors by using format '% #s'.
51
+ // %X show the first error message by using indent and show the StackTrace of error by using format '%+s'.
52
+ // % X show the first error message by using indent and show the StackTrace of error by using format '% s'.
53
+ // %#X show the first error message by using indent and show the StackTrace of error by using format '%#s'.
54
+ // % #X show the first error message by using indent and show the StackTrace of error by using format '% #s'.
55
+ // %-x don't show any error messages, just show all of StackTrace of errors.
56
+ // %-X don't show the error message, just show the StackTrace of the first error.
57
+ // %4x same with '%x', padding 4, indent 1 by default.
58
+ // %.3x same with '%x', padding 0 by default, indent 3.
59
+ // %4.3x same with '%x', padding 4, indent 3.
60
+ // %4.x same with '%x', padding 4, indent 0.
61
+ // % 4x same with '% x', padding 4, indent 2 by default.
62
+ // % .3x same with '% x', padding 0 by default, indent 3.
63
+ // % 4.3x same with '% x', padding 4, indent 3.
64
+ // % 4.x same with '% x', padding 4, indent 0.
65
+ // %#4.3x same with '%#x', padding 4, indent 3.
66
+ // % #4.3x same with '% #x', padding 4, indent 3.
53
67
func (e * Erf ) Format (f fmt.State , verb rune ) {
54
68
buf := bytes .NewBuffer (make ([]byte , 0 , 4096 ))
55
69
switch verb {
56
70
case 's' , 'v' :
57
- if ! f .Flag ('+' ) {
58
- buf .WriteString (e .err .Error ())
59
- break
60
- }
71
+ buf .WriteString (e .err .Error ())
72
+ case 'x' , 'X' :
61
73
format := "%+"
62
- for _ , r := range []rune {'- ' , '#' } {
74
+ for _ , r := range []rune {' ' , '#' } {
63
75
if f .Flag (int (r )) {
64
76
format += string (r )
65
77
}
66
78
}
67
79
pad , wid , prec := byte ('\t' ), 0 , 1
68
- if f .Flag ('- ' ) {
80
+ if f .Flag (' ' ) {
69
81
pad = ' '
70
82
prec = 2
71
83
}
@@ -79,35 +91,33 @@ func (e *Erf) Format(f fmt.State, verb rune) {
79
91
format += "s"
80
92
padding := bytes .Repeat ([]byte {pad }, wid )
81
93
indent := bytes .Repeat ([]byte {pad }, prec )
82
- for _ , line := range strings .Split (e .err .Error (), "\n " ) {
83
- buf .Write (padding )
84
- buf .Write (indent )
85
- buf .WriteString (line )
86
- buf .WriteRune ('\n' )
87
- }
88
- buf .WriteString (fmt .Sprintf (format , e .StackTrace ()))
89
- buf .WriteRune ('\n' )
90
- if ! f .Flag ('0' ) && ! f .Flag (' ' ) {
91
- for err := e .Unwrap (); err != nil ; {
92
- if e2 , ok := err .(* Erf ); ok {
94
+ count := 0
95
+ for err := error (e ); err != nil ; {
96
+ if e2 , ok := err .(* Erf ); ok {
97
+ if count > 0 {
93
98
buf .WriteRune ('\n' )
99
+ }
100
+ count ++
101
+ if ! f .Flag ('-' ) {
94
102
for _ , line := range strings .Split (e2 .err .Error (), "\n " ) {
95
103
buf .Write (padding )
96
104
buf .Write (indent )
97
105
buf .WriteString (line )
98
106
buf .WriteRune ('\n' )
99
107
}
100
- buf .WriteString (fmt .Sprintf (format , e2 .StackTrace ()))
101
- buf .WriteRune ('\n' )
102
108
}
103
- if wErr , ok := err .( WrappedError ); ok {
104
- err = wErr . Unwrap ( )
105
- } else {
106
- err = nil
109
+ buf . WriteString ( fmt . Sprintf ( format , e2 . StackTrace ()))
110
+ buf . WriteRune ( '\n' )
111
+ if verb == 'X' {
112
+ break
107
113
}
108
114
}
115
+ if wErr , ok := err .(WrappedError ); ok {
116
+ err = wErr .Unwrap ()
117
+ } else {
118
+ err = nil
119
+ }
109
120
}
110
- buf .WriteRune ('\n' )
111
121
default :
112
122
return
113
123
}
0 commit comments