Skip to content

Commit b13617d

Browse files
committed
UPDATE FORMAT
1 parent b763404 commit b13617d

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type Errors interface {
4848
if errors.Contains(e3, e1) {
4949
// TODO ...
5050
}
51+
52+
// full format
53+
fmt.Println(fmt.Sprintf("%+v", e3))
54+
55+
// json format
56+
fmt.Println(fmt.Sprintf("%-v", e3))
5157
```
5258

5359
[Read the examples for more usages.](https://github.com/pharosnet/errors/tree/master/example)

errors.go

+24
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ func WithF(cause error, format string, args ...interface{}) error {
4747
}
4848
}
4949

50+
func WithDepth(depth int, cause error, message string) error {
51+
if cause == nil {
52+
return nil
53+
}
54+
return &errorUp{
55+
msg: message,
56+
cause: cause,
57+
pcs: callersByAssigned(depth, 3),
58+
occurred: timeNow(),
59+
}
60+
}
61+
62+
func WithDepthF(depth int, cause error, format string, args ...interface{}) error {
63+
if cause == nil {
64+
return nil
65+
}
66+
return &errorUp{
67+
msg: fmt.Sprintf(format, args...),
68+
cause: cause,
69+
pcs: callersByAssigned(depth, 3),
70+
occurred: timeNow(),
71+
}
72+
}
73+
5074
func Wrap(e error) error {
5175
return &errorUp{
5276
msg: e.Error(),

errors_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package errors_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/pharosnet/errors"
8+
)
9+
10+
func TestErrorF(t *testing.T) {
11+
e1 := errors.New("error 1")
12+
e2 := errors.WithF(e1, "error %d", 2)
13+
fmt.Println(fmt.Sprintf("%+v", e2))
14+
15+
e3 := errors.WithDepth(2, e2, "error 3")
16+
fmt.Println(fmt.Sprintf("%-v", e3))
17+
18+
}

format.go

+34-6
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,59 @@ func DefaultFormatFn(s fmt.State, verb rune, e Errors) {
1313
case 'v':
1414
switch {
1515
case s.Flag('+'):
16-
fmt.Fprintf(s, "%s\n", e.Error())
16+
_, _ = fmt.Fprintf(s, "- %s\n", e.Error())
1717
for i, pc := range e.PCS() {
1818
fn := runtime.FuncForPC(pc)
1919
if fn == nil {
20-
io.WriteString(s, "unknown")
20+
_, _ = io.WriteString(s, "unknown")
2121
} else {
2222
file, line := fn.FileLine(pc)
2323
home, filename := fileName(file)
2424
if i == 0 {
25-
fmt.Fprintf(s, "\t[T] %s\n\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", e.OccurTime().String(), fn.Name(), home, filename, line)
25+
_, _ = fmt.Fprintf(s, "\t[T] %s\n\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", e.OccurTime().String(), fn.Name(), home, filename, line)
2626
} else {
27-
fmt.Fprintf(s, "\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", fn.Name(), home, filename, line)
27+
_, _ = fmt.Fprintf(s, "\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", fn.Name(), home, filename, line)
2828
}
2929
}
3030
}
3131
if e.Cause() != nil {
3232
hasCause, ok := e.Cause().(Errors)
3333
if !ok {
34-
fmt.Fprintf(s, "%v\n", e.Cause())
34+
_, _ = fmt.Fprintf(s, "%v\n", e.Cause())
3535
} else {
3636
hasCause.Format(s, verb)
3737
}
3838
}
39+
case s.Flag('-'):
40+
_, _ = io.WriteString(s, "{")
41+
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime())
42+
for i, pc := range e.PCS() {
43+
if i > 0 {
44+
_, _ = io.WriteString(s, ",")
45+
}
46+
fn := runtime.FuncForPC(pc)
47+
if fn == nil {
48+
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
49+
} else {
50+
file, line := fn.FileLine(pc)
51+
home, filename := fileName(file)
52+
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, fn.Name(), home, filename, line)
53+
}
54+
}
55+
_, _ = io.WriteString(s, "]")
56+
if e.Cause() != nil {
57+
_, _ = io.WriteString(s, ",")
58+
hasCause, ok := e.Cause().(Errors)
59+
if !ok {
60+
_, _ = fmt.Fprintf(s, `"cause":{"msg":"%s"}`, e.Cause().Error())
61+
} else {
62+
_, _ = io.WriteString(s, `"cause":`)
63+
hasCause.Format(s, verb)
64+
}
65+
}
66+
_, _ = io.WriteString(s, "}")
3967
default:
40-
fmt.Fprintf(s, "%s", e.Error())
68+
_, _ = fmt.Fprintf(s, "%s", e.Error())
4169
}
4270
}
4371
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/pharosnet/errors
2+
3+
go 1.14

0 commit comments

Comments
 (0)