errors

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 22, 2022 License: LGPL-3.0 Imports: 5 Imported by: 18,733

README

errors

import "github.com/juju/errors"

GoDoc

The juju/errors provides an easy way to annotate errors without losing the original error context.

The exported New and Errorf functions are designed to replace the errors.New and fmt.Errorf functions respectively. The same underlying error is there, but the package also records the location at which the error was created.

A primary use case for this library is to add extra context any time an error is returned from a function.

    if err := SomeFunc(); err != nil {
	    return err
	}

This instead becomes:

    if err := SomeFunc(); err != nil {
	    return errors.Trace(err)
	}

which just records the file and line number of the Trace call, or

    if err := SomeFunc(); err != nil {
	    return errors.Annotate(err, "more context")
	}

which also adds an annotation to the error.

When you want to check to see if an error is of a particular type, a helper function is normally exported by the package that returned the error, like the os package does. The underlying cause of the error is available using the Cause function.

os.IsNotExist(errors.Cause(err))

The result of the Error() call on an annotated error is the annotations joined with colons, then the result of the Error() method for the underlying error that was the cause.

err := errors.Errorf("original")
err = errors.Annotatef(err, "context")
err = errors.Annotatef(err, "more context")
err.Error() -> "more context: context: original"

Obviously recording the file, line and functions is not very useful if you cannot get them back out again.

errors.ErrorStack(err)

will return something like:

first error
github.com/juju/errors/annotation_test.go:193:
github.com/juju/errors/annotation_test.go:194: annotation
github.com/juju/errors/annotation_test.go:195:
github.com/juju/errors/annotation_test.go:196: more context
github.com/juju/errors/annotation_test.go:197:

The first error was generated by an external system, so there was no location associated. The second, fourth, and last lines were generated with Trace calls, and the other two through Annotate.

Sometimes when responding to an error you want to return a more specific error for the situation.

    if err := FindField(field); err != nil {
	    return errors.Wrap(err, errors.NotFoundf(field))
	}

This returns an error where the complete error stack is still available, and errors.Cause() will return the NotFound error.

func AlreadyExistsf

func AlreadyExistsf(format string, args ...interface{}) error

AlreadyExistsf returns an error which satisfies IsAlreadyExists().

func Annotate

func Annotate(other error, message string) error

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

func Annotatef(other error, format string, args ...interface{}) error

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

func BadRequestf(format string, args ...interface{}) error

BadRequestf returns an error which satisfies IsBadRequest().

func Cause

func Cause(err error) error

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

func DeferredAnnotatef(err *error, format string, args ...interface{})

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

func Details(err error) string

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

func ErrorStack(err error) string

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/juju/errors/annotation_test.go:193:
github.com/juju/errors/annotation_test.go:194: annotation
github.com/juju/errors/annotation_test.go:195:
github.com/juju/errors/annotation_test.go:196: more context
github.com/juju/errors/annotation_test.go:197:

func Errorf

func Errorf(format string, args ...interface{}) error

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 Forbiddenf

func Forbiddenf(format string, args ...interface{}) error

Forbiddenf returns an error which satistifes IsForbidden()

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists reports whether the error was created with AlreadyExistsf() or NewAlreadyExists().

func IsBadRequest

func IsBadRequest(err error) bool

IsBadRequest reports whether err was created with BadRequestf() or NewBadRequest().

func IsForbidden

func IsForbidden(err error) bool

IsForbidden reports whether err was created with Forbiddenf() or NewForbidden().

func IsMethodNotAllowed

func IsMethodNotAllowed(err error) bool

IsMethodNotAllowed reports whether err was created with MethodNotAllowedf() or NewMethodNotAllowed().

func IsNotAssigned

func IsNotAssigned(err error) bool

IsNotAssigned reports whether err was created with NotAssignedf() or NewNotAssigned().

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err was created with NotFoundf() or NewNotFound().

func IsNotImplemented

func IsNotImplemented(err error) bool

IsNotImplemented reports whether err was created with NotImplementedf() or NewNotImplemented().

func IsNotProvisioned

func IsNotProvisioned(err error) bool

IsNotProvisioned reports whether err was created with NotProvisionedf() or NewNotProvisioned().

func IsNotSupported

func IsNotSupported(err error) bool

IsNotSupported reports whether the error was created with NotSupportedf() or NewNotSupported().

func IsNotValid

func IsNotValid(err error) bool

IsNotValid reports whether the error was created with NotValidf() or NewNotValid().

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized reports whether err was created with Unauthorizedf() or NewUnauthorized().

func IsUserNotFound

func IsUserNotFound(err error) bool

IsUserNotFound reports whether err was created with UserNotFoundf() or NewUserNotFound().

func Mask

func Mask(other error) error

Mask hides the underlying error type, and records the location of the masking.

func Maskf

func Maskf(other error, format string, args ...interface{}) error

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

func MethodNotAllowedf(format string, args ...interface{}) error

MethodNotAllowedf returns an error which satisfies IsMethodNotAllowed().

func New

func New(message string) error

New is a drop in replacement for the standard library errors module that records the location that the error is created.

For example:

return errors.New("validation failed")

func NewAlreadyExists

func NewAlreadyExists(err error, msg string) error

NewAlreadyExists returns an error which wraps err and satisfies IsAlreadyExists().

func NewBadRequest

func NewBadRequest(err error, msg string) error

NewBadRequest returns an error which wraps err that satisfies IsBadRequest().

func NewForbidden

func NewForbidden(err error, msg string) error

NewForbidden returns an error which wraps err that satisfies IsForbidden().

func NewMethodNotAllowed

func NewMethodNotAllowed(err error, msg string) error

NewMethodNotAllowed returns an error which wraps err that satisfies IsMethodNotAllowed().

func NewNotAssigned

func NewNotAssigned(err error, msg string) error

NewNotAssigned returns an error which wraps err that satisfies IsNotAssigned().

func NewNotFound

func NewNotFound(err error, msg string) error

NewNotFound returns an error which wraps err that satisfies IsNotFound().

func NewNotImplemented

func NewNotImplemented(err error, msg string) error

NewNotImplemented returns an error which wraps err and satisfies IsNotImplemented().

func NewNotProvisioned

func NewNotProvisioned(err error, msg string) error

NewNotProvisioned returns an error which wraps err that satisfies IsNotProvisioned().

func NewNotSupported

func NewNotSupported(err error, msg string) error

NewNotSupported returns an error which wraps err and satisfies IsNotSupported().

func NewNotValid

func NewNotValid(err error, msg string) error

NewNotValid returns an error which wraps err and satisfies IsNotValid().

func NewUnauthorized

func NewUnauthorized(err error, msg string) error

NewUnauthorized returns an error which wraps err and satisfies IsUnauthorized().

func NewUserNotFound

func NewUserNotFound(err error, msg string) error

NewUserNotFound returns an error which wraps err and satisfies IsUserNotFound().

func NotAssignedf

func NotAssignedf(format string, args ...interface{}) error

NotAssignedf returns an error which satisfies IsNotAssigned().

func NotFoundf

func NotFoundf(format string, args ...interface{}) error

NotFoundf returns an error which satisfies IsNotFound().

func NotImplementedf

func NotImplementedf(format string, args ...interface{}) error

NotImplementedf returns an error which satisfies IsNotImplemented().

func NotProvisionedf

func NotProvisionedf(format string, args ...interface{}) error

NotProvisionedf returns an error which satisfies IsNotProvisioned().

func NotSupportedf

func NotSupportedf(format string, args ...interface{}) error

NotSupportedf returns an error which satisfies IsNotSupported().

func NotValidf

func NotValidf(format string, args ...interface{}) error

NotValidf returns an error which satisfies IsNotValid().

func Trace

func Trace(other error) error

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)
}

func Unauthorizedf

func Unauthorizedf(format string, args ...interface{}) error

Unauthorizedf returns an error which satisfies IsUnauthorized().

func UserNotFoundf

func UserNotFoundf(format string, args ...interface{}) error

UserNotFoundf returns an error which satisfies IsUserNotFound().

func Wrap

func Wrap(other, newDescriptive error) error

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)
}

func Wrapf

func Wrapf(other, newDescriptive error, format string, args ...interface{}) error

Wrapf changes the Cause of the error, and adds an annotation. The location of the Wrap call is also stored in the error stack.

For example:

if err := SomeFunc(); err != nil {
    return errors.Wrapf(err, simpleErrorType, "invalid value %q", value)
}

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 errors package can understand.

func NewErr
func NewErr(format string, args ...interface{}) Err

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 {
    errors.Err
    code int
}

func NewFooError(code int) error {
    err := &FooError{errors.NewErr("foo"), code}
    err.SetLocation(1)
    return err
}
func NewErrWithCause
func NewErrWithCause(other error, format string, args ...interface{}) Err

NewErrWithCause is used to return an Err with cause 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 {
    errors.Err
    code int
}

func (e *FooError) Annotate(format string, args ...interface{}) error {
    err := &FooError{errors.NewErrWithCause(e.Err, format, args...), e.code}
    err.SetLocation(1)
    return err
})
func (*Err) Cause
func (e *Err) Cause() error

The 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) Error
func (e *Err) Error() string

Error implements error.Error.

func (*Err) Format
func (e *Err) Format(s fmt.State, verb rune)

Format implements fmt.Formatter When printing errors with %+v it also prints the stack trace. %#v unsurprisingly will print the real underlying type.

func (*Err) Location
func (e *Err) Location() (filename string, line int)

Location is the file and line of where the error was most recently created or annotated.

func (*Err) Message
func (e *Err) Message() string

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
func (e *Err) SetLocation(callDepth int)

SetLocation records the source location of the error at callDepth stack frames above the call.

func (*Err) StackTrace
func (e *Err) StackTrace() []string

StackTrace returns one string for each location recorded in the stack of errors. The first value is the originating error, with a line for each other annotation or tracing of the error.

func (*Err) Underlying
func (e *Err) Underlying() error

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.


Generated by godoc2md

Documentation

Overview

Package errors provides an easy way to annotate errors without losing the original error context.

The exported `New` and `Errorf` functions are designed to replace the `errors.New` and `fmt.Errorf` functions respectively. The same underlying error is there, but the package also records the location at which the error was created.

A primary use case for this library is to add extra context any time an error is returned from a function.

    if err := SomeFunc(); err != nil {
	    return err
	}

This instead becomes:

    if err := SomeFunc(); err != nil {
	    return errors.Trace(err)
	}

which just records the file and line number of the Trace call, or

    if err := SomeFunc(); err != nil {
	    return errors.Annotate(err, "more context")
	}

which also adds an annotation to the error.

When you want to check to see if an error is of a particular type, a helper function is normally exported by the package that returned the error, like the `os` package does. The underlying cause of the error is available using the `Cause` function.

os.IsNotExist(errors.Cause(err))

The result of the `Error()` call on an annotated error is the annotations joined with colons, then the result of the `Error()` method for the underlying error that was the cause.

err := errors.Errorf("original")
err = errors.Annotatef(err, "context")
err = errors.Annotatef(err, "more context")
err.Error() -> "more context: context: original"

Obviously recording the file, line and functions is not very useful if you cannot get them back out again.

errors.ErrorStack(err)

will return something like:

first error
github.com/juju/errors/annotation_test.go:193:
github.com/juju/errors/annotation_test.go:194: annotation
github.com/juju/errors/annotation_test.go:195:
github.com/juju/errors/annotation_test.go:196: more context
github.com/juju/errors/annotation_test.go:197:

The first error was generated by an external system, so there was no location associated. The second, fourth, and last lines were generated with Trace calls, and the other two through Annotate.

Sometimes when responding to an error you want to return a more specific error for the situation.

    if err := FindField(field); err != nil {
	    return errors.Wrap(err, errors.NotFoundf(field))
	}

This returns an error where the complete error stack is still available, and `errors.Cause()` will return the `NotFound` error.

Index

Examples

Constants

View Source
const (
	// Timeout represents an error on timeout.
	Timeout = ConstError("timeout")
	// NotFound represents an error when something has not been found.
	NotFound = ConstError("not found")
	// UserNotFound represents an error when a non-existent user is looked up.
	UserNotFound = ConstError("user not found")
	// Unauthorized represents an error when an operation is unauthorized.
	Unauthorized = ConstError("unauthorized")
	// NotImplemented represents an error when something is not
	// implemented.
	NotImplemented = ConstError("not implemented")
	// AlreadyExists represents and error when something already exists.
	AlreadyExists = ConstError("already exists")
	// NotSupported represents an error when something is not supported.
	NotSupported = ConstError("not supported")
	// NotValid represents an error when something is not valid.
	NotValid = ConstError("not valid")
	// NotProvisioned represents an error when something is not yet provisioned.
	NotProvisioned = ConstError("not provisioned")
	// NotAssigned represents an error when something is not yet assigned to
	// something else.
	NotAssigned = ConstError("not assigned")
	// BadRequest represents an error when a request has bad parameters.
	BadRequest = ConstError("bad request")
	// MethodNotAllowed represents an error when an HTTP request
	// is made with an inappropriate method.
	MethodNotAllowed = ConstError("method not allowed")
	// Forbidden represents an error when a request cannot be completed because of
	// missing privileges.
	Forbidden = ConstError("forbidden")
	// QuotaLimitExceeded is emitted when an action failed due to a quota limit check.
	QuotaLimitExceeded = ConstError("quota limit exceeded")
	// NotYetAvailable is the error returned when a resource is not yet available
	// but it might be in the future.
	NotYetAvailable = ConstError("not yet available")
)

Different types of errors

Variables

This section is empty.

Functions

func AlreadyExistsf

func AlreadyExistsf(format string, args ...interface{}) error

AlreadyExistsf returns an error which satisfies Is(err, AlreadyExists) and the Locationer interface.

func Annotate

func Annotate(other error, message string) error

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

func Annotatef(other error, format string, args ...interface{}) error

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 As

func As(err error, target interface{}) bool

As is a proxy for the As function in Go's standard `errors` library (pkg.go.dev/errors).

func AsType

func AsType[T error](err error) (T, bool)

AsType finds the first error in err's chain that is assignable to type T, and if a match is found, returns that error value and true. Otherwise, it returns T's zero value and false.

AsType is equivalent to errors.As, but uses a type parameter and returns the target, to avoid having to define a variable before the call. For example, callers can replace this:

var pathError *fs.PathError
if errors.As(err, &pathError) {
    fmt.Println("Failed at path:", pathError.Path)
}

With:

if pathError, ok := errors.AsType[*fs.PathError](err); ok {
    fmt.Println("Failed at path:", pathError.Path)
}
Example
package main

import (
	"fmt"

	"github.com/juju/errors"
)

type MyError struct {
	Message string
}

func (m *MyError) Error() string {
	return m.Message
}

func main() {
	myErr := &MyError{Message: "these are not the droids you're looking for"}
	err := fmt.Errorf("wrapped: %w", myErr)
	myErr, is := errors.AsType[*MyError](err)
	fmt.Println(is)
	fmt.Println(myErr.Message)

}
Output:

true
these are not the droids you're looking for

func BadRequestf

func BadRequestf(format string, args ...interface{}) error

BadRequestf returns an error which satisfies Is(err, BadRequest) and the Locationer interface.

func Cause

func Cause(err error) error

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

func DeferredAnnotatef(err *error, format string, args ...interface{})

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

func Details(err error) string

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

func ErrorStack(err error) string

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/juju/errors/annotation_test.go:193:
github.com/juju/errors/annotation_test.go:194: annotation
github.com/juju/errors/annotation_test.go:195:
github.com/juju/errors/annotation_test.go:196: more context
github.com/juju/errors/annotation_test.go:197:

func Errorf

func Errorf(format string, args ...interface{}) error

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 Forbiddenf

func Forbiddenf(format string, args ...interface{}) error

Forbiddenf returns an error which satistifes Is(err, Forbidden) and the Locationer interface.

func HasType

func HasType[T error](err error) bool

HasType is a function wrapper around AsType dropping the where return value from AsType() making a function that can be used like this:

return HasType[*MyError](err)

Or

if HasType[*MyError](err) {}
Example
package main

import (
	"fmt"

	"github.com/juju/errors"
)

type MyError struct {
	Message string
}

func (m *MyError) Error() string {
	return m.Message
}

func main() {
	myErr := &MyError{Message: "these are not the droids you're looking for"}
	err := fmt.Errorf("wrapped: %w", myErr)
	is := errors.HasType[*MyError](err)
	fmt.Println(is)

}
Output:

true

func Hide

func Hide(err error) error

Hide takes an error and silences it's error string from appearing in fmt like

Example
package main

import (
	stderrors "errors"
	"fmt"

	"github.com/juju/errors"
)

func main() {
	myConstError := errors.ConstError("I don't want to be fmt printed")
	err := fmt.Errorf("don't show this error%w", errors.Hide(myConstError))

	fmt.Println(err)
	fmt.Println(stderrors.Is(err, myConstError))

}
Output:

don't show this error
true

func Is

func Is(err, target error) bool

Is is a proxy for the Is function in Go's standard `errors` library (pkg.go.dev/errors).

func IsAlreadyExists deprecated

func IsAlreadyExists(err error) bool

Deprecated: IsAlreadyExists reports whether the err is a AlreadyExists error. Use Is(err, AlreadyExists).

func IsBadRequest deprecated

func IsBadRequest(err error) bool

Deprecated: IsBadRequest reports whether err is a BadRequest error. Use Is(err, BadRequest)

func IsForbidden deprecated

func IsForbidden(err error) bool

Deprecated: IsForbidden reports whether err is a Forbidden error. Use Is(err, Forbidden).

func IsMethodNotAllowed deprecated

func IsMethodNotAllowed(err error) bool

Deprecated: IsMethodNotAllowed reports whether err is a MethodNotAllowed error. Use Is(err, MethodNotAllowed)

func IsNotAssigned deprecated

func IsNotAssigned(err error) bool

Deprecated: IsNotAssigned reports whether err is a NotAssigned error. Use Is(err, NotAssigned)

func IsNotFound deprecated

func IsNotFound(err error) bool

Deprecated: IsNotFound reports whether err is a NotFound error. Use Is(err, NotFound).

func IsNotImplemented deprecated

func IsNotImplemented(err error) bool

Deprecated: IsNotImplemented reports whether err is a NotImplemented error. Use Is(err, NotImplemented).

func IsNotProvisioned deprecated

func IsNotProvisioned(err error) bool

Deprecated: IsNotProvisioned reports whether err is a NotProvisioned error. Use Is(err, NotProvisioned).

func IsNotSupported deprecated

func IsNotSupported(err error) bool

Deprecated: IsNotSupported reports whether err is a NotSupported error. Use Is(err, NotSupported).

func IsNotValid deprecated

func IsNotValid(err error) bool

Deprecated: IsNotValid reports whether err is a NotValid error. Use Is(err, NotValid).

func IsNotYetAvailable deprecated

func IsNotYetAvailable(err error) bool

Deprecated: IsNotYetAvailable reports whether err is a NotYetAvailable err. Use Is(err, NotYetAvailable)

func IsQuotaLimitExceeded deprecated

func IsQuotaLimitExceeded(err error) bool

Deprecated: IsQuotaLimitExceeded reports whether err is a QuoteLimitExceeded err. Use Is(err, QuotaLimitExceeded).

func IsTimeout deprecated

func IsTimeout(err error) bool

Deprecated: IsTimeout reports whether err is a Timeout error. Use Is(err, Timeout).

func IsUnauthorized deprecated

func IsUnauthorized(err error) bool

Deprecated: IsUnauthorized reports whether err is a Unauthorized error. Use Is(err, Unauthorized).

func IsUserNotFound deprecated

func IsUserNotFound(err error) bool

Deprecated: IsUserNotFound reports whether err is a UserNotFound error. Use Is(err, UserNotFound).

func Mask

func Mask(other error) error

Mask hides the underlying error type, and records the location of the masking.

func Maskf

func Maskf(other error, format string, args ...interface{}) error

Maskf 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

func MethodNotAllowedf(format string, args ...interface{}) error

MethodNotAllowedf returns an error which satisfies Is(err, MethodNotAllowed) and the Locationer interface.

func New

func New(message string) error

New is a drop in replacement for the standard library errors module that records the location that the error is created.

For example:

return errors.New("validation failed")

func NewAlreadyExists

func NewAlreadyExists(err error, msg string) error

NewAlreadyExists returns an error which wraps err and satisfies Is(err, AlreadyExists) and the Locationer interface.

func NewBadRequest

func NewBadRequest(err error, msg string) error

NewBadRequest returns an error which wraps err and satisfies Is(err, BadRequest) and the Locationer interface.

func NewForbidden

func NewForbidden(err error, msg string) error

NewForbidden returns an error which wraps err and satisfies Is(err, Forbidden) and the Locationer interface.

func NewMethodNotAllowed

func NewMethodNotAllowed(err error, msg string) error

NewMethodNotAllowed returns an error which wraps err and satisfies Is(err, MethodNotAllowed) and the Locationer interface.

func NewNotAssigned

func NewNotAssigned(err error, msg string) error

NewNotAssigned returns an error which wraps err and satisfies Is(err, NotAssigned) and the Locationer interface.

func NewNotFound

func NewNotFound(err error, msg string) error

NewNotFound returns an error which wraps err and satisfies Is(err, NotFound) and the Locationer interface.

func NewNotImplemented

func NewNotImplemented(err error, msg string) error

NewNotImplemented returns an error which wraps err and satisfies Is(err, NotImplemented) and the Locationer interface.

func NewNotProvisioned

func NewNotProvisioned(err error, msg string) error

NewNotProvisioned returns an error which wraps err and satisfies Is(err, NotProvisioned) and the Locationer interface.

func NewNotSupported

func NewNotSupported(err error, msg string) error

NewNotSupported returns an error which satisfies Is(err, NotSupported) and the Locationer interface.

func NewNotValid

func NewNotValid(err error, msg string) error

NewNotValid returns an error which wraps err and satisfies Is(err, NotValid) and the Locationer interface.

func NewNotYetAvailable

func NewNotYetAvailable(err error, msg string) error

NewNotYetAvailable returns an error which wraps err and satisfies Is(err, NotYetAvailable) and the Locationer interface.

func NewQuotaLimitExceeded

func NewQuotaLimitExceeded(err error, msg string) error

NewQuotaLimitExceeded returns an error which wraps err and satisfies Is(err, QuotaLimitExceeded) and the Locationer interface.

func NewTimeout

func NewTimeout(err error, msg string) error

NewTimeout returns an error which wraps err and satisfies Is(err, Timeout) and the Locationer interface.

func NewUnauthorized

func NewUnauthorized(err error, msg string) error

NewUnauthorized returns an error which wraps err and satisfies Is(err, Unathorized) and the Locationer interface.

func NewUserNotFound

func NewUserNotFound(err error, msg string) error

NewUserNotFound returns an error which wraps err and satisfies Is(err, UserNotFound) and the Locationer interface.

func NotAssignedf

func NotAssignedf(format string, args ...interface{}) error

NotAssignedf returns an error which satisfies Is(err, NotAssigned) and the Locationer interface.

func NotFoundf

func NotFoundf(format string, args ...interface{}) error

NotFoundf returns an error which satisfies Is(err, NotFound) and the Locationer interface.

func NotImplementedf

func NotImplementedf(format string, args ...interface{}) error

NotImplementedf returns an error which satisfies Is(err, NotImplemented) and the Locationer interface.

func NotProvisionedf

func NotProvisionedf(format string, args ...interface{}) error

NotProvisionedf returns an error which satisfies Is(err, NotProvisioned) and the Locationer interface.

func NotSupportedf

func NotSupportedf(format string, args ...interface{}) error

NotSupportedf returns an error which satisfies Is(err, NotSupported) and the Locationer interface.

func NotValidf

func NotValidf(format string, args ...interface{}) error

NotValidf returns an error which satisfies Is(err, NotValid) and the Locationer interface.

func NotYetAvailablef

func NotYetAvailablef(format string, args ...interface{}) error

NotYetAvailablef returns an error which satisfies Is(err, NotYetAvailable) and the Locationer interface.

func QuotaLimitExceededf

func QuotaLimitExceededf(format string, args ...interface{}) error

QuotaLimitExceededf returns an error which satisfies Is(err, QuotaLimitExceeded) and the Locationer interface.

func SetLocation

func SetLocation(err error, callDepth int) error

SetLocation takes a given error and records where in the stack SetLocation was called from and returns the wrapped error with the location information set. The returned error implements the Locationer interface. If err is nil then a nil error is returned.

func Timeoutf

func Timeoutf(format string, args ...interface{}) error

Timeoutf returns an error which satisfies Is(err, Timeout) and the Locationer interface.

func Trace

func Trace(other error) error

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/juju/errors"
)

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(errors.Trace(err1))
	// Tracing nil will return nil
	fmt.Println(errors.Trace(err2))

}
Output:

something wicked this way comes
<nil>

func Unauthorizedf

func Unauthorizedf(format string, args ...interface{}) error

Unauthorizedf returns an error that satisfies Is(err, Unauthorized) and the Locationer interface.

func Unwrap

func Unwrap(err error) error

Unwrap is a proxy for the Unwrap function in Go's standard `errors` library (pkg.go.dev/errors).

func UserNotFoundf

func UserNotFoundf(format string, args ...interface{}) error

UserNotFoundf returns an error which satisfies Is(err, UserNotFound) and the Locationer interface.

func WithType

func WithType(err error, errType ConstError) error

WithType is responsible for annotating an already existing error so that it also satisfies that of a ConstError. The resultant error returned should satisfy Is(err, errType). If err is nil then a nil error will also be returned.

Now with Go's Is, As and Unwrap support it no longer makes sense to Wrap() 2 errors as both of those errors could be chains of errors in their own right. WithType aims to solve some of the usefulness of Wrap with the ability to make a pre-existing error also satisfy a ConstError type.

func Wrap

func Wrap(other, newDescriptive error) error

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)
}

func Wrapf

func Wrapf(other, newDescriptive error, format string, args ...interface{}) error

Wrapf changes the Cause of the error, and adds an annotation. The location of the Wrap call is also stored in the error stack.

For example:

if err := SomeFunc(); err != nil {
    return errors.Wrapf(err, simpleErrorType, "invalid value %q", value)
}

Types

type ConstError

type ConstError string

a ConstError is a prototype for a certain type of error

func (ConstError) Error

func (e ConstError) Error() string

ConstError implements error

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 errors package can understand.

func NewErr

func NewErr(format string, args ...interface{}) Err

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 {
    errors.Err
    code int
}

func NewFooError(code int) error {
    err := &FooError{errors.NewErr("foo"), code}
    err.SetLocation(1)
    return err
}

func NewErrWithCause

func NewErrWithCause(other error, format string, args ...interface{}) Err

NewErrWithCause is used to return an Err with cause 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 {
    errors.Err
    code int
}

func (e *FooError) Annotate(format string, args ...interface{}) error {
    err := &FooError{errors.NewErrWithCause(e.Err, format, args...), e.code}
    err.SetLocation(1)
    return err
})

func (*Err) Cause

func (e *Err) Cause() error

Cause returns 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) Error

func (e *Err) Error() string

Error implements error.Error.

func (*Err) Format

func (e *Err) Format(s fmt.State, verb rune)

Format implements fmt.Formatter When printing errors with %+v it also prints the stack trace. %#v unsurprisingly will print the real underlying type.

func (*Err) Location

func (e *Err) Location() (function string, line int)

Location returns the package path-qualified function name and line of where the error was most recently created or annotated.

func (*Err) Message

func (e *Err) Message() string

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

func (e *Err) SetLocation(callDepth int)

SetLocation records the package path-qualified function name of the error at callDepth stack frames above the call.

func (*Err) StackTrace

func (e *Err) StackTrace() []string

StackTrace returns one string for each location recorded in the stack of errors. The first value is the originating error, with a line for each other annotation or tracing of the error.

func (*Err) Underlying

func (e *Err) Underlying() error

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.

func (*Err) Unwrap

func (e *Err) Unwrap() error

Unwrap is a synonym for Underlying, which allows Err to be used with the Unwrap, Is and As functions in Go's standard `errors` library.

type Locationer

type Locationer interface {
	// Location returns the path-qualified function name where the error was
	// created and the line number
	Location() (function string, line int)
}

Locationer is an interface that represents a certain class of errors that contain the location information from where they were raised.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL