Skip to content

Commit 625c4a9

Browse files
authored
Merge pull request #4 from RyougiNevermore/master
update depth
2 parents 177737e + 99f31ca commit 625c4a9

5 files changed

Lines changed: 61 additions & 37 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Errors interface {
3939
```go
4040
e1 := io.EOF
4141
e2 := errors.With(e1, "error2")
42-
e3 := errors.WithF(e2, "%s", "error3")
42+
e3 := errors.Withf(e2, "%s", "error3")
4343

4444
if errors.Contains(e3, e2) {
4545
// TODO ..

errors.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func New(message string) error {
1414
}
1515
}
1616

17-
func ErrorF(format string, args ...interface{}) error {
17+
func Errorf(format string, args ...interface{}) error {
1818
return &errorUp{
1919
msg: fmt.Sprintf(format, args...),
2020
cause: nil,
@@ -23,6 +23,24 @@ func ErrorF(format string, args ...interface{}) error {
2323
}
2424
}
2525

26+
func NewWithDepth(depth int, skip int, message string) error {
27+
return &errorUp{
28+
msg: message,
29+
cause: nil,
30+
pcs: callersByAssigned(depth, skip),
31+
occurred: timeNow(),
32+
}
33+
}
34+
35+
func ErrorWithDepthf(depth int, skip int, format string, args ...interface{}) error {
36+
return &errorUp{
37+
msg: fmt.Sprintf(format, args...),
38+
cause: nil,
39+
pcs: callersByAssigned(depth, skip),
40+
occurred: timeNow(),
41+
}
42+
}
43+
2644
func With(cause error, message string) error {
2745
if cause == nil {
2846
return nil
@@ -35,7 +53,7 @@ func With(cause error, message string) error {
3553
}
3654
}
3755

38-
func WithF(cause error, format string, args ...interface{}) error {
56+
func Withf(cause error, format string, args ...interface{}) error {
3957
if cause == nil {
4058
return nil
4159
}
@@ -47,26 +65,26 @@ func WithF(cause error, format string, args ...interface{}) error {
4765
}
4866
}
4967

50-
func WithDepth(depth int, cause error, message string) error {
68+
func WithDepth(depth int, skip int, cause error, message string) error {
5169
if cause == nil {
5270
return nil
5371
}
5472
return &errorUp{
5573
msg: message,
5674
cause: cause,
57-
pcs: callersByAssigned(depth, 3),
75+
pcs: callersByAssigned(depth, skip),
5876
occurred: timeNow(),
5977
}
6078
}
6179

62-
func WithDepthF(depth int, cause error, format string, args ...interface{}) error {
80+
func WithDepthf(depth int, skip int, cause error, format string, args ...interface{}) error {
6381
if cause == nil {
6482
return nil
6583
}
6684
return &errorUp{
6785
msg: fmt.Sprintf(format, args...),
6886
cause: cause,
69-
pcs: callersByAssigned(depth, 3),
87+
pcs: callersByAssigned(depth, skip),
7088
occurred: timeNow(),
7189
}
7290
}
@@ -80,15 +98,6 @@ func Wrap(e error) error {
8098
}
8199
}
82100

83-
func NewByAssigned(depth int, skip int, message string) error {
84-
return &errorUp{
85-
msg: message,
86-
cause: nil,
87-
pcs: callersByAssigned(depth, skip),
88-
occurred: timeNow(),
89-
}
90-
}
91-
92101
type errorUp struct {
93102
msg string
94103
cause error

errors_test.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,24 @@ import (
99

1010
func TestErrorF(t *testing.T) {
1111
e1 := errors.New("error 1")
12-
e2 := errors.WithF(e1, "error %d", 2)
12+
e2 := errors.Withf(e1, "error %d", 2)
1313
fmt.Println(fmt.Sprintf("%+v", e2))
1414

15-
e3 := errors.WithDepth(2, e2, "error 3")
15+
e3 := errors.WithDepth(2, 3, e2, "error 3")
1616
fmt.Println(fmt.Sprintf("%-v", e3))
1717

1818
}
19+
20+
func wrap(msg string) error {
21+
return errors.NewWithDepth(1, 4, msg)
22+
}
23+
24+
func Test_Wrap(t *testing.T) {
25+
26+
e1 := wrap("1")
27+
e2 := errors.With(e1, "error 2")
28+
e3 := errors.With(e2, "error 3")
29+
30+
fmt.Println(fmt.Sprintf("%+v", e3))
31+
32+
}

format.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"runtime"
7+
"time"
78
)
89

910
type Format func(s fmt.State, verb rune, e Errors)
@@ -20,11 +21,11 @@ func DefaultFormatFn(s fmt.State, verb rune, e Errors) {
2021
_, _ = io.WriteString(s, "unknown")
2122
} else {
2223
file, line := fn.FileLine(pc)
23-
home, filename := fileName(file)
24+
_, filename := fileName(file)
2425
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)
26+
_, _ = fmt.Fprintf(s, "\t[T] %s\n\t[F] %s\n\t[F] %s:%d \n", e.OccurTime().Format(time.RFC3339), fn.Name(), filename, line)
2627
} else {
27-
_, _ = fmt.Fprintf(s, "\t[F] %s\n\t[H] %s\n\t[F] %s:%d \n", fn.Name(), home, filename, line)
28+
_, _ = fmt.Fprintf(s, "\t[F] %s\n\t[F] %s:%d \n", fn.Name(), filename, line)
2829
}
2930
}
3031
}
@@ -38,18 +39,18 @@ func DefaultFormatFn(s fmt.State, verb rune, e Errors) {
3839
}
3940
case s.Flag('-'):
4041
_, _ = io.WriteString(s, "{")
41-
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime())
42+
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime().Format(time.RFC3339))
4243
for i, pc := range e.PCS() {
4344
if i > 0 {
4445
_, _ = io.WriteString(s, ",")
4546
}
4647
fn := runtime.FuncForPC(pc)
4748
if fn == nil {
48-
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
49+
_, _ = fmt.Fprintf(s, `{"fn":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", 0)
4950
} else {
5051
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)
52+
_, filename := fileName(file)
53+
_, _ = fmt.Fprintf(s, `{"fn":"%s", "file":"%s", "line":%d}`, fn.Name(), filename, line)
5354
}
5455
}
5556
_, _ = io.WriteString(s, "]")
@@ -75,35 +76,35 @@ func JsonFormatFn(s fmt.State, verb rune, e Errors) {
7576
case 'v':
7677
switch {
7778
case s.Flag('+'):
78-
io.WriteString(s, "{")
79-
fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime())
79+
_, _ = io.WriteString(s, "{")
80+
_, _ = fmt.Fprintf(s, `"msg":"%s", "occurTime":"%s", "stack":[`, e.Error(), e.OccurTime().Format(time.RFC3339))
8081
for i, pc := range e.PCS() {
8182
if i > 0 {
82-
io.WriteString(s, ",")
83+
_, _ = io.WriteString(s, ",")
8384
}
8485
fn := runtime.FuncForPC(pc)
8586
if fn == nil {
86-
fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
87+
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, "unknown", "unknown", "unknown", 0)
8788
} else {
8889
file, line := fn.FileLine(pc)
8990
home, filename := fileName(file)
90-
fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, fn.Name(), home, filename, line)
91+
_, _ = fmt.Fprintf(s, `{"fn":"%s", "home":"%s", "file":"%s", "line":%d}`, fn.Name(), home, filename, line)
9192
}
9293
}
93-
io.WriteString(s, "]")
94+
_, _ = io.WriteString(s, "]")
9495
if e.Cause() != nil {
95-
io.WriteString(s, ",")
96+
_, _ = io.WriteString(s, ",")
9697
hasCause, ok := e.Cause().(Errors)
9798
if !ok {
98-
fmt.Fprintf(s, `"cause":{"msg":"%s"}`, e.Cause().Error())
99+
_, _ = fmt.Fprintf(s, `"cause":{"msg":"%s"}`, e.Cause().Error())
99100
} else {
100-
io.WriteString(s, `"cause":`)
101+
_, _ = io.WriteString(s, `"cause":`)
101102
hasCause.Format(s, verb)
102103
}
103104
}
104-
io.WriteString(s, "}")
105+
_, _ = io.WriteString(s, "}")
105106
default:
106-
fmt.Fprintf(s, "%s", e.Error())
107+
_, _ = fmt.Fprintf(s, "%s", e.Error())
107108
}
108109
}
109110
}

stack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func fileName(src string) (goPath string, file string) {
2424
}
2525

2626
func timeNow() time.Time {
27-
return time.Now().In(_cfg.loc)
27+
return time.Now()
2828
}
2929

3030
func callers() []uintptr {

0 commit comments

Comments
 (0)