Skip to content

Commit 7674e14

Browse files
committed
x 初步确定
1 parent dd32086 commit 7674e14

8 files changed

Lines changed: 143 additions & 45 deletions

File tree

x/shortcut.go renamed to x/errors.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

5-
package x
5+
package errors
66

77
import (
88
"errors"
99
)
1010

11+
func New(text string) error {
12+
return errors.New(text)
13+
}
14+
1115
// Is is a shortcut of errors.Is.
1216
func Is(err, target error) bool {
1317
return errors.Is(err, target)
Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

5-
package x
5+
package errors
66

77
import (
8+
"io"
89
"os"
910
"testing"
1011
)
@@ -24,18 +25,22 @@ func (te *testError) Error() string {
2425
// go test -v -cover -count=1 -test.cpu=1 -run=^TestIs$
2526
func TestIs(t *testing.T) {
2627
testErr := &testError{reason: "test error"}
27-
wrapErr := Wrap(testErr, -1000, "wrap test error")
28+
wrapErr := Wrap(-1000, "wrap test error").With(testErr)
2829

2930
testCases := []struct {
3031
err error
3132
cause error
3233
}{
3334
{
34-
err: Wrap(testErr, 1000, "wow"),
35+
err: io.EOF,
36+
cause: io.EOF,
37+
},
38+
{
39+
err: Wrap(1000, "wow").With(testErr),
3540
cause: testErr,
3641
},
3742
{
38-
err: Wrap(wrapErr, 1000, "wow too"),
43+
err: Wrap(1000, "wow too").With(wrapErr),
3944
cause: testErr,
4045
},
4146
}
@@ -50,7 +55,7 @@ func TestIs(t *testing.T) {
5055
// go test -v -cover -count=1 -test.cpu=1 -run=^TestAs$
5156
func TestAs(t *testing.T) {
5257
testErr := &testError{reason: "test error"}
53-
wrapErr := Wrap(testErr, -1000, "wrap test error")
58+
wrapErr := Wrap(-1000, "wrap test error").With(testErr)
5459

5560
targetTestErr := &testError{}
5661
targetTestErr2 := &testError{}
@@ -63,19 +68,19 @@ func TestAs(t *testing.T) {
6368
want any
6469
}{
6570
{
66-
err: Wrap(testErr, 1000, "wow"),
71+
err: Wrap(1000, "wow").With(testErr),
6772
target: &targetTestErr,
6873
ok: true,
6974
want: &testErr,
7075
},
7176
{
72-
err: Wrap(wrapErr, 1000, "wow too"),
77+
err: Wrap(1000, "wow too").With(wrapErr),
7378
target: &targetTestErr2,
7479
ok: true,
7580
want: &testErr,
7681
},
7782
{
78-
err: New(1000, "no"),
83+
err: Wrap(1000, "no"),
7984
target: &targetPathErr,
8085
ok: false,
8186
want: nil,
@@ -93,7 +98,7 @@ func TestAs(t *testing.T) {
9398
// go test -v -cover -count=1 -test.cpu=1 -run=^TestUnwrap$
9499
func TestUnwrap(t *testing.T) {
95100
testErr := &testError{reason: "test error"}
96-
wrapErr := Wrap(testErr, -1000, "wrap test error")
101+
wrapErr := Wrap(-1000, "wrap test error").With(testErr)
97102

98103
testCases := []struct {
99104
err error
@@ -104,11 +109,11 @@ func TestUnwrap(t *testing.T) {
104109
cause: nil,
105110
},
106111
{
107-
err: Wrap(testErr, 1000, "wow"),
112+
err: Wrap(1000, "wow").With(testErr),
108113
cause: testErr,
109114
},
110115
{
111-
err: Wrap(wrapErr, 1000, "wow too"),
116+
err: Wrap(1000, "wow too").With(wrapErr),
112117
cause: wrapErr,
113118
},
114119
}

x/runtime.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2024 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package errors
6+
7+
import (
8+
"runtime"
9+
"strconv"
10+
)
11+
12+
func Caller() string {
13+
_, file, line, ok := runtime.Caller(1)
14+
if !ok {
15+
return ""
16+
}
17+
18+
return file + ":" + strconv.Itoa(line)
19+
}
20+
21+
func Callers() []string {
22+
pcs := make([]uintptr, 16)
23+
n := runtime.Callers(2, pcs)
24+
frames := runtime.CallersFrames(pcs[:n])
25+
26+
var callers []string
27+
for {
28+
frame, more := frames.Next()
29+
30+
caller := frame.File + ":" + strconv.Itoa(frame.Line)
31+
callers = append(callers, caller)
32+
33+
if !more {
34+
break
35+
}
36+
}
37+
38+
return callers
39+
}

x/runtime_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2024 FishGoddess. All rights reserved.
2+
// Use of this source code is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package errors
6+
7+
import (
8+
"testing"
9+
)
10+
11+
// BenchmarkCaller-2 1390318 876.7 ns/op 296 B/op 3 allocs/op
12+
func BenchmarkCaller(b *testing.B) {
13+
b.ReportAllocs()
14+
b.ResetTimer()
15+
16+
for i := 0; i < b.N; i++ {
17+
Caller()
18+
}
19+
}
20+
21+
// BenchmarkCallers-2 443419 2573 ns/op 732 B/op 12 allocs/op
22+
func BenchmarkCallers(b *testing.B) {
23+
b.ReportAllocs()
24+
b.ResetTimer()
25+
26+
for i := 0; i < b.N; i++ {
27+
Callers()
28+
}
29+
}
30+
31+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCaller$
32+
func TestCaller(t *testing.T) {
33+
caller := Caller()
34+
t.Log(caller)
35+
}
36+
37+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestCallers$
38+
func TestCallers(t *testing.T) {
39+
callers := Callers()
40+
t.Log(callers)
41+
}

x/unwrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

5-
package x
5+
package errors
66

77
// Code unwraps error and gets code of it.
88
// It returns 0 if err is nil.

x/unwrap_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

5-
package x
5+
package errors
66

77
import (
88
"io"
@@ -27,12 +27,12 @@ func TestCode(t *testing.T) {
2727
code: 999,
2828
},
2929
{
30-
err: New(1000, "wow"),
30+
err: Wrap(1000, "wow"),
3131
defaultCode: 999,
3232
code: 1000,
3333
},
3434
{
35-
err: Wrap(io.EOF, 1000, "eof"),
35+
err: Wrap(1000, "eof").With(io.EOF),
3636
defaultCode: 999,
3737
code: 1000,
3838
},
@@ -64,12 +64,12 @@ func TestMessage(t *testing.T) {
6464
message: "eof",
6565
},
6666
{
67-
err: New(1000, "wow"),
67+
err: Wrap(1000, "wow"),
6868
defaultMessage: "xxx",
6969
message: "wow",
7070
},
7171
{
72-
err: Wrap(io.EOF, 1000, "eof"),
72+
err: Wrap(1000, "eof").With(io.EOF),
7373
defaultMessage: "xxx",
7474
message: "eof",
7575
},

x/error.go renamed to x/wrap.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,54 @@
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

5-
package x
5+
package errors
66

77
import (
88
"fmt"
9+
"strings"
910
)
1011

1112
type Error struct {
1213
code int32
1314
message string
1415
cause error
16+
caller string
17+
args []any
1518
}
1619

17-
// New returns *Error with code and message.
18-
func New(code int32, message string) *Error {
19-
return &Error{
20+
// Wrap returns *Error with code and message formatted with args.
21+
func Wrap(code int32, message string, args ...any) *Error {
22+
if len(args) > 0 {
23+
message = fmt.Sprintf(message, args...)
24+
}
25+
26+
err := &Error{
2027
code: code,
2128
message: message,
2229
}
30+
31+
return err
2332
}
2433

25-
// Newf returns *Error with code and formatted message.
26-
func Newf(code int32, format string, args ...interface{}) *Error {
27-
message := fmt.Sprintf(format, args...)
28-
return New(code, message)
34+
func (e *Error) With(err error) *Error {
35+
e.cause = err
36+
return e
2937
}
3038

31-
// Wrap wraps err to *Error with code and message.
32-
func Wrap(err error, code int32, message string) *Error {
33-
return &Error{
34-
code: code,
35-
message: message,
36-
cause: err,
37-
}
39+
func (e *Error) WithCaller() *Error {
40+
e.caller = Caller()
41+
return e
42+
}
43+
44+
func (e *Error) WithCallers() *Error {
45+
callers := Callers()
46+
e.caller = strings.Join(callers, "¦")
47+
return e
3848
}
3949

40-
// Wrapf wraps err to *Error with code and formatted message.
41-
func Wrapf(err error, code int32, format string, args ...interface{}) *Error {
42-
message := fmt.Sprintf(format, args...)
43-
return Wrap(err, code, message)
50+
func (e *Error) WithArgs(args ...any) *Error {
51+
e.args = append(e.args, args...)
52+
return e
4453
}
4554

4655
// Code returns the code of *Error.

x/error_test.go renamed to x/wrap_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Use of this source code is governed by a MIT style
33
// license that can be found in the LICENSE file.
44

5-
package x
5+
package errors
66

77
import (
88
"io"
99
"testing"
1010
)
1111

12-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestNew$
13-
func TestNew(t *testing.T) {
12+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWrap$
13+
func TestWrap(t *testing.T) {
1414
testCases := []struct {
1515
code int32
1616
message string
@@ -20,7 +20,7 @@ func TestNew(t *testing.T) {
2020
}
2121

2222
for _, testCase := range testCases {
23-
err := New(testCase.code, testCase.message)
23+
err := Wrap(testCase.code, testCase.message)
2424
if err.Code() != testCase.code {
2525
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
2626
}
@@ -35,8 +35,8 @@ func TestNew(t *testing.T) {
3535
}
3636
}
3737

38-
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWrap$
39-
func TestWrap(t *testing.T) {
38+
// go test -v -cover -count=1 -test.cpu=1 -run=^TestWrapWith$
39+
func TestWrapWith(t *testing.T) {
4040
testCases := []struct {
4141
code int32
4242
message string
@@ -47,7 +47,7 @@ func TestWrap(t *testing.T) {
4747
}
4848

4949
for _, testCase := range testCases {
50-
err := Wrap(testCase.cause, testCase.code, testCase.message)
50+
err := Wrap(testCase.code, testCase.message).With(testCase.cause)
5151
if err.Code() != testCase.code {
5252
t.Errorf("err.Code() %d != testCase.code %d", err.Code(), testCase.code)
5353
}
@@ -75,7 +75,7 @@ func TestErrorError(t *testing.T) {
7575
}
7676

7777
for _, testCase := range testCases {
78-
err := Wrap(testCase.cause, testCase.code, testCase.message)
78+
err := Wrap(testCase.code, testCase.message).With(testCase.cause)
7979
if err.Error() != testCase.errorString {
8080
t.Errorf("err.Error() %s != testCase.errorString %s", err.Error(), testCase.errorString)
8181
}

0 commit comments

Comments
 (0)