errors

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2018 License: BSD-3-Clause Imports: 2 Imported by: 51

Documentation

Overview

Package errors is the same as gopkg.in/errgo.v2/errors except that it adds convenience functions that use the fmt package to format error messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any(err error) bool

Any returns true. It can be used as an argument to Note to allow any cause to pass through to the wrapped error.

func Because

func Because(err, cause error, msg string) error

Because returns a new error that wraps err and has the given cause, and adds the given message.

If err is nil and msg is empty, the returned error's message will be the same as cause's. This is equivalent to calling errors.Note(cause, errors.Is(cause), "") and is useful for annotating an global error value with source location information.

Because returns a nil error if all of err, cause and msg are zero.

func Becausef

func Becausef(err, cause error, format string, a ...interface{}) error

Becausef is like Because except it formats the message with a fmt format specifier.

func Cause

func Cause(err error) error

Cause returns the cause of the given error. If err does not implement Causer or its Cause method returns nil, it returns err itself.

Cause is the usual way to diagnose errors that may have been wrapped.

func Details

func Details(err error) string

Details returns information about the stack of underlying errors wrapped by err, in the format:

[{filename:99: error one} {otherfile:55: cause of error one}]

The details are found by type-asserting the error to the Locator, Causer and Wrapper interfaces. Details of the underlying stack are found by recursively calling Underlying when the underlying error implements Wrapper.

func Is

func Is(err error) func(error) bool

Is returns a function that returns whether the an error is equal to the given error. It is intended to be used as a "shouldPreserveCause" argument to Note. For example:

return errgo.Note(err, errgo.Is(http.ErrNoCookie), "")

would return an error with an http.ErrNoCookie cause only if that was err's cause.

func New

func New(s string) error

New returns a new error with the given error message and no cause. It is a drop-in replacement for errors.New from the standard library.

func Newf

func Newf(format string, a ...interface{}) error

Newf is like New except it formats the message with a fmt format specifier.

func Note

func Note(err error, shouldPreserveCause func(error) bool, msg string) error

Note returns a new error that wraps the given error and holds information about the caller. It preserves the cause if shouldPreserveCause is non-nil and shouldPreserveCause(err) is true. If msg is non-empty, it is used to prefix the returned error's string value.

If err is nil, Note returns nil without calling shouldPreserveCause.

func Notef

func Notef(err error, shouldPreserveCause func(error) bool, format string, a ...interface{}) error

Notef is like Note except it formats the message with a fmt format specifier.

func SetLocation

func SetLocation(err error, callDepth int)

SetLocation sets the location of the error to the file and line number of the code that is running callDepth frames above the caller. It does nothing if the error was not created by this package. SetLocation should only be called mmediately after an error has been created, before it is shared with other code which could access it concurrently.

func Wrap

func Wrap(err error) error

Wrap returns a new error that wraps the given error and holds information about the caller. It is equivalent to Note(err, nil, ""). Note that this means that the returned error has no cause.

Types

type Causer

type Causer = errors.Causer

type Locationer deprecated

type Locationer = errors.Locationer

Deprecated: Locationer is the old name for Locator, kept for backward compatibility only.

type Locator added in v2.1.0

type Locator = errors.Locator

type Wrapper

type Wrapper = errors.Wrapper

Jump to

Keyboard shortcuts

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