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

Commit 7add756

Browse files
Merge pull request #8 from goinsane/develop
v1.1.1
2 parents 372a29b + c38b182 commit 7add756

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

erf.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,9 @@ func (e *Erf) Format(f fmt.State, verb rune) {
7676
format += string(r)
7777
}
7878
}
79-
pad, wid, prec := byte('\t'), 0, 1
80-
if f.Flag(' ') {
81-
pad = ' '
82-
prec = 2
83-
}
84-
if w, ok := f.Width(); ok {
85-
wid = w
86-
}
87-
if p, ok := f.Precision(); ok {
88-
prec = p
89-
}
90-
format += fmt.Sprintf("%d.%d", wid, prec)
91-
format += "s"
92-
padding := bytes.Repeat([]byte{pad}, wid)
93-
indent := bytes.Repeat([]byte{pad}, prec)
79+
pad, wid, prec := getPadWidPrec(f)
80+
format += fmt.Sprintf("%d.%ds", wid, prec)
81+
padding, indent := bytes.Repeat([]byte{pad}, wid), bytes.Repeat([]byte{pad}, prec)
9482
count := 0
9583
for err := error(e); err != nil; {
9684
if e2, ok := err.(*Erf); ok {
@@ -108,6 +96,7 @@ func (e *Erf) Format(f fmt.State, verb rune) {
10896
}
10997
buf.WriteString(fmt.Sprintf(format, e2.StackTrace()))
11098
buf.WriteRune('\n')
99+
buf.Write(padding)
111100
if verb == 'X' {
112101
break
113102
}

examples/example1.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func main() {
6565
fmt.Printf("%#x\n", err)
6666
}
6767

68-
fmt.Println("#### Foo: show with stack trace with 2 whitespace chars of padding and 1 whitespace char of indent")
68+
fmt.Println("#### Foo: show with stack trace (padding 2, indent 1)")
6969
if err := Foo(-6); err != nil {
7070
fmt.Printf("%2.1x\n", err)
7171
}
@@ -75,18 +75,24 @@ func main() {
7575
fmt.Printf("%x\n", err)
7676
}
7777

78+
fmt.Println("#### Bar: show wrapped error with stack trace (padding 2, indent 1)")
79+
if err := Foo(-8); err != nil {
80+
err = erf.Wrap(err)
81+
fmt.Printf("%2.1x\n", err)
82+
}
83+
7884
fmt.Println("#### Baz: show with stack trace")
79-
if err := Baz(-8); err != nil {
85+
if err := Baz(-9); err != nil {
8086
fmt.Printf("%x\n", err)
8187
}
8288

8389
fmt.Println("#### Baz: just show all of stack traces of errors")
84-
if err := Baz(-9); err != nil {
90+
if err := Baz(-10); err != nil {
8591
fmt.Printf("%-x\n", err)
8692
}
8793

8894
fmt.Println("#### Baz: just show the stack trace of the first error")
89-
if err := Baz(-10); err != nil {
95+
if err := Baz(-11); err != nil {
9096
fmt.Printf("%-X\n", err)
9197
}
9298
}

stack.go

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,8 @@ func (c StackCaller) Format(f fmt.State, verb rune) {
5050
buf.WriteString(fmt.Sprintf("%s(%#x)", fn, c.Entry))
5151
break
5252
}
53-
pad, wid, prec := byte('\t'), 0, 1
54-
if f.Flag(' ') {
55-
pad = ' '
56-
prec = 2
57-
}
58-
if w, ok := f.Width(); ok {
59-
wid = w
60-
}
61-
if p, ok := f.Precision(); ok {
62-
prec = p
63-
}
64-
padding := bytes.Repeat([]byte{pad}, wid)
65-
indent := bytes.Repeat([]byte{pad}, prec)
53+
pad, wid, prec := getPadWidPrec(f)
54+
padding, indent := bytes.Repeat([]byte{pad}, wid), bytes.Repeat([]byte{pad}, prec)
6655
buf.Write(padding)
6756
buf.WriteString(fmt.Sprintf("%s(%#x)", fn, c.Entry))
6857
buf.WriteRune('\n')
@@ -146,13 +135,8 @@ func (t *StackTrace) Format(f fmt.State, verb rune) {
146135
format += string(r)
147136
}
148137
}
149-
if w, ok := f.Width(); ok {
150-
format += fmt.Sprintf("%d", w)
151-
}
152-
if p, ok := f.Precision(); ok {
153-
format += fmt.Sprintf(".%d", p)
154-
}
155-
format += "s"
138+
_, wid, prec := getPadWidPrec(f)
139+
format += fmt.Sprintf("%d.%ds", wid, prec)
156140
for i, c := range t.callers {
157141
if i > 0 {
158142
buf.WriteRune('\n')

utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package erf
22

33
import (
4+
"fmt"
45
"go/build"
56
"os"
67
"strings"
@@ -27,3 +28,18 @@ func trimDirs(s string) string {
2728
}
2829
return s
2930
}
31+
32+
func getPadWidPrec(f fmt.State) (pad byte, wid, prec int) {
33+
pad, wid, prec = byte('\t'), 0, 1
34+
if f.Flag(' ') {
35+
pad = ' '
36+
prec = 2
37+
}
38+
if w, ok := f.Width(); ok {
39+
wid = w
40+
}
41+
if p, ok := f.Precision(); ok {
42+
prec = p
43+
}
44+
return
45+
}

0 commit comments

Comments
 (0)