OLD | NEW |
| (Empty) |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | |
2 // Use of this source code is governed under the Apache License, Version 2.0 | |
3 // that can be found in the LICENSE file. | |
4 | |
5 package errors | |
6 | |
7 // Transient is an Error implementation. It wraps an existing Error, marking | |
8 // it as transient. This can be tested with IsTransient. | |
9 type Transient interface { | |
10 error | |
11 | |
12 // IsTransient returns true if this error type is transient. | |
13 IsTransient() bool | |
14 } | |
15 | |
16 type transientWrapper struct { | |
17 error | |
18 finfo StackFrameInfo | |
19 } | |
20 | |
21 var _ interface { | |
22 Transient | |
23 StackContexter | |
24 } = transientWrapper{} | |
25 | |
26 func (t transientWrapper) IsTransient() bool { | |
27 return true | |
28 } | |
29 | |
30 func (t transientWrapper) InnerError() error { | |
31 return t.error | |
32 } | |
33 | |
34 func (t transientWrapper) StackContext() StackContext { | |
35 return StackContext{ | |
36 FrameInfo: t.finfo, | |
37 InternalReason: "errors.WrapTransient()", | |
38 } | |
39 } | |
40 | |
41 // IsTransient tests if a given error or, if it is a container, any of its | |
42 // contained errors is Transient. | |
43 func IsTransient(err error) bool { | |
44 return Any(err, func(err error) bool { | |
45 if t, ok := err.(Transient); ok { | |
46 return t.IsTransient() | |
47 } | |
48 return false | |
49 }) | |
50 } | |
51 | |
52 // WrapTransient wraps an existing error with in a Transient error. | |
53 // | |
54 // If the supplied error is already Transient, it will be returned. If the | |
55 // supplied error is nil, nil wil be returned. | |
56 // | |
57 // If the supplied error is not Transient, the current stack frame will be | |
58 // captured. This wrapping action will show up on the stack trace returned by | |
59 // RenderStack. | |
60 func WrapTransient(err error) error { | |
61 if err == nil || IsTransient(err) { | |
62 return err | |
63 } | |
64 return transientWrapper{err, StackFrameInfoForError(1, err)} | |
65 } | |
OLD | NEW |