Skip to content
This repository was archived by the owner on Nov 21, 2023. It is now read-only.

Commit 372a29b

Browse files
Merge pull request #7 from goinsane/develop
v1.1.0
2 parents 02985fd + f69bb29 commit 372a29b

File tree

10 files changed

+166
-255
lines changed

10 files changed

+166
-255
lines changed

erf.go

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,47 @@ func (e *Erf) Unwrap() error {
3737
}
3838

3939
// 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.
4142
//
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.
5367
func (e *Erf) Format(f fmt.State, verb rune) {
5468
buf := bytes.NewBuffer(make([]byte, 0, 4096))
5569
switch verb {
5670
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':
6173
format := "%+"
62-
for _, r := range []rune{'-', '#'} {
74+
for _, r := range []rune{' ', '#'} {
6375
if f.Flag(int(r)) {
6476
format += string(r)
6577
}
6678
}
6779
pad, wid, prec := byte('\t'), 0, 1
68-
if f.Flag('-') {
80+
if f.Flag(' ') {
6981
pad = ' '
7082
prec = 2
7183
}
@@ -79,35 +91,33 @@ func (e *Erf) Format(f fmt.State, verb rune) {
7991
format += "s"
8092
padding := bytes.Repeat([]byte{pad}, wid)
8193
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 {
9398
buf.WriteRune('\n')
99+
}
100+
count++
101+
if !f.Flag('-') {
94102
for _, line := range strings.Split(e2.err.Error(), "\n") {
95103
buf.Write(padding)
96104
buf.Write(indent)
97105
buf.WriteString(line)
98106
buf.WriteRune('\n')
99107
}
100-
buf.WriteString(fmt.Sprintf(format, e2.StackTrace()))
101-
buf.WriteRune('\n')
102108
}
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
107113
}
108114
}
115+
if wErr, ok := err.(WrappedError); ok {
116+
err = wErr.Unwrap()
117+
} else {
118+
err = nil
119+
}
109120
}
110-
buf.WriteRune('\n')
111121
default:
112122
return
113123
}

erf_test.go

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,56 @@ import (
88

99
func ExampleErf() {
1010
e := erf.New("an example erf error")
11-
e2 := erf.Wrap(e)
11+
err := erf.Errorf("we have an example error: %w", e)
1212

13-
fmt.Println("just show first error message (default: padding char '\\t', padding 0, indent 1)")
14-
fmt.Printf("%s\n\n", e2)
13+
fmt.Println("just show the first error message without padding and indent.")
14+
fmt.Printf("%v\n\n", err)
1515

16-
fmt.Println("show error message with indent, append stack trace")
17-
fmt.Printf("%+s\n\n", e2)
16+
fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '%+s'.")
17+
fmt.Printf("%x\n\n", err)
1818

19-
fmt.Println("show first error message with indent, append stack trace")
20-
fmt.Printf("%+ s\n\n", e2)
19+
fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '% s'.")
20+
fmt.Printf("% x\n\n", err)
2121

22-
fmt.Println("use file name as file path for StackCaller")
23-
fmt.Printf("%+#s\n\n", e2)
22+
fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '%#s'.")
23+
fmt.Printf("%#x\n\n", err)
2424

25-
fmt.Println("padding 2, indent 1 by default")
26-
fmt.Printf("%+#2s\n\n", e2)
25+
fmt.Println("list all error messages by using indent and show StackTrace of errors by using format '% #s'.")
26+
fmt.Printf("% #x\n\n", err)
2727

28-
fmt.Println("padding 2, indent 3")
29-
fmt.Printf("%+#2.3s\n\n", e2)
28+
fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '%+s'.")
29+
fmt.Printf("%X\n\n", err)
3030

31-
fmt.Println("use ' ' as padding char (padding 0, indent 2)")
32-
fmt.Printf("%-s\n\n", e2)
31+
fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '% s'.")
32+
fmt.Printf("% X\n\n", err)
3333

34-
fmt.Println("show error message with indent, append stack trace")
35-
fmt.Printf("%-+s\n\n", e2)
34+
fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '%#s'.")
35+
fmt.Printf("%#X\n\n", err)
3636

37-
fmt.Println("show first error message with indent, append stack trace")
38-
fmt.Printf("%-+ s\n\n", e2)
37+
fmt.Println("show the first error message by using indent and show the StackTrace of error by using format '% #s'.")
38+
fmt.Printf("% #X\n\n", err)
3939

40-
fmt.Println("use file name as file path for StackCaller")
41-
fmt.Printf("%-+#s\n\n", e2)
40+
fmt.Println("don't show any error messages, just show all of StackTrace of errors.")
41+
fmt.Printf("%-x\n\n", err)
4242

43-
fmt.Println("padding 2, indent 2 by default")
44-
fmt.Printf("%-+#2s\n\n", e2)
43+
fmt.Println("don't show the error message, just show the StackTrace of the first error.")
44+
fmt.Printf("%-X\n\n", err)
4545

46-
fmt.Println("padding 2, indent 3")
47-
fmt.Printf("%-+#2.3s\n\n", e2)
46+
fmt.Println("padding 2, indent 1 by default. padding char '\\t'.")
47+
fmt.Printf("%2x\n\n", err)
48+
49+
fmt.Println("padding 2, indent 3. padding char '\\t'.")
50+
fmt.Printf("%2.3x\n\n", err)
51+
52+
fmt.Println("padding 0 by default, indent 3. padding char '\\t'.")
53+
fmt.Printf("%.3x\n\n", err)
54+
55+
fmt.Println("padding 2, indent 2 by default. padding char ' '.")
56+
fmt.Printf("% 2x\n\n", err)
57+
58+
fmt.Println("padding 2, indent 3. padding char ' '.")
59+
fmt.Printf("% 2.3x\n\n", err)
60+
61+
fmt.Println("padding 0 by default, indent 3. padding char ' '.")
62+
fmt.Printf("% .3x\n\n", err)
4863
}

examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/example_dev.go

examples/erf.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

examples/example1.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,43 +40,53 @@ func Baz(z int) error {
4040
}
4141

4242
func main() {
43-
fmt.Println("#### Foo: just show error text")
43+
fmt.Println("#### Foo: just the show first error message without padding and indent")
4444
if err := Foo(-1); err != nil {
4545
fmt.Printf("%v\n", err)
4646
}
4747

4848
fmt.Println("#### Foo: show with stack trace")
4949
if err := Foo(-2); err != nil {
50-
fmt.Printf("%+v\n", err)
50+
fmt.Printf("%x\n", err)
5151
}
5252

5353
fmt.Println("#### Foo: show with stack trace, and use space as whitespace instead of tab")
5454
if err := Foo(-3); err != nil {
55-
fmt.Printf("%+-v\n", err)
55+
fmt.Printf("% x\n", err)
5656
}
5757

58-
fmt.Println("#### Foo: show with last stack trace")
58+
fmt.Println("#### Foo: show with first stack trace")
5959
if err := Foo(-4); err != nil {
60-
fmt.Printf("%+0v\n", err)
60+
fmt.Printf("%X\n", err)
6161
}
6262

6363
fmt.Println("#### Foo: show with stack trace with only file names except full path")
6464
if err := Foo(-5); err != nil {
65-
fmt.Printf("%+#v\n", err)
65+
fmt.Printf("%#x\n", err)
6666
}
6767

6868
fmt.Println("#### Foo: show with stack trace with 2 whitespace chars of padding and 1 whitespace char of indent")
6969
if err := Foo(-6); err != nil {
70-
fmt.Printf("%+2.1v\n", err)
70+
fmt.Printf("%2.1x\n", err)
7171
}
7272

7373
fmt.Println("#### Bar: show with stack trace")
7474
if err := Bar(-7); err != nil {
75-
fmt.Printf("%+v\n", err)
75+
fmt.Printf("%x\n", err)
7676
}
7777

7878
fmt.Println("#### Baz: show with stack trace")
7979
if err := Baz(-8); err != nil {
80-
fmt.Printf("%+v\n", err)
80+
fmt.Printf("%x\n", err)
81+
}
82+
83+
fmt.Println("#### Baz: just show all of stack traces of errors")
84+
if err := Baz(-9); err != nil {
85+
fmt.Printf("%-x\n", err)
86+
}
87+
88+
fmt.Println("#### Baz: just show the stack trace of the first error")
89+
if err := Baz(-10); err != nil {
90+
fmt.Printf("%-X\n", err)
8191
}
8292
}

examples/example2.go

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)