Skip to content

Commit 11410dd

Browse files
committed
重构
1 parent 63feffc commit 11410dd

9 files changed

Lines changed: 60 additions & 18 deletions

File tree

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## ✒ 历史版本的特性介绍 (Features in old versions)
22

3-
### v0.8.0
3+
### v0.6.0-alpha
44

55
> 此版本发布于 2024-08-09
66

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ fmt:
66
go fmt ./...
77

88
test:
9-
go test -v -cover -count=1 -test.cpu=1 ./...
9+
go test -cover -count=1 -test.cpu=1 ./...

README.en.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ func main() {
3939
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))
4040

4141
// Also, we provide some useful information carrier for you.
42-
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("key", "value")
42+
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
4343
fmt.Println(err)
4444
}
45+
4546
```
4647

4748
* [basic](_examples/basic.go)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ func main() {
3939
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))
4040

4141
// Also, we provide some useful information carrier for you.
42-
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("key", "value")
42+
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
4343
fmt.Println(err)
4444
}
45+
4546
```
4647

4748
* [basic](_examples/basic.go)

_examples/basic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ func main() {
2525
fmt.Println(errors.Code(io.EOF, 6699), errors.Message(io.EOF, "default message"))
2626

2727
// Also, we provide some useful information carrier for you.
28-
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller().WithArgs("key", "value")
28+
err = errors.Wrap(9999, "io timeout").With(io.EOF).WithCaller()
2929
fmt.Println(err)
3030
}

_icons/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

errors_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package errors
66

77
import (
8+
"errors"
89
"io"
910
"os"
1011
"testing"
@@ -124,3 +125,40 @@ func TestUnwrap(t *testing.T) {
124125
}
125126
}
126127
}
128+
129+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestJoin$
130+
func TestJoin(t *testing.T) {
131+
testCases := []struct {
132+
errs []error
133+
}{
134+
{
135+
errs: nil,
136+
},
137+
{
138+
errs: []error{nil, nil},
139+
},
140+
{
141+
errs: []error{io.EOF, &testError{}},
142+
},
143+
{
144+
errs: []error{io.EOF, nil},
145+
},
146+
}
147+
148+
for _, testCase := range testCases {
149+
got := Join(testCase.errs...)
150+
want := errors.Join(testCase.errs...)
151+
152+
if got != nil && want != nil {
153+
if got.Error() != want.Error() {
154+
t.Errorf("got %+v != want %+v", got, want)
155+
}
156+
157+
continue
158+
}
159+
160+
if got != want {
161+
t.Errorf("got %+v != want %+v", got, want)
162+
}
163+
}
164+
}

runtime.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func Caller() string {
13-
_, file, line, ok := runtime.Caller(1)
13+
_, file, line, ok := runtime.Caller(2)
1414
if !ok {
1515
return ""
1616
}
@@ -20,7 +20,7 @@ func Caller() string {
2020

2121
func Callers() []string {
2222
var pcs [16]uintptr
23-
n := runtime.Callers(2, pcs[:])
23+
n := runtime.Callers(3, pcs[:])
2424
frames := runtime.CallersFrames(pcs[:n])
2525

2626
var callers []string

wrap.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package errors
66

77
import (
8+
"bytes"
89
"fmt"
910
"strings"
1011
)
@@ -14,7 +15,6 @@ type Error struct {
1415
message string
1516
cause error
1617
caller string
17-
args []any
1818
}
1919

2020
// Wrap returns *Error with code and message formatted with args.
@@ -47,11 +47,6 @@ func (e *Error) WithCallers() *Error {
4747
return e
4848
}
4949

50-
func (e *Error) WithArgs(args ...any) *Error {
51-
e.args = append(e.args, args...)
52-
return e
53-
}
54-
5550
// Code returns the code of *Error.
5651
func (e *Error) Code() int32 {
5752
return e.code
@@ -74,9 +69,16 @@ func (e *Error) Error() string {
7469

7570
// String returns *Error as string.
7671
func (e *Error) String() string {
77-
if e.cause == nil {
78-
return fmt.Sprintf("%d: %s", e.code, e.message)
72+
var buff bytes.Buffer
73+
fmt.Fprintf(&buff, "%d: %s", e.code, e.message)
74+
75+
if e.caller != "" {
76+
fmt.Fprintf(&buff, " [%s]", e.caller)
77+
}
78+
79+
if e.cause != nil {
80+
fmt.Fprintf(&buff, " (%+v)", e.cause)
7981
}
8082

81-
return fmt.Sprintf("%d: %s (%+v)", e.code, e.message, e.cause)
83+
return buff.String()
8284
}

0 commit comments

Comments
 (0)