| 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 _ interface { |
| 21 Transient |
| 22 Wrapped |
| 23 } = transientWrapper{} |
| 24 |
| 20 func (t transientWrapper) IsTransient() bool { | 25 func (t transientWrapper) IsTransient() bool { |
| 21 return true | 26 return true |
| 22 } | 27 } |
| 23 | 28 |
| 29 func (t transientWrapper) InnerError() error { |
| 30 return t.error |
| 31 } |
| 32 |
| 24 // IsTransient tests if a given error is Transient. | 33 // IsTransient tests if a given error is Transient. |
| 25 func IsTransient(err error) bool { | 34 func IsTransient(err error) bool { |
| 26 if t, ok := err.(Transient); ok { | 35 if t, ok := err.(Transient); ok { |
| 27 return t.IsTransient() | 36 return t.IsTransient() |
| 28 } | 37 } |
| 29 return false | 38 return false |
| 30 } | 39 } |
| 31 | 40 |
| 32 // WrapTransient wraps an existing error with in a Transient error. | 41 // WrapTransient wraps an existing error with in a Transient error. |
| 33 // | 42 // |
| 34 // If the supplied error is already Transient, it will be returned. If the | 43 // If the supplied error is already Transient, it will be returned. If the |
| 35 // supplied error is nil, nil wil be returned. | 44 // supplied error is nil, nil wil be returned. |
| 36 func WrapTransient(err error) error { | 45 func WrapTransient(err error) error { |
| 37 if err == nil || IsTransient(err) { | 46 if err == nil || IsTransient(err) { |
| 38 return err | 47 return err |
| 39 } | 48 } |
| 40 return transientWrapper{err} | 49 return transientWrapper{err} |
| 41 } | 50 } |
| OLD | NEW |