OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 ) | 9 ) |
10 | 10 |
11 func someProcessingFunction(val int) error { | 11 func someProcessingFunction(val int) error { |
12 if val == 1 { | 12 if val == 1 { |
13 // New and Reason automatically include stack information. | 13 // New and Reason automatically include stack information. |
14 » » return Reason("bad number: %(val)d").D("val", val).Err() | 14 » » return Reason("bad number: %d", val).Err() |
15 } | 15 } |
16 if err := someProcessingFunction(val - 1); err != nil { | 16 if err := someProcessingFunction(val - 1); err != nil { |
17 // correctly handles recursion | 17 // correctly handles recursion |
18 » » return Annotate(err).D("val", val).Err() | 18 » » return Annotate(err, "").InternalReason("val(%d)", val).Err() |
19 } | 19 } |
20 return nil | 20 return nil |
21 } | 21 } |
22 | 22 |
23 func someLibFunc(vals ...int) error { | 23 func someLibFunc(vals ...int) error { |
24 for i, v := range vals { | 24 for i, v := range vals { |
25 if err := someProcessingFunction(v); err != nil { | 25 if err := someProcessingFunction(v); err != nil { |
26 » » » return (Annotate(err).Reason("processing %(val)d"). | 26 » » » return Annotate(err, "processing %d", v). |
27 » » » » D("val", v). | 27 » » » » InternalReason("secret(%s)/i(%d)", "value", i).E
rr() |
28 » » » » D("i", i). | |
29 » » » » D("secret", "value", "%s").Err()) | |
30 } | 28 } |
31 } | 29 } |
32 return nil | 30 return nil |
33 } | 31 } |
34 | 32 |
35 type MiscWrappedError struct{ error } | 33 type MiscWrappedError struct{ error } |
36 | 34 |
37 func (e *MiscWrappedError) Error() string { return fmt.Sprintf("super wrappe
r(%s)", e.error.Error()) } | 35 func (e *MiscWrappedError) Error() string { return fmt.Sprintf("super wrappe
r(%s)", e.error.Error()) } |
38 func (e *MiscWrappedError) InnerError() error { return e.error } | 36 func (e *MiscWrappedError) InnerError() error { return e.error } |
39 | 37 |
40 func errorWrapper(err error) error { | 38 func errorWrapper(err error) error { |
41 if err != nil { | 39 if err != nil { |
42 err = &MiscWrappedError{err} | 40 err = &MiscWrappedError{err} |
43 } | 41 } |
44 return err | 42 return err |
45 } | 43 } |
46 | 44 |
47 func someIntermediateFunc(vals ...int) error { | 45 func someIntermediateFunc(vals ...int) error { |
48 errch := make(chan error) | 46 errch := make(chan error) |
49 go func() { | 47 go func() { |
50 defer close(errch) | 48 defer close(errch) |
51 » » errch <- Annotate(errorWrapper(someLibFunc(vals...))).Reason("co
uld not process").Err() | 49 » » errch <- Annotate(errorWrapper(someLibFunc(vals...)), "could not
process").Err() |
52 }() | 50 }() |
53 me := MultiError(nil) | 51 me := MultiError(nil) |
54 for err := range errch { | 52 for err := range errch { |
55 if err != nil { | 53 if err != nil { |
56 me = append(me, err) | 54 me = append(me, err) |
57 } | 55 } |
58 } | 56 } |
59 if me != nil { | 57 if me != nil { |
60 » » return Annotate(me).Reason("while processing %(vals)v").D("vals"
, vals).Err() | 58 » » return Annotate(me, "while processing %v", vals).Err() |
61 } | 59 } |
62 return nil | 60 return nil |
63 } | 61 } |
64 | 62 |
65 func ExampleAnnotate() { | 63 func ExampleAnnotate() { |
66 if err := someIntermediateFunc(3); err != nil { | 64 if err := someIntermediateFunc(3); err != nil { |
67 » » err = Annotate(err).Reason("top level").Err() | 65 » » err = Annotate(err, "top level").Err() |
68 fmt.Println("Public-facing error:\n ", err) | 66 fmt.Println("Public-facing error:\n ", err) |
69 fmt.Println("\nfull error:") | 67 fmt.Println("\nfull error:") |
70 » » for _, l := range FixForTest(RenderStack(err).ToLines("runtime",
"_test")) { | 68 » » for _, l := range FixForTest(RenderStack(err, "runtime", "_test"
)) { |
71 fmt.Println(l) | 69 fmt.Println(l) |
72 } | 70 } |
73 } | 71 } |
74 | 72 |
75 // Output: | 73 // Output: |
76 // Public-facing error: | 74 // Public-facing error: |
77 // top level: while processing [3]: could not process: super wrapper(p
rocessing 3: bad number: 1) | 75 // top level: while processing [3]: could not process: super wrapper(p
rocessing 3: bad number: 1) |
78 // | 76 // |
79 // full error: | 77 // full error: |
80 // GOROUTINE LINE | 78 // GOROUTINE LINE |
81 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:14
- errors.someProcessingFunction() | 79 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:14
- errors.someProcessingFunction() |
82 » // reason: "bad number: 1" | 80 » // reason: bad number: 1 |
83 » // "val" = 1 | |
84 // | 81 // |
85 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:16
- errors.someProcessingFunction() | 82 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:16
- errors.someProcessingFunction() |
86 » // "val" = 2 | 83 » // internal reason: val(2) |
87 // | 84 // |
88 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:16
- errors.someProcessingFunction() | 85 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:16
- errors.someProcessingFunction() |
89 » // "val" = 3 | 86 » // internal reason: val(3) |
90 // | 87 // |
91 // From frame 2 to 3, the following wrappers were found: | 88 // From frame 2 to 3, the following wrappers were found: |
92 // unknown wrapper *errors.MiscWrappedError | 89 // unknown wrapper *errors.MiscWrappedError |
93 // | 90 // |
94 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:25
- errors.someLibFunc() | 91 // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:25
- errors.someLibFunc() |
95 » // reason: "processing 3" | 92 » // reason: processing 3 |
96 » // "i" = 0 | 93 » // internal reason: secret(value)/i(0) |
97 » // "secret" = value | |
98 » // "val" = 3 | |
99 // | 94 // |
100 // From frame 3 to 4, the following wrappers were found: | 95 // From frame 3 to 4, the following wrappers were found: |
101 » // MultiError 1/1: following first non-nil error. | 96 » // internal reason: MultiError 1/1: following first non-nil error. |
102 » // "non-nil" = 1 | |
103 » // "total" = 1 | |
104 // | 97 // |
105 » // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:51
- errors.someIntermediateFunc.func1() | 98 » // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:49
- errors.someIntermediateFunc.func1() |
106 » // reason: "could not process" | 99 » // reason: could not process |
107 // | 100 // |
108 // ... skipped SOME frames in pkg "runtime"... | 101 // ... skipped SOME frames in pkg "runtime"... |
109 // | 102 // |
110 // GOROUTINE LINE | 103 // GOROUTINE LINE |
111 » // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:60
- errors.someIntermediateFunc() | 104 » // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:58
- errors.someIntermediateFunc() |
112 » // reason: "while processing [3]" | 105 » // reason: while processing [3] |
113 » // "vals" = []int{3} | |
114 // | 106 // |
115 » // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:66
- errors.ExampleAnnotate() | 107 » // #? github.com/luci/luci-go/common/errors/annotate_example_test.go:64
- errors.ExampleAnnotate() |
116 » // reason: "top level" | 108 » // reason: top level |
117 // | 109 // |
118 // #? testing/example.go:XXX - testing.runExample() | 110 // #? testing/example.go:XXX - testing.runExample() |
119 // #? testing/example.go:XXX - testing.runExamples() | 111 // #? testing/example.go:XXX - testing.runExamples() |
120 // #? testing/testing.go:XXX - testing.(*M).Run() | 112 // #? testing/testing.go:XXX - testing.(*M).Run() |
121 // ... skipped SOME frames in pkg "_test"... | 113 // ... skipped SOME frames in pkg "_test"... |
122 // ... skipped SOME frames in pkg "runtime"... | 114 // ... skipped SOME frames in pkg "runtime"... |
123 } | 115 } |
OLD | NEW |