Documentation
¶
Index ¶
- func Annotate(other error, message string) error
- func Annotatef(other error, format string, args ...interface{}) error
- func BadRequestf(format string, args ...interface{}) error
- func Cause(err error) error
- func DeferredAnnotatef(err *error, format string, args ...interface{})
- func Details(err error) string
- func ErrorStack(err error) string
- func Errorf(format string, args ...interface{}) error
- func InternalServerf(format string, args ...interface{}) error
- func IsBadRequest(err error) bool
- func IsInternalServer(err error) bool
- func IsMethodNotAllowed(err error) bool
- func IsNotFound(err error) bool
- func IsNotImplemented(err error) bool
- func IsUnauthorized(err error) bool
- func Mask(other error) error
- func Maskf(other error, format string, args ...interface{}) error
- func MethodNotAllowedf(format string, args ...interface{}) error
- func New(message string) error
- func NewBadRequest(err error, msg string) error
- func NewInternalServer(err error, msg string) error
- func NewMethodNotAllowed(err error, msg string) error
- func NewNotFound(err error, msg string) error
- func NewNotImplemented(err error, msg string) error
- func NewUnauthorized(err error, msg string) error
- func NotFoundf(format string, args ...interface{}) error
- func NotImplementedf(format string, args ...interface{}) error
- func Trace(other error) error
- func Unauthorizedf(format string, args ...interface{}) error
- func Wrap(other, newDescriptive error) error
- func Wrapf(other, newDescriptive error, format string, args ...interface{}) error
- type Err
- func (e *Err) Cause() error
- func (e *Err) Code() int
- func (e *Err) ContentType() string
- func (e *Err) Error() string
- func (e *Err) Location() (filename, function string, line int)
- func (e *Err) Message() string
- func (e *Err) SetCode(code int)
- func (e *Err) SetLocation(callDepth int)
- func (e Err) Stack() string
- func (e *Err) StackTrace() []string
- func (e *Err) Underlying() error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Annotate ¶
Annotate is used to add extra context to an existing error. The location of the Annotate call is recorded with the annotations. The file, line and function are also recorded.
For example:
if err := SomeFunc(); err != nil {
return errors.Annotate(err, "failed to frombulate")
}
func Annotatef ¶
Annotatef is used to add extra context to an existing error. The location of the Annotate call is recorded with the annotations. The file, line and function are also recorded.
For example:
if err := SomeFunc(); err != nil {
return errors.Annotatef(err, "failed to frombulate the %s", arg)
}
func BadRequestf ¶
BadRequestf returns an error which satisfies IsBadRequest().
func Cause ¶
Cause returns the cause of the given error. This will be either the original error, or the result of a Wrap or Mask call.
Cause is the usual way to diagnose errors that may have been wrapped by the other errors functions.
func DeferredAnnotatef ¶
DeferredAnnotatef annotates the given error (when it is not nil) with the given format string and arguments (like fmt.Sprintf). If *err is nil, DeferredAnnotatef does nothing. This method is used in a defer statement in order to annotate any resulting error with the same message.
For example:
defer DeferredAnnotatef(&err, "failed to frombulate the %s", arg)
func Details ¶
Details returns information about the stack of errors wrapped by err, in the format:
[{filename:99: error one} {otherfile:55: cause of error one}]
This is a terse alternative to ErrorStack as it returns a single line.
func ErrorStack ¶
ErrorStack returns a string representation of the annotated error. If the error passed as the parameter is not an annotated error, the result is simply the result of the Error() method on that error.
If the error is an annotated error, a multi-line string is returned where each line represents one entry in the annotation stack. The full filename from the call stack is used in the output.
first error github.com/hifx/errgo/annotation_test.go:193: github.com/hifx/errgo/annotation_test.go:194: annotation github.com/hifx/errgo/annotation_test.go:195: github.com/hifx/errgo/annotation_test.go:196: more context github.com/hifx/errgo/annotation_test.go:197:
func Errorf ¶
Errorf creates a new annotated error and records the location that the error is created. This should be a drop in replacement for fmt.Errorf.
For example:
return errors.Errorf("validation failed: %s", message)
func InternalServerf ¶
InternalServerf returns an error which satisfies IsInternalServer().
func IsBadRequest ¶
IsBadRequest reports whether err was created with BadRequestf() or NewBadRequest().
func IsInternalServer ¶
IsInternalServer reports whether err was created with InternalServerf() or NewInternalServer().
func IsMethodNotAllowed ¶
IsMethodNotAllowed reports whether err was created with MethodNotAllowedf() or NewMethodNotAllowed().
func IsNotFound ¶
IsNotFound reports whether err was created with NotFoundf() or NewNotFound().
func IsNotImplemented ¶
IsNotImplemented reports whether err was created with NotImplementedf() or NewNotImplemented().
func IsUnauthorized ¶
IsUnauthorized reports whether err was created with Unauthorizedf() or NewUnauthorized().
func Maskf ¶
Mask masks the given error with the given format string and arguments (like fmt.Sprintf), returning a new error that maintains the error stack, but hides the underlying error type. The error string still contains the full annotations. If you want to hide the annotations, call Wrap.
func MethodNotAllowedf ¶
MethodNotAllowedf returns an error which satisfies IsMethodNotAllowed().
func New ¶
New is a drop in replacement for the standard libary errors module that records the location that the error is created.
For example:
return errors.New("validation failed")
func NewBadRequest ¶
NewBadRequest returns an error which wraps err that satisfies IsBadRequest().
func NewInternalServer ¶
NewInternalServer returns an error which wraps err that satisfies IsInternalServer().
func NewMethodNotAllowed ¶
NewMethodNotAllowed returns an error which wraps err that satisfies IsMethodNotAllowed().
func NewNotFound ¶
NewNotFound returns an error which wraps err that satisfies IsNotFound().
func NewNotImplemented ¶
NewNotImplemented returns an error which wraps err and satisfies IsNotImplemented().
func NewUnauthorized ¶
NewUnauthorized returns an error which wraps err and satisfies IsUnauthorized().
func NotImplementedf ¶
NotImplementedf returns an error which satisfies IsNotImplemented().
func Trace ¶
Trace adds the location of the Trace call to the stack. The Cause of the resulting error is the same as the error parameter. If the other error is nil, the result will be nil.
For example:
if err := SomeFunc(); err != nil {
return errors.Trace(err)
}
Example ¶
package main
import (
"fmt"
"github.com/hifx/errgo"
)
func main() {
var err1 error = fmt.Errorf("something wicked this way comes")
var err2 error = nil
// Tracing a non nil error will return an error
fmt.Println(errgo.Trace(err1))
// Tracing nil will return nil
fmt.Println(errgo.Trace(err2))
}
Output: something wicked this way comes <nil>
func Unauthorizedf ¶
Unauthorizedf returns an error which satisfies IsUnauthorized().
func Wrap ¶
Wrap changes the Cause of the error. The location of the Wrap call is also stored in the error stack.
For example:
if err := SomeFunc(); err != nil {
newErr := &packageError{"more context", private_value}
return errors.Wrap(err, newErr)
}
Types ¶
type Err ¶
type Err struct {
// contains filtered or unexported fields
}
Err holds a description of an error along with information about where the error was created.
It may be embedded in custom error types to add extra information that this errgo package can understand.
func NewErr ¶
NewErr is used to return an Err for the purpose of embedding in other structures. The location is not specified, and needs to be set with a call to SetLocation.
For example:
type FooError struct {
errgo.Err
code int
}
func NewFooError(code int) error {
err := &FooError{errgo.NewErr("foo"), code}
err.SetLocation(1)
return err
}
func NewErrWithCause ¶
NewErrWithCause is used to return an Err with case by other error for the purpose of embedding in other structures. The location is not specified, and needs to be set with a call to SetLocation.
For example:
type FooError struct {
errgo.Err
code int
}
func (e *FooError) Annotate(format string, args ...interface{}) error {
err := &FooError{errgo.NewErrWithCause(e.Err, format, args...), e.code}
err.SetLocation(1)
return err
})
func NewJSONErrWithCause ¶
NewJSONErrWithCause is same as NewErrWithCause except the content type set to application/json
func (*Err) Cause ¶
Cause of an error is the most recent error in the error stack that meets one of these criteria: the original error that was raised; the new error that was passed into the Wrap function; the most recently masked error; or nil if the error itself is considered the Cause. Normally this method is not invoked directly, but instead through the Cause stand alone function.
func (*Err) ContentType ¶
ContentType returns the HTTP content type of the error.
func (*Err) Location ¶
Location is the file and line of where the error was most recently created or annotated.
func (*Err) Message ¶
Message returns the message stored with the most recent location. This is the empty string if the most recent call was Trace, or the message stored with Annotate or Mask.
func (*Err) SetLocation ¶
SetLocation records the source location of the error at callDepth stack frames above the call.
func (*Err) StackTrace ¶
StackTrace returns one string for each location recorded in the stack of errgo. The first value is the originating error, with a line for each other annotation or tracing of the error.
func (*Err) Underlying ¶
Underlying returns the previous error in the error stack, if any. A client should not ever really call this method. It is used to build the error stack and should not be introspected by client calls. Or more specifically, clients should not depend on anything but the `Cause` of an error.