Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit a2ae9c7

Browse files
committed
errors: Import changes and refactorings from pkg/errors
Benchmark ran with Go 1.9 name old time/op new time/op delta AssertBehaviourEmptyStruct-4 23.6ns ± 1% 23.1ns ± 1% -2.21% (p=0.000 n=10+10) AssertBehaviourConstant-4 23.3ns ± 2% 23.2ns ± 1% ~ (p=0.719 n=10+8) AssertBehaviourPointer-4 9.23ns ± 1% 8.42ns ± 1% -8.80% (p=0.000 n=9+10) AssertBehaviourNoMatch-4 62.6ns ± 1% 92.6ns ± 1% +48.00% (p=0.000 n=10+10) MultiErr-4 936ns ± 2% 942ns ± 2% ~ (p=0.305 n=10+10) MultiErrPointer-4 724ns ± 1% 728ns ± 2% ~ (p=0.267 n=9+10) MultiErrConstant-4 758ns ± 3% 749ns ± 2% ~ (p=0.108 n=9+9) name old alloc/op new alloc/op delta AssertBehaviourEmptyStruct-4 0.00B ±NaN% 0.00B ±NaN% ~ (all samples are equal) AssertBehaviourConstant-4 0.00B ±NaN% 0.00B ±NaN% ~ (all samples are equal) AssertBehaviourPointer-4 0.00B ±NaN% 0.00B ±NaN% ~ (all samples are equal) AssertBehaviourNoMatch-4 0.00B ±NaN% 0.00B ±NaN% ~ (all samples are equal) MultiErr-4 140B ± 0% 140B ± 0% ~ (all samples are equal) MultiErrPointer-4 224B ± 0% 224B ± 0% ~ (all samples are equal) MultiErrConstant-4 224B ± 0% 224B ± 0% ~ (all samples are equal) name old allocs/op new allocs/op delta AssertBehaviourEmptyStruct-4 0.00 ±NaN% 0.00 ±NaN% ~ (all samples are equal) AssertBehaviourConstant-4 0.00 ±NaN% 0.00 ±NaN% ~ (all samples are equal) AssertBehaviourPointer-4 0.00 ±NaN% 0.00 ±NaN% ~ (all samples are equal) AssertBehaviourNoMatch-4 0.00 ±NaN% 0.00 ±NaN% ~ (all samples are equal) MultiErr-4 5.00 ± 0% 5.00 ± 0% ~ (all samples are equal) MultiErrPointer-4 4.00 ± 0% 4.00 ± 0% ~ (all samples are equal) MultiErrConstant-4 4.00 ± 0% 4.00 ± 0% ~ (all samples are equal)
1 parent 097a9f7 commit a2ae9c7

13 files changed

Lines changed: 1135 additions & 627 deletions

behaviour.go

Lines changed: 264 additions & 264 deletions
Large diffs are not rendered by default.

behaviour_error.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,33 @@ import "fmt"
2020
// if an error has a specific behaviour attached.
2121
type BehaviourFunc func(error) bool
2222

23-
func errWrapf(err error, format string, args ...interface{}) wrapper {
24-
ret := wrapper{
25-
cause: cause{
23+
func fmtNoSprintf(format string, _ ...interface{}) string {
24+
return format
25+
}
26+
27+
func errWrapf(err error, format string, args ...interface{}) *withStack {
28+
sprintf := fmtNoSprintf
29+
if len(args) > 0 {
30+
sprintf = fmt.Sprintf
31+
}
32+
return &withStack{
33+
error: &withMessage{
2634
cause: err,
27-
msg: format,
35+
msg: sprintf(format, args...),
2836
},
2937
stack: callers(),
3038
}
31-
if len(args) > 0 {
32-
ret.cause.msg = fmt.Sprintf(format, args...)
33-
}
34-
return ret
3539
}
3640

37-
func errNewf(format string, args ...interface{}) (ret _error) {
38-
ret.msg = format
39-
ret.stack = callers()
41+
func errNewf(format string, args ...interface{}) *fundamental {
42+
sprintf := fmtNoSprintf
4043
if len(args) > 0 {
41-
ret.msg = fmt.Sprintf(format, args...)
44+
sprintf = fmt.Sprintf
45+
}
46+
return &fundamental{
47+
msg: sprintf(format, args...),
48+
stack: callers(),
4249
}
43-
return
4450
}
4551

4652
// HasBehaviour checks if err contains at least one of the provided behaviour

behaviour_error_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ func TestCausedBehaviour(t *testing.T) {
4646
}
4747

4848
func TestError_Error(t *testing.T) {
49-
t.Parallel()
49+
5050
const e1 Error = "e1"
5151
assert.EqualError(t, e1, "e1")
5252
}
5353

5454
func TestWrapf2(t *testing.T) {
55-
t.Parallel()
55+
5656
var e = Wrapf(nil, "Error %d")
5757
assert.Nil(t, e)
5858
}
5959

6060
func TestErrorContainsAny(t *testing.T) {
61-
t.Parallel()
61+
6262
tests := []struct {
6363
me error
6464
vf []BehaviourFunc
@@ -84,18 +84,18 @@ func TestErrorContainsAny(t *testing.T) {
8484
}
8585

8686
func TestErrWrapf(t *testing.T) {
87-
t.Parallel()
87+
8888
const e Error = "Error1"
89-
if haveEB, want := errWrapf(e, "Hello World %#v"), "Hello World %#v"; haveEB.msg != want {
90-
t.Errorf("have %q want %q", haveEB.msg, want)
89+
if haveEB, want := errWrapf(e, "Hello World %#v"), "Hello World %#v"; haveEB.error.(*withMessage).msg != want {
90+
t.Errorf("have %q want %q", haveEB.error.(*withMessage).msg, want)
9191
}
92-
if haveEB, want := errWrapf(e, "Hello World %d", 123), "Hello World 123"; haveEB.msg != want {
93-
t.Errorf("have %q want %q", haveEB.msg, want)
92+
if haveEB, want := errWrapf(e, "Hello World %d", 123), "Hello World 123"; haveEB.error.(*withMessage).msg != want {
93+
t.Errorf("have %q want %q", haveEB.error.(*withMessage).msg, want)
9494
}
9595
}
9696

9797
func TestErrNewf(t *testing.T) {
98-
t.Parallel()
98+
9999
if have, want := errNewf("Hello World %d", 633), "Hello World 633"; have.msg != want {
100100
t.Errorf("have %q want %q", have.msg, want)
101101
}

behaviour_gen.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ func isSeparator(r rune) bool {
174174

175175
const behaviourTemplate = `
176176
type (
177-
{{.LcName}} struct{ wrapper }
178-
{{.LcName}}f struct{ _error }
177+
{{.LcName}} struct{ *withStack }
178+
{{.LcName}}f struct{ *fundamental }
179179
)
180180
181181
// New{{.TcName}} returns an error which wraps err that satisfies
@@ -184,22 +184,22 @@ func New{{.TcName}}(err error, msg string, args ...interface{}) error {
184184
if err == nil {
185185
return nil
186186
}
187-
return &{{.LcName}}{errWrapf(err, msg, args...)}
187+
return {{.LcName}}{errWrapf(err, msg, args...)}
188188
}
189189
190190
// New{{.TcName}}f returns a formatted error that satisfies Is{{.TcName}}().
191191
func New{{.TcName}}f(format string, args ...interface{}) error {
192-
return &{{.LcName}}f{errNewf(format, args...)}
192+
return {{.LcName}}f{errNewf(format, args...)}
193193
}
194194
195195
func is{{.TcName}}(err error) (ok bool) {
196196
type iFace interface {
197197
{{.TcName}}() bool
198198
}
199199
switch et := err.(type) {
200-
case *{{.LcName}}:
200+
case {{.LcName}}:
201201
ok = true
202-
case *{{.LcName}}f:
202+
case {{.LcName}}f:
203203
ok = true
204204
case iFace:
205205
ok = et.{{.TcName}}()
@@ -222,9 +222,7 @@ func (nf testBehave) {{.TcName}}() bool {
222222
return nf.ret
223223
}
224224
225-
func Test{{.TcName}}(t *testing.T) {
226-
t.Parallel()
227-
225+
func TestBehaviour{{.TcName}}(t *testing.T) {
228226
tests := []struct {
229227
err error
230228
is BehaviourFunc

0 commit comments

Comments
 (0)