Skip to content

Commit 62f5e81

Browse files
committed
errors: add Unwrap, Is, and As
Unwrap, Is and As are as defined in proposal Issue #29934. Also add Opaque for enforcing an error cannot be unwrapped. Change-Id: I4f3feaa42e3ee7477b588164ac622ba4d5e77cad Reviewed-on: https://go-review.googlesource.com/c/163558 Run-TryBot: Marcel van Lohuizen <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 37f8481 commit 62f5e81

File tree

4 files changed

+380
-1
lines changed

4 files changed

+380
-1
lines changed

src/errors/example_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package errors_test
66

77
import (
8+
"errors"
89
"fmt"
10+
"os"
911
"time"
1012
)
1113

@@ -32,3 +34,16 @@ func Example() {
3234
}
3335
// Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away
3436
}
37+
38+
func ExampleAs() {
39+
_, err := os.Open("non-existing")
40+
if err != nil {
41+
var pathError *os.PathError
42+
if errors.As(err, &pathError) {
43+
fmt.Println("Failed at path:", pathError.Path)
44+
}
45+
}
46+
47+
// Output:
48+
// Failed at path: non-existing
49+
}

src/errors/wrap.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package errors
6+
7+
import (
8+
"internal/reflectlite"
9+
)
10+
11+
// A Wrapper provides context around another error.
12+
type Wrapper interface {
13+
// Unwrap returns the next error in the error chain.
14+
// If there is no next error, Unwrap returns nil.
15+
Unwrap() error
16+
}
17+
18+
// Opaque returns an error with the same error formatting as err
19+
// but that does not match err and cannot be unwrapped.
20+
func Opaque(err error) error {
21+
return noWrapper{err}
22+
}
23+
24+
type noWrapper struct {
25+
error
26+
}
27+
28+
func (e noWrapper) FormatError(p Printer) (next error) {
29+
if f, ok := e.error.(Formatter); ok {
30+
return f.FormatError(p)
31+
}
32+
p.Print(e.error)
33+
return nil
34+
}
35+
36+
// Unwrap returns the result of calling the Unwrap method on err, if err
37+
// implements Unwrap. Otherwise, Unwrap returns nil.
38+
func Unwrap(err error) error {
39+
u, ok := err.(Wrapper)
40+
if !ok {
41+
return nil
42+
}
43+
return u.Unwrap()
44+
}
45+
46+
// Is reports whether any error in err's chain matches target.
47+
//
48+
// An error is considered to match a target if it is equal to that target or if
49+
// it implements a method Is(error) bool such that Is(target) returns true.
50+
func Is(err, target error) bool {
51+
if target == nil {
52+
return err == target
53+
}
54+
for {
55+
if err == target {
56+
return true
57+
}
58+
if x, ok := err.(interface{ Is(error) bool }); ok && x.Is(target) {
59+
return true
60+
}
61+
// TODO: consider supporing target.Is(err). This would allow
62+
// user-definable predicates, but also may allow for coping with sloppy
63+
// APIs, thereby making it easier to get away with them.
64+
if err = Unwrap(err); err == nil {
65+
return false
66+
}
67+
}
68+
}
69+
70+
// As finds the first error in err's chain that matches the type to which target
71+
// points, and if so, sets the target to its value and returns true. An error
72+
// matches a type if it is assignable to the target type, or if it has a method
73+
// As(interface{}) bool such that As(target) returns true. As will panic if
74+
// target is not a non-nil pointer to a type which implements error or is of
75+
// interface type.
76+
//
77+
// The As method should set the target to its value and return true if err
78+
// matches the type to which target points.
79+
func As(err error, target interface{}) bool {
80+
if target == nil {
81+
panic("errors: target cannot be nil")
82+
}
83+
val := reflectlite.ValueOf(target)
84+
typ := val.Type()
85+
if typ.Kind() != reflectlite.Ptr || val.IsNil() {
86+
panic("errors: target must be a non-nil pointer")
87+
}
88+
if e := typ.Elem(); e.Kind() != reflectlite.Interface && !e.Implements(errorType) {
89+
panic("errors: *target must be interface or implement error")
90+
}
91+
targetType := typ.Elem()
92+
for {
93+
if reflectlite.TypeOf(err).AssignableTo(targetType) {
94+
val.Elem().Set(reflectlite.ValueOf(err))
95+
return true
96+
}
97+
if x, ok := err.(interface{ As(interface{}) bool }); ok && x.As(target) {
98+
return true
99+
}
100+
if err = Unwrap(err); err == nil {
101+
return false
102+
}
103+
}
104+
}
105+
106+
var errorType = reflectlite.TypeOf((*error)(nil)).Elem()

src/errors/wrap_test.go

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
// Copyright 2018 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package errors_test
6+
7+
import (
8+
"bytes"
9+
"errors"
10+
"fmt"
11+
"os"
12+
"testing"
13+
)
14+
15+
func TestIs(t *testing.T) {
16+
err1 := errors.New("1")
17+
erra := wrapped{"wrap 2", err1}
18+
errb := wrapped{"wrap 3", erra}
19+
erro := errors.Opaque(err1)
20+
errco := wrapped{"opaque", erro}
21+
22+
err3 := errors.New("3")
23+
24+
poser := &poser{"either 1 or 3", func(err error) bool {
25+
return err == err1 || err == err3
26+
}}
27+
28+
testCases := []struct {
29+
err error
30+
target error
31+
match bool
32+
}{
33+
{nil, nil, true},
34+
{err1, nil, false},
35+
{err1, err1, true},
36+
{erra, err1, true},
37+
{errb, err1, true},
38+
{errco, erro, true},
39+
{errco, err1, false},
40+
{erro, erro, true},
41+
{err1, err3, false},
42+
{erra, err3, false},
43+
{errb, err3, false},
44+
{poser, err1, true},
45+
{poser, err3, true},
46+
{poser, erra, false},
47+
{poser, errb, false},
48+
{poser, erro, false},
49+
{poser, errco, false},
50+
}
51+
for _, tc := range testCases {
52+
t.Run("", func(t *testing.T) {
53+
if got := errors.Is(tc.err, tc.target); got != tc.match {
54+
t.Errorf("Is(%v, %v) = %v, want %v", tc.err, tc.target, got, tc.match)
55+
}
56+
})
57+
}
58+
}
59+
60+
type poser struct {
61+
msg string
62+
f func(error) bool
63+
}
64+
65+
func (p *poser) Error() string { return p.msg }
66+
func (p *poser) Is(err error) bool { return p.f(err) }
67+
func (p *poser) As(err interface{}) bool {
68+
switch x := err.(type) {
69+
case **poser:
70+
*x = p
71+
case *errorT:
72+
*x = errorT{}
73+
case **os.PathError:
74+
*x = &os.PathError{}
75+
default:
76+
return false
77+
}
78+
return true
79+
}
80+
81+
func TestAs(t *testing.T) {
82+
var errT errorT
83+
var errP *os.PathError
84+
var timeout interface{ Timeout() bool }
85+
var p *poser
86+
_, errF := os.Open("non-existing")
87+
88+
testCases := []struct {
89+
err error
90+
target interface{}
91+
match bool
92+
}{{
93+
wrapped{"pittied the fool", errorT{}},
94+
&errT,
95+
true,
96+
}, {
97+
errF,
98+
&errP,
99+
true,
100+
}, {
101+
errors.Opaque(errT),
102+
&errT,
103+
false,
104+
}, {
105+
errorT{},
106+
&errP,
107+
false,
108+
}, {
109+
wrapped{"wrapped", nil},
110+
&errT,
111+
false,
112+
}, {
113+
&poser{"error", nil},
114+
&errT,
115+
true,
116+
}, {
117+
&poser{"path", nil},
118+
&errP,
119+
true,
120+
}, {
121+
&poser{"oh no", nil},
122+
&p,
123+
true,
124+
}, {
125+
errors.New("err"),
126+
&timeout,
127+
false,
128+
}, {
129+
errF,
130+
&timeout,
131+
true,
132+
}, {
133+
wrapped{"path error", errF},
134+
&timeout,
135+
true,
136+
}}
137+
for i, tc := range testCases {
138+
name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.err, tc.target)
139+
t.Run(name, func(t *testing.T) {
140+
match := errors.As(tc.err, tc.target)
141+
if match != tc.match {
142+
t.Fatalf("match: got %v; want %v", match, tc.match)
143+
}
144+
if !match {
145+
return
146+
}
147+
if tc.target == nil {
148+
t.Fatalf("non-nil result after match")
149+
}
150+
})
151+
}
152+
}
153+
154+
func TestAsValidation(t *testing.T) {
155+
var s string
156+
testCases := []interface{}{
157+
nil,
158+
(*int)(nil),
159+
"error",
160+
&s,
161+
}
162+
err := errors.New("error")
163+
for _, tc := range testCases {
164+
t.Run(fmt.Sprintf("%T(%v)", tc, tc), func(t *testing.T) {
165+
defer func() {
166+
recover()
167+
}()
168+
if errors.As(err, tc) {
169+
t.Errorf("As(err, %T(%v)) = true, want false", tc, tc)
170+
return
171+
}
172+
t.Errorf("As(err, %T(%v)) did not panic", tc, tc)
173+
})
174+
}
175+
}
176+
177+
func TestUnwrap(t *testing.T) {
178+
err1 := errors.New("1")
179+
erra := wrapped{"wrap 2", err1}
180+
erro := errors.Opaque(err1)
181+
182+
testCases := []struct {
183+
err error
184+
want error
185+
}{
186+
{nil, nil},
187+
{wrapped{"wrapped", nil}, nil},
188+
{err1, nil},
189+
{erra, err1},
190+
{wrapped{"wrap 3", erra}, erra},
191+
192+
{erro, nil},
193+
{wrapped{"opaque", erro}, erro},
194+
}
195+
for _, tc := range testCases {
196+
if got := errors.Unwrap(tc.err); got != tc.want {
197+
t.Errorf("Unwrap(%v) = %v, want %v", tc.err, got, tc.want)
198+
}
199+
}
200+
}
201+
202+
func TestOpaque(t *testing.T) {
203+
someError := errors.New("some error")
204+
testCases := []struct {
205+
err error
206+
next error
207+
}{
208+
{errorT{}, nil},
209+
{wrapped{"b", nil}, nil},
210+
{wrapped{"c", someError}, someError},
211+
}
212+
for _, tc := range testCases {
213+
t.Run("", func(t *testing.T) {
214+
opaque := errors.Opaque(tc.err)
215+
216+
f, ok := opaque.(errors.Formatter)
217+
if !ok {
218+
t.Fatal("Opaque error does not implement Formatter")
219+
}
220+
var p printer
221+
next := f.FormatError(&p)
222+
if next != tc.next {
223+
t.Errorf("next was %v; want %v", next, tc.next)
224+
}
225+
if got, want := p.buf.String(), tc.err.Error(); got != want {
226+
t.Errorf("error was %q; want %q", got, want)
227+
}
228+
if got := errors.Unwrap(opaque); got != nil {
229+
t.Errorf("Unwrap returned non-nil error (%v)", got)
230+
}
231+
})
232+
}
233+
}
234+
235+
type errorT struct{}
236+
237+
func (errorT) Error() string { return "errorT" }
238+
239+
type wrapped struct {
240+
msg string
241+
err error
242+
}
243+
244+
func (e wrapped) Error() string { return e.msg }
245+
246+
func (e wrapped) Unwrap() error { return e.err }
247+
248+
func (e wrapped) FormatError(p errors.Printer) error {
249+
p.Print(e.msg)
250+
return e.err
251+
}
252+
253+
type printer struct {
254+
errors.Printer
255+
buf bytes.Buffer
256+
}
257+
258+
func (p *printer) Print(args ...interface{}) { fmt.Fprint(&p.buf, args...) }

0 commit comments

Comments
 (0)