aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/pkg/errors/errors.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/pkg/errors/errors.go')
-rw-r--r--vendor/github.com/pkg/errors/errors.go134
1 files changed, 79 insertions, 55 deletions
diff --git a/vendor/github.com/pkg/errors/errors.go b/vendor/github.com/pkg/errors/errors.go
index 6c45c8d..1c9731a 100644
--- a/vendor/github.com/pkg/errors/errors.go
+++ b/vendor/github.com/pkg/errors/errors.go
@@ -28,7 +28,7 @@
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
-// type Causer interface {
+// type causer interface {
// Cause() error
// }
//
@@ -43,6 +43,9 @@
// // unknown error
// }
//
+// causer interface is not exported by this package, but is considered a part
+// of stable public API.
+//
// Formatted printing of errors
//
// All error values returned from this package implement fmt.Formatter and can
@@ -77,6 +80,9 @@
// }
// }
//
+// stackTracer interface is not exported by this package, but is considered a part
+// of stable public API.
+//
// See the documentation for Frame.Format for more details.
package errors
@@ -85,68 +91,62 @@ import (
"io"
)
-// _error is an error implementation returned by New and Errorf
-// that implements its own fmt.Formatter.
-type _error struct {
+// New returns an error with the supplied message.
+// New also records the stack trace at the point it was called.
+func New(message string) error {
+ return &fundamental{
+ msg: message,
+ stack: callers(),
+ }
+}
+
+// Errorf formats according to a format specifier and returns the string
+// as a value that satisfies error.
+// Errorf also records the stack trace at the point it was called.
+func Errorf(format string, args ...interface{}) error {
+ return &fundamental{
+ msg: fmt.Sprintf(format, args...),
+ stack: callers(),
+ }
+}
+
+// fundamental is an error that has a message and a stack, but no caller.
+type fundamental struct {
msg string
*stack
}
-func (e _error) Error() string { return e.msg }
+func (f *fundamental) Error() string { return f.msg }
-func (e _error) Format(s fmt.State, verb rune) {
+func (f *fundamental) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
- io.WriteString(s, e.msg)
- fmt.Fprintf(s, "%+v", e.StackTrace())
+ io.WriteString(s, f.msg)
+ f.stack.Format(s, verb)
return
}
fallthrough
case 's':
- io.WriteString(s, e.msg)
- }
-}
-
-// New returns an error with the supplied message.
-func New(message string) error {
- return _error{
- message,
- callers(),
- }
-}
-
-// Errorf formats according to a format specifier and returns the string
-// as a value that satisfies error.
-func Errorf(format string, args ...interface{}) error {
- return _error{
- fmt.Sprintf(format, args...),
- callers(),
+ io.WriteString(s, f.msg)
+ case 'q':
+ fmt.Fprintf(s, "%q", f.msg)
}
}
-type cause struct {
- cause error
- msg string
-}
-
-func (c cause) Error() string { return fmt.Sprintf("%s: %v", c.msg, c.Cause()) }
-func (c cause) Cause() error { return c.cause }
-
-// wrapper is an error implementation returned by Wrap and Wrapf
-// that implements its own fmt.Formatter.
-type wrapper struct {
- cause
+type withStack struct {
+ error
*stack
}
-func (w wrapper) Format(s fmt.State, verb rune) {
+func (w *withStack) Cause() error { return w.error }
+
+func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
- fmt.Fprintf(s, "%+v\n", w.Cause())
- io.WriteString(s, w.msg)
- fmt.Fprintf(s, "%+v", w.StackTrace())
+ fmt.Fprintf(s, "%+v", w.Cause())
+ w.stack.Format(s, verb)
return
}
fallthrough
@@ -163,12 +163,13 @@ func Wrap(err error, message string) error {
if err == nil {
return nil
}
- return wrapper{
- cause: cause{
- cause: err,
- msg: message,
- },
- stack: callers(),
+ err = &withMessage{
+ cause: err,
+ msg: message,
+ }
+ return &withStack{
+ err,
+ callers(),
}
}
@@ -178,12 +179,35 @@ func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
- return wrapper{
- cause: cause{
- cause: err,
- msg: fmt.Sprintf(format, args...),
- },
- stack: callers(),
+ err = &withMessage{
+ cause: err,
+ msg: fmt.Sprintf(format, args...),
+ }
+ return &withStack{
+ err,
+ callers(),
+ }
+}
+
+type withMessage struct {
+ cause error
+ msg string
+}
+
+func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
+func (w *withMessage) Cause() error { return w.cause }
+
+func (w *withMessage) Format(s fmt.State, verb rune) {
+ switch verb {
+ case 'v':
+ if s.Flag('+') {
+ fmt.Fprintf(s, "%+v\n", w.Cause())
+ io.WriteString(s, w.msg)
+ return
+ }
+ fallthrough
+ case 's', 'q':
+ io.WriteString(s, w.Error())
}
}
@@ -191,7 +215,7 @@ func Wrapf(err error, format string, args ...interface{}) error {
// An error value has a cause if it implements the following
// interface:
//
-// type Causer interface {
+// type causer interface {
// Cause() error
// }
//