Index: common/errors/wrap.go |
diff --git a/common/errors/wrap.go b/common/errors/wrap.go |
new file mode 100644 |
index 0000000000000000000000000000000000000000..806b404822852266b613be05f16097da277cd33a |
--- /dev/null |
+++ b/common/errors/wrap.go |
@@ -0,0 +1,33 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package errors |
+ |
+// Wrap wraps an error. |
+type Wrap interface { |
+ // InnerError returns the wrapped error. |
+ InnerError() error |
+} |
+ |
+// Unwrap returns the inner error of err. |
+// Returns nil if err does not wrap anything or err is nil. |
+func Unwrap(err error) error { |
+ if wrap, ok := err.(Wrap); ok { |
+ return wrap.InnerError() |
+ } |
+ return nil |
+} |
+ |
+// UnwrapAll unwraps a wrapped error recursively. |
+// Returns nil iff err is nil. |
+func UnwrapAll(err error) error { |
+ for { |
+ inner := Unwrap(err) |
+ if inner == nil { |
+ break |
+ } |
+ err = inner |
+ } |
+ return err |
+} |