|
@@ -3,107 +3,110 @@ package errors
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
|
|
+ "io"
|
|
|
|
|
+ "os"
|
|
|
"runtime"
|
|
"runtime"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
// New returns an error that formats as the given text.
|
|
// New returns an error that formats as the given text.
|
|
|
-func New(message string) error {
|
|
|
|
|
- pc, _, _, _ := runtime.Caller(1) // the caller of New
|
|
|
|
|
|
|
+func New(text string) error {
|
|
|
return struct {
|
|
return struct {
|
|
|
error
|
|
error
|
|
|
pc uintptr
|
|
pc uintptr
|
|
|
}{
|
|
}{
|
|
|
- fmt.Errorf(message),
|
|
|
|
|
- pc,
|
|
|
|
|
|
|
+ fmt.Errorf(text),
|
|
|
|
|
+ pc(),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// Errorf returns a formatted error.
|
|
|
|
|
-func Errorf(format string, args ...interface{}) error {
|
|
|
|
|
- pc, _, _, _ := runtime.Caller(1) // the caller of Errorf
|
|
|
|
|
- return struct {
|
|
|
|
|
- error
|
|
|
|
|
- pc uintptr
|
|
|
|
|
- }{
|
|
|
|
|
- fmt.Errorf(format, args...),
|
|
|
|
|
- pc,
|
|
|
|
|
|
|
+type e struct {
|
|
|
|
|
+ cause error
|
|
|
|
|
+ message string
|
|
|
|
|
+ pc uintptr
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (e *e) Error() string {
|
|
|
|
|
+ return e.message + ": " + e.cause.Error()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (e *e) Cause() error {
|
|
|
|
|
+ return e.cause
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// Wrap returns an error annotating the cause with message.
|
|
|
|
|
+// If cause is nil, Wrap returns nil.
|
|
|
|
|
+func Wrap(cause error, message string) error {
|
|
|
|
|
+ if cause == nil {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return &e{
|
|
|
|
|
+ cause: cause,
|
|
|
|
|
+ message: message,
|
|
|
|
|
+ pc: pc(),
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+type causer interface {
|
|
|
|
|
+ Cause() error
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// Cause returns the underlying cause of the error, if possible.
|
|
// Cause returns the underlying cause of the error, if possible.
|
|
|
// An error value has a cause if it implements the following
|
|
// An error value has a cause if it implements the following
|
|
|
-// method:
|
|
|
|
|
-//
|
|
|
|
|
-// Cause() error
|
|
|
|
|
|
|
+// interface:
|
|
|
//
|
|
//
|
|
|
|
|
+// type Causer interface {
|
|
|
|
|
+// Cause() error
|
|
|
|
|
+// }
|
|
|
//
|
|
//
|
|
|
// If the error does not implement Cause, the original error will
|
|
// If the error does not implement Cause, the original error will
|
|
|
// be returned. If the error is nil, nil will be returned without further
|
|
// be returned. If the error is nil, nil will be returned without further
|
|
|
// investigation.
|
|
// investigation.
|
|
|
func Cause(err error) error {
|
|
func Cause(err error) error {
|
|
|
- if err == nil {
|
|
|
|
|
- return nil
|
|
|
|
|
- }
|
|
|
|
|
- type causer interface {
|
|
|
|
|
- Cause() error
|
|
|
|
|
- }
|
|
|
|
|
- if err, ok := err.(causer); ok {
|
|
|
|
|
- return err.Cause()
|
|
|
|
|
|
|
+ for err != nil {
|
|
|
|
|
+ cause, ok := err.(causer)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ err = cause.Cause()
|
|
|
}
|
|
}
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func underlying(err error) (error, bool) {
|
|
|
|
|
- if err == nil {
|
|
|
|
|
- return nil, false
|
|
|
|
|
- }
|
|
|
|
|
- type underlying interface {
|
|
|
|
|
- underlying() error
|
|
|
|
|
- }
|
|
|
|
|
- if err, ok := err.(underlying); ok {
|
|
|
|
|
- return err.underlying(), true
|
|
|
|
|
- }
|
|
|
|
|
- return nil, false
|
|
|
|
|
|
|
+type locationer interface {
|
|
|
|
|
+ Location() (string, int)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-type traced struct {
|
|
|
|
|
- error // underlying error
|
|
|
|
|
- pc uintptr
|
|
|
|
|
|
|
+// Print prints the error to Stderr.
|
|
|
|
|
+func Print(err error) {
|
|
|
|
|
+ Fprint(os.Stderr, err)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (t *traced) underlying() error { return t.error }
|
|
|
|
|
|
|
+// Fprint prints the error to the supplied writer.
|
|
|
|
|
+// The format of the output is the same as Print.
|
|
|
|
|
+// If err is nil, nothing is printed.
|
|
|
|
|
+func Fprint(w io.Writer, err error) {
|
|
|
|
|
+ for err != nil {
|
|
|
|
|
+ location, ok := err.(locationer)
|
|
|
|
|
+ if ok {
|
|
|
|
|
+ file, line := location.Location()
|
|
|
|
|
+ fmt.Fprint(w, "%s:%d: ", file, line)
|
|
|
|
|
+ }
|
|
|
|
|
+ switch err := err.(type) {
|
|
|
|
|
+ case *e:
|
|
|
|
|
+ fmt.Fprintln(w, err.message)
|
|
|
|
|
+ default:
|
|
|
|
|
+ fmt.Fprintln(w, err.Error())
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
-// Trace adds caller information to the error.
|
|
|
|
|
-// If error is nil, nil will be returned.
|
|
|
|
|
-func Trace(err error) error {
|
|
|
|
|
- if err == nil {
|
|
|
|
|
- return nil
|
|
|
|
|
|
|
+ cause, ok := err.(causer)
|
|
|
|
|
+ if !ok {
|
|
|
|
|
+ break
|
|
|
|
|
+ }
|
|
|
|
|
+ err = cause.Cause()
|
|
|
}
|
|
}
|
|
|
- pc, _, _, _ := runtime.Caller(1) // the caller of Trace
|
|
|
|
|
- return traced{
|
|
|
|
|
- error: err,
|
|
|
|
|
- pc: pc,
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-type annotated struct {
|
|
|
|
|
- error // underlying error
|
|
|
|
|
- pc uintptr
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (a *annotated) Cause() error { return a.error }
|
|
|
|
|
-
|
|
|
|
|
-// Annotate returns a new error annotating the error provided
|
|
|
|
|
-// with the message, and the location of the caller of Annotate.
|
|
|
|
|
-// The underlying error can be recovered by calling Cause.
|
|
|
|
|
-// If err is nil, nil will be returned.
|
|
|
|
|
-func Annotate(err error, message string) error {
|
|
|
|
|
- if err == nil {
|
|
|
|
|
- return nil
|
|
|
|
|
- }
|
|
|
|
|
- pc, _, _, _ := runtime.Caller(1) // the caller of Annotate
|
|
|
|
|
- return annotated{
|
|
|
|
|
- error: err,
|
|
|
|
|
- pc: pc,
|
|
|
|
|
- }
|
|
|
|
|
|
|
+func pc() uintptr {
|
|
|
|
|
+ pc, _, _, _ := runtime.Caller(2)
|
|
|
|
|
+ return pc
|
|
|
}
|
|
}
|