Debian Codegraph @

fmt/errors/alias.go

raw symbols SCIP
1 package errors
2
3 import (
4 "gopkg.in/errgo.v2/errors"
5 )
6
7 type (
8 Causer = errors.Causer
9 Wrapper = errors.Wrapper
10 Locator = errors.Locator
11 )
12
13 // Deprecated: Locationer is the old name for Locator,
14 // kept for backward compatibility only.
15 type Locationer = errors.Locationer
16
17 // New returns a new error with the given error message and no cause. It
18 // is a drop-in replacement for errors.New from the standard library.
19 func New(s string) error {
20 err := errors.New(s)
21 errors.SetLocation(err, 1)
22 return err
23 }
24
25 // Note returns a new error that wraps the given error
26 // and holds information about the caller. It preserves the cause if
27 // shouldPreserveCause is non-nil and shouldPreserveCause(err) is true. If msg is
28 // non-empty, it is used to prefix the returned error's string value.
29 //
30 // If err is nil, Note returns nil without calling shouldPreserveCause.
31 func Note(err error, shouldPreserveCause func(error) bool, msg string) error {
32 err = errors.Note(err, shouldPreserveCause, msg)
33 errors.SetLocation(err, 1)
34 return err
35 }
36
37 // Because returns a new error that wraps err and has the given cause,
38 // and adds the given message.
39 //
40 // If err is nil and msg is empty, the returned error's message will be
41 // the same as cause's. This is equivalent to calling errors.Note(cause,
42 // errors.Is(cause), "") and is useful for annotating an global error
43 // value with source location information.
44 //
45 // Because returns a nil error if all of err, cause and msg are
46 // zero.
47 func Because(err, cause error, msg string) error {
48 err = errors.Because(err, cause, msg)
49 errors.SetLocation(err, 1)
50 return err
51 }
52
53 // Wrap returns a new error that wraps the given error and holds
54 // information about the caller. It is equivalent to Note(err, nil, "").
55 // Note that this means that the returned error has no cause.
56 func Wrap(err error) error {
57 err = errors.Wrap(err)
58 errors.SetLocation(err, 1)
59 return err
60 }
61
62 // Cause returns the cause of the given error. If err does not
63 // implement Causer or its Cause method returns nil, it returns err itself.
64 //
65 // Cause is the usual way to diagnose errors that may have
66 // been wrapped.
67 func Cause(err error) error {
68 return errors.Cause(err)
69 }
70
71 // SetLocation sets the location of the error to the file and line number
72 // of the code that is running callDepth frames above
73 // the caller. It does nothing if the error was not created
74 // by this package. SetLocation should only be called
75 // mmediately after an error has been created, before
76 // it is shared with other code which could access it
77 // concurrently.
78 func SetLocation(err error, callDepth int) {
79 errors.SetLocation(err, callDepth+1)
80 }
81
82 // Details returns information about the stack of
83 // underlying errors wrapped by err, in the format:
84 //
85 // [{filename:99: error one} {otherfile:55: cause of error one}]
86 //
87 // The details are found by type-asserting the error to
88 // the Locator, Causer and Wrapper interfaces.
89 // Details of the underlying stack are found by
90 // recursively calling Underlying when the
91 // underlying error implements Wrapper.
92 func Details(err error) string {
93 return errors.Details(err)
94 }
95
96 // Is returns a function that returns whether the
97 // an error is equal to the given error.
98 // It is intended to be used as a "shouldPreserveCause" argument
99 // to Note. For example:
100 //
101 // return errgo.Note(err, errgo.Is(http.ErrNoCookie), "")
102 //
103 // would return an error with an http.ErrNoCookie cause
104 // only if that was err's cause.
105 func Is(err error) func(error) bool {
106 return errors.Is(err)
107 }
108
109 // Any returns true. It can be used as an argument to Note
110 // to allow any cause to pass through to the wrapped
111 // error.
112 func Any(err error) bool {
113 return errors.Any(err)
114 }