4 // |
4 // |
5 // if err != nil { |
5 // if err != nil { |
6 // return err |
6 // return err |
7 // } |
7 // } |
8 // |
8 // |
9 // which applied recursively up the call stack results in error reports |
9 // which when applied recursively up the call stack results in error reports |
10 // without context or debugging information. The errors package allows |
10 // without context or debugging information. The errors package allows |
11 // programmers to add context to the failure path in their code in a way |
11 // programmers to add context to the failure path in their code in a way |
12 // that does not destroy the original value of the error. |
12 // that does not destroy the original value of the error. |
13 // |
13 // |
14 // Adding context to an error |
14 // Adding context to an error |
15 // |
15 // |
16 // The errors.Wrap function returns a new error that adds context to the |
16 // The errors.Wrap function returns a new error that adds context to the |
17 // original error by recording a stack trace at the point Wrap is called, |
17 // original error by recording a stack trace at the point Wrap is called, |
18 // and the supplied message. For example |
18 // together with the supplied message. For example |
19 // |
19 // |
20 // _, err := ioutil.ReadAll(r) |
20 // _, err := ioutil.ReadAll(r) |
21 // if err != nil { |
21 // if err != nil { |
22 // return errors.Wrap(err, "read failed") |
22 // return errors.Wrap(err, "read failed") |
23 // } |
23 // } |
24 // |
24 // |
25 // If additional control is required the errors.WithStack and errors.WithMessage |
25 // If additional control is required, the errors.WithStack and |
26 // functions destructure errors.Wrap into its component operations of annotating |
26 // errors.WithMessage functions destructure errors.Wrap into its component |
27 // an error with a stack trace and an a message, respectively. |
27 // operations: annotating an error with a stack trace and with a message, |
|
28 // respectively. |
28 // |
29 // |
29 // Retrieving the cause of an error |
30 // Retrieving the cause of an error |
30 // |
31 // |
31 // Using errors.Wrap constructs a stack of errors, adding context to the |
32 // Using errors.Wrap constructs a stack of errors, adding context to the |
32 // preceding error. Depending on the nature of the error it may be necessary |
33 // preceding error. Depending on the nature of the error it may be necessary |
36 // type causer interface { |
37 // type causer interface { |
37 // Cause() error |
38 // Cause() error |
38 // } |
39 // } |
39 // |
40 // |
40 // can be inspected by errors.Cause. errors.Cause will recursively retrieve |
41 // can be inspected by errors.Cause. errors.Cause will recursively retrieve |
41 // the topmost error which does not implement causer, which is assumed to be |
42 // the topmost error that does not implement causer, which is assumed to be |
42 // the original cause. For example: |
43 // the original cause. For example: |
43 // |
44 // |
44 // switch err := errors.Cause(err).(type) { |
45 // switch err := errors.Cause(err).(type) { |
45 // case *MyError: |
46 // case *MyError: |
46 // // handle specifically |
47 // // handle specifically |
47 // default: |
48 // default: |
48 // // unknown error |
49 // // unknown error |
49 // } |
50 // } |
50 // |
51 // |
51 // causer interface is not exported by this package, but is considered a part |
52 // Although the causer interface is not exported by this package, it is |
52 // of stable public API. |
53 // considered a part of its stable public interface. |
53 // |
54 // |
54 // Formatted printing of errors |
55 // Formatted printing of errors |
55 // |
56 // |
56 // All error values returned from this package implement fmt.Formatter and can |
57 // All error values returned from this package implement fmt.Formatter and can |
57 // be formatted by the fmt package. The following verbs are supported |
58 // be formatted by the fmt package. The following verbs are supported: |
58 // |
59 // |
59 // %s print the error. If the error has a Cause it will be |
60 // %s print the error. If the error has a Cause it will be |
60 // printed recursively |
61 // printed recursively. |
61 // %v see %s |
62 // %v see %s |
62 // %+v extended format. Each Frame of the error's StackTrace will |
63 // %+v extended format. Each Frame of the error's StackTrace will |
63 // be printed in detail. |
64 // be printed in detail. |
64 // |
65 // |
65 // Retrieving the stack trace of an error or wrapper |
66 // Retrieving the stack trace of an error or wrapper |
66 // |
67 // |
67 // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are |
68 // New, Errorf, Wrap, and Wrapf record a stack trace at the point they are |
68 // invoked. This information can be retrieved with the following interface. |
69 // invoked. This information can be retrieved with the following interface: |
69 // |
70 // |
70 // type stackTracer interface { |
71 // type stackTracer interface { |
71 // StackTrace() errors.StackTrace |
72 // StackTrace() errors.StackTrace |
72 // } |
73 // } |
73 // |
74 // |
74 // Where errors.StackTrace is defined as |
75 // The returned errors.StackTrace type is defined as |
75 // |
76 // |
76 // type StackTrace []Frame |
77 // type StackTrace []Frame |
77 // |
78 // |
78 // The Frame type represents a call site in the stack trace. Frame supports |
79 // The Frame type represents a call site in the stack trace. Frame supports |
79 // the fmt.Formatter interface that can be used for printing information about |
80 // the fmt.Formatter interface that can be used for printing information about |
83 // for _, f := range err.StackTrace() { |
84 // for _, f := range err.StackTrace() { |
84 // fmt.Printf("%+s:%d", f) |
85 // fmt.Printf("%+s:%d", f) |
85 // } |
86 // } |
86 // } |
87 // } |
87 // |
88 // |
88 // stackTracer interface is not exported by this package, but is considered a part |
89 // Although the stackTracer interface is not exported by this package, it is |
89 // of stable public API. |
90 // considered a part of its stable public interface. |
90 // |
91 // |
91 // See the documentation for Frame.Format for more details. |
92 // See the documentation for Frame.Format for more details. |
92 package errors |
93 package errors |
93 |
94 |
94 import ( |
95 import ( |
190 callers(), |
191 callers(), |
191 } |
192 } |
192 } |
193 } |
193 |
194 |
194 // Wrapf returns an error annotating err with a stack trace |
195 // Wrapf returns an error annotating err with a stack trace |
195 // at the point Wrapf is call, and the format specifier. |
196 // at the point Wrapf is called, and the format specifier. |
196 // If err is nil, Wrapf returns nil. |
197 // If err is nil, Wrapf returns nil. |
197 func Wrapf(err error, format string, args ...interface{}) error { |
198 func Wrapf(err error, format string, args ...interface{}) error { |
198 if err == nil { |
199 if err == nil { |
199 return nil |
200 return nil |
200 } |
201 } |