OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package errors |
6 | 6 |
7 // Transient is an Error implementation. It wraps an existing Error, marking | 7 // Transient is an Error implementation. It wraps an existing Error, marking |
8 // it as transient. This can be tested with IsTransient. | 8 // it as transient. This can be tested with IsTransient. |
9 type Transient interface { | 9 type Transient interface { |
10 error | 10 error |
11 | 11 |
12 // IsTransient returns true if this error type is transient. | 12 // IsTransient returns true if this error type is transient. |
13 IsTransient() bool | 13 IsTransient() bool |
14 } | 14 } |
15 | 15 |
16 type transientWrapper struct { | 16 type transientWrapper struct { |
17 error | 17 error |
18 } | 18 } |
19 | 19 |
| 20 var _ Transient = transientWrapper{} |
| 21 var _ Wrap = transientWrapper{} |
| 22 |
20 func (t transientWrapper) IsTransient() bool { | 23 func (t transientWrapper) IsTransient() bool { |
21 return true | 24 return true |
22 } | 25 } |
23 | 26 |
| 27 func (t transientWrapper) InnerError() error { |
| 28 return t.error |
| 29 } |
| 30 |
24 // IsTransient tests if a given error is Transient. | 31 // IsTransient tests if a given error is Transient. |
25 func IsTransient(err error) bool { | 32 func IsTransient(err error) bool { |
26 if t, ok := err.(Transient); ok { | 33 if t, ok := err.(Transient); ok { |
27 return t.IsTransient() | 34 return t.IsTransient() |
28 } | 35 } |
29 return false | 36 return false |
30 } | 37 } |
31 | 38 |
32 // WrapTransient wraps an existing error with in a Transient error. | 39 // WrapTransient wraps an existing error with in a Transient error. |
33 // | 40 // |
34 // If the supplied error is already Transient, it will be returned. If the | 41 // If the supplied error is already Transient, it will be returned. If the |
35 // supplied error is nil, nil wil be returned. | 42 // supplied error is nil, nil wil be returned. |
36 func WrapTransient(err error) error { | 43 func WrapTransient(err error) error { |
37 if err == nil || IsTransient(err) { | 44 if err == nil || IsTransient(err) { |
38 return err | 45 return err |
39 } | 46 } |
40 return transientWrapper{err} | 47 return transientWrapper{err} |
41 } | 48 } |
OLD | NEW |