| 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 xerrors_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"golang.org/x/xerrors" |
| 14 |
) |
| 15 |
|
| 16 |
func TestIs(t *testing.T) { |
| 17 |
err1 := xerrors.New("1") |
| 18 |
erra := xerrors.Errorf("wrap 2: %w", err1) |
| 19 |
errb := xerrors.Errorf("wrap 3: %w", erra) |
| 20 |
erro := xerrors.Opaque(err1) |
| 21 |
errco := xerrors.Errorf("opaque: %w", erro) |
| 22 |
err3 := xerrors.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 |
{nil, err1, false}, |
| 35 |
{err1, nil, false}, |
| 36 |
{err1, err1, true}, |
| 37 |
{erra, err1, true}, |
| 38 |
{errb, err1, true}, |
| 39 |
{errco, erro, true}, |
| 40 |
{errco, err1, false}, |
| 41 |
{erro, erro, true}, |
| 42 |
{err1, err3, false}, |
| 43 |
{erra, err3, false}, |
| 44 |
{errb, err3, false}, |
| 45 |
{poser, err1, true}, |
| 46 |
{poser, err3, true}, |
| 47 |
{poser, erra, false}, |
| 48 |
{poser, errb, false}, |
| 49 |
{poser, erro, false}, |
| 50 |
{poser, errco, false}, |
| 51 |
{errorUncomparable{}, errorUncomparable{}, true}, |
| 52 |
{errorUncomparable{}, &errorUncomparable{}, false}, |
| 53 |
{&errorUncomparable{}, errorUncomparable{}, true}, |
| 54 |
{&errorUncomparable{}, &errorUncomparable{}, false}, |
| 55 |
{errorUncomparable{}, err1, false}, |
| 56 |
{&errorUncomparable{}, err1, false}, |
| 57 |
} |
| 58 |
for _, tc := range testCases { |
| 59 |
t.Run("", func(t *testing.T) { |
| 60 |
if got := xerrors.Is(tc.err, tc.target); got != tc.match { |
| 61 |
t.Errorf("Is(%v, %v) = %v, want %v", tc.err, tc.target, got, tc.match) |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
type poser struct { |
| 68 |
msg string |
| 69 |
f func(error) bool |
| 70 |
} |
| 71 |
|
| 72 |
func (p *poser) Error() string { return p.msg } |
| 73 |
func (p *poser) Is(err error) bool { return p.f(err) } |
| 74 |
func (p *poser) As(err any) bool { |
| 75 |
switch x := err.(type) { |
| 76 |
case **poser: |
| 77 |
*x = p |
| 78 |
case *errorT: |
| 79 |
*x = errorT{} |
| 80 |
case **os.PathError: |
| 81 |
*x = &os.PathError{} |
| 82 |
default: |
| 83 |
return false |
| 84 |
} |
| 85 |
return true |
| 86 |
} |
| 87 |
|
| 88 |
func TestErrorsIs(t *testing.T) { |
| 89 |
var errSentinel = errors.New("sentinel") |
| 90 |
|
| 91 |
got := errors.Is(xerrors.Errorf("%w", errSentinel), errSentinel) |
| 92 |
if !got { |
| 93 |
t.Error("got false, want true") |
| 94 |
} |
| 95 |
|
| 96 |
got = errors.Is(xerrors.Errorf("%w: %s", errSentinel, "foo"), errSentinel) |
| 97 |
if !got { |
| 98 |
t.Error("got false, want true") |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func TestAs(t *testing.T) { |
| 103 |
var errT errorT |
| 104 |
var errP *os.PathError |
| 105 |
var timeout interface{ Timeout() bool } |
| 106 |
var p *poser |
| 107 |
_, errF := os.Open("non-existing") |
| 108 |
|
| 109 |
testCases := []struct { |
| 110 |
err error |
| 111 |
target any |
| 112 |
match bool |
| 113 |
}{{ |
| 114 |
nil, |
| 115 |
&errP, |
| 116 |
false, |
| 117 |
}, { |
| 118 |
xerrors.Errorf("pittied the fool: %w", errorT{}), |
| 119 |
&errT, |
| 120 |
true, |
| 121 |
}, { |
| 122 |
errF, |
| 123 |
&errP, |
| 124 |
true, |
| 125 |
}, { |
| 126 |
xerrors.Opaque(errT), |
| 127 |
&errT, |
| 128 |
false, |
| 129 |
}, { |
| 130 |
errorT{}, |
| 131 |
&errP, |
| 132 |
false, |
| 133 |
}, { |
| 134 |
errWrap{nil}, |
| 135 |
&errT, |
| 136 |
false, |
| 137 |
}, { |
| 138 |
&poser{"error", nil}, |
| 139 |
&errT, |
| 140 |
true, |
| 141 |
}, { |
| 142 |
&poser{"path", nil}, |
| 143 |
&errP, |
| 144 |
true, |
| 145 |
}, { |
| 146 |
&poser{"oh no", nil}, |
| 147 |
&p, |
| 148 |
true, |
| 149 |
}, { |
| 150 |
xerrors.New("err"), |
| 151 |
&timeout, |
| 152 |
false, |
| 153 |
}, { |
| 154 |
errF, |
| 155 |
&timeout, |
| 156 |
true, |
| 157 |
}, { |
| 158 |
xerrors.Errorf("path error: %w", errF), |
| 159 |
&timeout, |
| 160 |
true, |
| 161 |
}} |
| 162 |
for i, tc := range testCases { |
| 163 |
name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.err, tc.target) |
| 164 |
t.Run(name, func(t *testing.T) { |
| 165 |
match := xerrors.As(tc.err, tc.target) |
| 166 |
if match != tc.match { |
| 167 |
t.Fatalf("xerrors.As(%T, %T): got %v; want %v", tc.err, tc.target, match, tc.match) |
| 168 |
} |
| 169 |
if !match { |
| 170 |
return |
| 171 |
} |
| 172 |
if tc.target == nil { |
| 173 |
t.Fatalf("non-nil result after match") |
| 174 |
} |
| 175 |
}) |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
func TestAsValidation(t *testing.T) { |
| 180 |
var s string |
| 181 |
testCases := []any{ |
| 182 |
nil, |
| 183 |
(*int)(nil), |
| 184 |
"error", |
| 185 |
&s, |
| 186 |
} |
| 187 |
err := xerrors.New("error") |
| 188 |
for _, tc := range testCases { |
| 189 |
t.Run(fmt.Sprintf("%T(%v)", tc, tc), func(t *testing.T) { |
| 190 |
defer func() { |
| 191 |
recover() |
| 192 |
}() |
| 193 |
if xerrors.As(err, tc) { |
| 194 |
t.Errorf("As(err, %T(%v)) = true, want false", tc, tc) |
| 195 |
return |
| 196 |
} |
| 197 |
t.Errorf("As(err, %T(%v)) did not panic", tc, tc) |
| 198 |
}) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
func TestUnwrap(t *testing.T) { |
| 203 |
err1 := xerrors.New("1") |
| 204 |
erra := xerrors.Errorf("wrap 2: %w", err1) |
| 205 |
erro := xerrors.Opaque(err1) |
| 206 |
|
| 207 |
testCases := []struct { |
| 208 |
err error |
| 209 |
want error |
| 210 |
}{ |
| 211 |
{nil, nil}, |
| 212 |
{errWrap{nil}, nil}, |
| 213 |
{err1, nil}, |
| 214 |
{erra, err1}, |
| 215 |
{xerrors.Errorf("wrap 3: %w", erra), erra}, |
| 216 |
|
| 217 |
{erro, nil}, |
| 218 |
{xerrors.Errorf("opaque: %w", erro), erro}, |
| 219 |
} |
| 220 |
for _, tc := range testCases { |
| 221 |
if got := xerrors.Unwrap(tc.err); got != tc.want { |
| 222 |
t.Errorf("Unwrap(%v) = %v, want %v", tc.err, got, tc.want) |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestOpaque(t *testing.T) { |
| 228 |
got := fmt.Sprintf("%v", xerrors.Errorf("foo: %v", xerrors.Opaque(errorT{}))) |
| 229 |
want := "foo: errorT" |
| 230 |
if got != want { |
| 231 |
t.Errorf("error without Format: got %v; want %v", got, want) |
| 232 |
} |
| 233 |
|
| 234 |
got = fmt.Sprintf("%v", xerrors.Errorf("foo: %v", xerrors.Opaque(errorD{}))) |
| 235 |
want = "foo: errorD" |
| 236 |
if got != want { |
| 237 |
t.Errorf("error with Format: got %v; want %v", got, want) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
type errorT struct{} |
| 242 |
|
| 243 |
func (errorT) Error() string { return "errorT" } |
| 244 |
|
| 245 |
type errorD struct{} |
| 246 |
|
| 247 |
func (errorD) Error() string { return "errorD" } |
| 248 |
|
| 249 |
func (errorD) FormatError(p xerrors.Printer) error { |
| 250 |
p.Print("errorD") |
| 251 |
p.Detail() |
| 252 |
p.Print("detail") |
| 253 |
return nil |
| 254 |
} |
| 255 |
|
| 256 |
type errWrap struct{ error } |
| 257 |
|
| 258 |
func (errWrap) Error() string { return "wrapped" } |
| 259 |
|
| 260 |
func (errWrap) Unwrap() error { return nil } |
| 261 |
|
| 262 |
type errorUncomparable struct { |
| 263 |
f []string |
| 264 |
} |
| 265 |
|
| 266 |
func (errorUncomparable) Error() string { |
| 267 |
return "uncomparable error" |
| 268 |
} |
| 269 |
|
| 270 |
func (errorUncomparable) Is(target error) bool { |
| 271 |
_, ok := target.(errorUncomparable) |
| 272 |
return ok |
| 273 |
} |