Common juju errors and functions to annotate errors. Based on juju/errgo

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

Comments
  • Add NewErrWithCause method to construct err with cause.

    Add NewErrWithCause method to construct err with cause.

    Add NewErrWithCause method to construct err with cause.

    just like NewErr...user also need a way to build custom error and do something like Trace or Annotate

    but,there is no way to create Err with cause error but without setLocation called..

    so It's better to add this~ thx

    (Review request: http://reviews.vapour.ws/r/3350/)

  • File paths are not being trimmed when project is outside of GOPATH (vgo)

    File paths are not being trimmed when project is outside of GOPATH (vgo)

    in path.go there's a function trimGoPath() it relies on GOPATH variable to trim absolute paths when stacktrace is printed. With VGO the go source now can be outside of GOPATH, when that happens paths are no longer trimmed.

    I could resolve the issue by setting srcDir to result of:

    	_, file, _, _ := runtime.Caller(0)
    	path := strings.TrimSuffix(file, "/main.go")
    

    and have it working, unfortunately it is a private variable.

    Perhaps there is a better way to get the path of the source code?

  • Print the stack trace when formatting with %+v

    Print the stack trace when formatting with %+v

    Discovered in: http://dave.cheney.net/2016/06/12/stack-traces-and-the-errors-package

    Code based on: https://github.com/pkg/errors

    With a minor but hackish change to still allow %#v to print the structure fields, in order to not break the principle of least surprise. Let me know if there is a better way to strip a type of an interface or invoke the default %#v behaviour on a type that implements fmt.Formatter.


    NOTE: this is the continuation of #26. I no longer work for cesanta and thus cannot address comments in the original PR.

    #26 received the green light from @mjs, and had an open question by @howbazaar, whom I replied to.

  • Refactor juju/errors to not rely on juju/errgo

    Refactor juju/errors to not rely on juju/errgo

    As discussed previously, this branch removes the dependency on juju/errgo.

    Instead of wrapping the errgo.Err struct and changing the behaviour of the methods, instead we have duplicated the linked list style errors of errgo, but the errors.Err struct hides the internals only exposing the values through methods.

    A Mask function is added to provide the same behaviour as the errgo.Mask function which keeps the error stack, but hides the underlying type information of the error. The existing Maskf and Contextf functions have been joined to DeferredAnnotatef, as all the call sites of those methods were just adding annotations.

  • [JUJU-759] Change error types to use new ConstError

    [JUJU-759] Change error types to use new ConstError

    Following #52, this library now supports errors.Is, errors.As, and errors.Unwrap from Go's standard errors library. This PR changes the IsX functions from errortypes.go to use errors.Is under the hood:

    • We've defined a new error type ConstError, whose values are "sentinel errors" representing a certain error type.
    • We've defined errWithType, which is an Err bundled with its error type errType ConstError. errWithType implements an Is method that compares its argument to errType. This ensures e.g. Is(e, NotFound) works as expected.
    • All the individual structs for different error types, e.g. timeout, notFound have been replaced with errWithType.
    • For X ConstError, Xf and NewX now return e errWithType where e.errType is X.
    • IsX functions are now implemented by comparison to X.

    This is structurally quite a big change, but the errors should behave exactly as before (all the tests in errortypes_test.go pass).

  • [JUJU-753] Add `Err.Unwrap` to support standard error library (Go 1.13+)

    [JUJU-753] Add `Err.Unwrap` to support standard error library (Go 1.13+)

    Since v1.13, Go's errors library has supported wrapping errors inside other errors, and manipulating these with the Is, As, and Unwrap functions. Here, I've added an Unwrap method to Err so that these functions are compatible with Err.

    I also added proxies for the Is, As, and Unwrap functions, closes #47. Finally, I've written unit tests for the Unwrap method in error_test.go.

  • fix panic on 1.4.x with chained imports

    fix panic on 1.4.x with chained imports

    I actually do not know the root cause of this error, but I've managed to reproduce it by chaining lots of imports together. At some point (only on go 1.4), having a deep import tree can lead to runtime.Caller returning a strange error condition during init() where ok is true but file is improperly left at an initialization value. This checks for that value and bails if it's found.

    For more information on this error, and to reproduce it yourself, look at https://github.com/jmoiron/jujut

  • Fix replaceLocation to be more robust.

    Fix replaceLocation to be more robust.

    For some reason running the tests under the cover tool was causing the entire source path, not just the bit from $GOPATH/src to appear.

    As there really isn't any guarantee what will be returned from runtime.Callers (you should see gccgo), make the test a bit more liberal in what we accept.

  • [JUJU-1339] Adds a shush method to the library and IsType.

    [JUJU-1339] Adds a shush method to the library and IsType.

    Shush is responsible for making an error silent in fmt output. Specifically useful with fmt.Errorf when you want an error to be wrapped but want its error message shushed.

    IsType is a convenience method for checking a more complex error is of type x within it's change. This is for cases where errors.Is can't be used and where errors.As is not needed for the actual error type.

  • [JUJU-790] Add GitHub Action to run `go test`

    [JUJU-790] Add GitHub Action to run `go test`

    This PR introduces a GitHub Action which installs the version of Go specified in go.mod, then runs go test. This action will be run as CI on pushes and pull requests.

    The action has been tested locally on my machine using nektos/act.

  • CauseEquals: new function, tests and regenerated documetnation

    CauseEquals: new function, tests and regenerated documetnation

    Useful when comparing constant error with returned from some deep function.

    Useful in situations like:

    ErrNotFound := errors.New("Not found")
    inSome := errors.Trace(ErrNotFound)
    veryDeep := errors.Trace(inSome)
    returned := errors.Trace(veryDeep)
    if errors.CauseEquals(ErrNotFound, returned) {
        fmt.Println("Can return soft error 404")
    } else {
        fmt.Println("Serious error, returning with code 500")
    }
    
Linter for errors.Is and errors.As

erris erris is a program for checking that errors are compared or type asserted using go1.13 errors.Is and errors.As functions. Install go get -u gith

Nov 27, 2022
An errors package optimized for the pkg/errors package
An errors package optimized for the pkg/errors package

errors An errors package optimized for the pkg/errors package Use Download and install go get github.com/dobyte/errors API // New Wrapping for errors.

Mar 2, 2022
eris provides a better way to handle, trace, and log errors in Go 🎆

eris Package eris provides a better way to handle, trace, and log errors in Go. go get github.com/rotisserie/eris Why you'll want to switch to eris Us

Dec 29, 2022
Golang errors with stack trace and source fragments.
Golang errors with stack trace and source fragments.

Golang Errors with Stack Trace and Source Fragments Tired of uninformative error output? Probably this will be more convenient: Example package main

Dec 17, 2022
Go tool to wrap and fix errors with the new %w verb directive
Go tool to wrap and fix errors with the new %w verb directive

errwrap Wrap and fix Go errors with the new %w verb directive. This tool analyzes fmt.Errorf() calls and reports calls that contain a verb directive t

Nov 10, 2022
Golang errors with stacktrace and context

merry Add context to errors, including automatic stack capture, cause chains, HTTP status code, user messages, and arbitrary values. The package is la

Nov 19, 2022
This structured Error package wraps errors with context and other info

RErr package This structured Error package wraps errors with context and other info. It can be used to enrich logging, for example with a structured l

Jan 21, 2022
The Emperor takes care of all errors personally
The Emperor takes care of all errors personally

The Emperor takes care of all errors personally. Go's philosophy encourages to gracefully handle errors whenever possible, but some times recovering f

Jan 9, 2023
A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.
A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.

Errors Errors package is a drop-in replacement of the built-in Go errors package with no external dependencies. It lets you create errors of 11 differ

Dec 6, 2022
A Go (golang) package for representing a list of errors as a single error.

go-multierror go-multierror is a package for Go that provides a mechanism for representing a list of error values as a single error. This allows a fun

Jan 1, 2023
Hierarchical errors reporting done right in Golang

Hierarchical errors made right Hate seeing error: exit status 128 in the output of programs without actual explanation what is going wrong? Or, maybe,

Nov 9, 2021
harvest Go errors with ease

Pears Harvest Go Errors with Ease Introduction Pears helps reduce the boilerplate and ensure correctness for common error-handling scenarios: Panic re

Apr 25, 2021
SupErr -- Go stdlib errors with super powers

superr SupErr -- Go stdlib errors with super powers. Pronounced super with a French accent :D Build a stack of errors compatible with Go stdlib and er

Nov 15, 2022
Package semerr helps to work with errors in Golang.
Package semerr helps to work with errors in Golang.

semerr Package semerr helps to work with errors in Golang. Const error An error that can be defined as const. var errMutable error = errors.New("mutab

Oct 30, 2022
A Nostress Errors Package For Golang

A Nostress Errors Package For Golang

Nov 2, 2021
Simple Go library for typed errors creation

go-typed-errors Why this repo was created? Main reason was to create additional methods for better error typing support. Why not to use errors.As and

Nov 5, 2021
Simple Go library for typed const errors creation

go-errors Why this repo was created? Main reason was to create additional methods for better error typing support. Features Errors as constants errors

Nov 5, 2021
This project is for parsing Artifactory logs for errors

hello-frog About this plugin This plugin is a template and a functioning example for a basic JFrog CLI plugin. This README shows the expected structur

Nov 30, 2021
Go package for errors with chained stack traces

errstack: Go errors with chained stack traces errstack is a Go package for creating errors with stack traces. It is heavily inspired by github.com/pkg

Nov 27, 2021