errors

package module
v1.5.4 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2023 License: Unlicense Imports: 3 Imported by: 4

README

Errors Go Reference Go Report Card

A simple package for creating and wrapping errors in golang

Usage

Creating an error
const ErrEOF = errors.Error("Unexpected end of file")
Wrapping an error
var err error
var config *os.File
if config, err = os.Open("./config.toml");err != nil{
    return errors.Wrap("error getting config file",err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v1.3.0

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

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true. Otherwise, it returns false.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

An error type might provide an As method so it can be treated as if it were a different error type.

As panics if target is not a non-nil pointer to either a type that implements error, or to any interface type.

This function is an alias to errors.As in the standard library.

func Cause

func Cause(err, cause error) error

Cause wraps an error in a another error. This returns nil if `cause` is nil. If `err` is nil `cause` is returned.

func CauseStr added in v1.2.0

func CauseStr(err error, cause string) error

CauseStr like `Cause` but cause is a string instead of an error

func Is added in v1.3.0

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

An error type might provide an Is method so it can be treated as equivalent to an existing error.

This function is an alias to errors.Is in the standard library.

func New added in v1.3.0

func New(e string) error

New returns an error that formats as the given text. Each call to New returns a distinct error value even if the text is identical.

This function is an alias to New in the standard library.

func Recover added in v1.4.0

func Recover(err *error)

Recover recovers from a panic. This function must be deferred. Usage:

   func someFunction() (err error){
		defer Recover(&err)
     // do stuff
   }

func RecoverStack added in v1.5.0

func RecoverStack(err *error)

RecoverStack recovers from a panic and gets the stack trace. This function must be deferred. Usage:

   func someFunction() (err error){
		defer RecoverStack(&err)
     // do stuff
   }

func Unwrap added in v1.3.0

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

This function is an alias to errors.Unwrap in the standard library.

func Wrap

func Wrap(str string, err error) error

Wrap wraps the given error. This does nothing if the error is nil.

Types

type Const added in v1.3.0

type Const string

Const an error that can be used as a constant.

The error message should be in the form of `package: func: error message` `package: error message`

Eg: `encoding/json: Marshal: unsupported type`

const ErrPanic Const = "panic"

ErrPanic a recovered panic

func (Const) Error added in v1.3.0

func (c Const) Error() string

type Error deprecated

type Error = Const

Error an error that can be used as a constant

Deprecated: use errors.Const for constants and New for other uses.

Jump to

Keyboard shortcuts

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