Debian Codegraph @

wrap.go

raw
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
6
7 import "errors"
8
9 // A Wrapper provides context around another error.
10 type Wrapper interface {
11 // Unwrap returns the next error in the error chain.
12 // If there is no next error, Unwrap returns nil.
13 Unwrap() error
14 }
15
16 // Opaque returns an error with the same error formatting as err
17 // but that does not match err and cannot be unwrapped.
18 func Opaque(err error) error {
19 return noWrapper{err}
20 }
21
22 type noWrapper struct {
23 error
24 }
25
26 func (e noWrapper) FormatError(p Printer) (next error) {
27 if f, ok := e.error.(Formatter); ok {
28 return f.FormatError(p)
29 }
30 p.Print(e.error)
31 return nil
32 }
33
34 // Unwrap returns the result of calling the Unwrap method on err, if err implements
35 // Unwrap. Otherwise, Unwrap returns nil.
36 //
37 // Unwrap only calls a method of the form "Unwrap() error".
38 // In particular Unwrap does not unwrap errors returned by [errors.Join].
39 //
40 // Deprecated: As of Go 1.13, this function simply calls [errors.Unwrap].
41 func Unwrap(err error) error { return errors.Unwrap(err) }
42
43 // Is reports whether any error in err's tree matches target.
44 //
45 // The tree consists of err itself, followed by the errors obtained by repeatedly
46 // calling its Unwrap() error or Unwrap() []error method. When err wraps multiple
47 // errors, Is examines err followed by a depth-first traversal of its children.
48 //
49 // An error is considered to match a target if it is equal to that target or if
50 // it implements a method Is(error) bool such that Is(target) returns true.
51 //
52 // An error type might provide an Is method so it can be treated as equivalent
53 // to an existing error. For example, if MyError defines
54 //
55 // func (m MyError) Is(target error) bool { return target == fs.ErrExist }
56 //
57 // then Is(MyError{}, fs.ErrExist) returns true. See [syscall.Errno.Is] for
58 // an example in the standard library. An Is method should only shallowly
59 // compare err and the target and not call [Unwrap] on either.
60 //
61 // Deprecated: As of Go 1.13, this function simply calls [errors.Is].
62 func Is(err, target error) bool { return errors.Is(err, target) }
63
64 // As finds the first error in err's tree that matches target, and if one is found,
65 // sets target to that error value and returns true. Otherwise, it returns false.
66 //
67 // The tree consists of err itself, followed by the errors obtained by repeatedly
68 // calling its Unwrap() error or Unwrap() []error method. When err wraps multiple
69 // errors, As examines err followed by a depth-first traversal of its children.
70 //
71 // An error matches target if the error's concrete value is assignable to the value
72 // pointed to by target, or if the error has a method As(any) bool such that
73 // As(target) returns true. In the latter case, the As method is responsible for
74 // setting target.
75 //
76 // An error type might provide an As method so it can be treated as if it were a
77 // different error type.
78 //
79 // As panics if target is not a non-nil pointer to either a type that implements
80 // error, or to any interface type.
81 //
82 // Deprecated: As of Go 1.13, this function simply calls [errors.As].
83 func As(err error, target any) bool { return errors.As(err, target) }