errors – emperror.dev/errors Index | Examples | Files | Directories

package errors

import "emperror.dev/errors"

Package errors is a drop-in replacement for the standard errors package and github.com/pkg/errors.

Index

Examples

Functions

func As

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

As finds the first error in err's chain that matches the type to which target points, and if so, sets the target to its value and returns true. An error matches a type if it is assignable to the target type, or if it has a method As(interface{}) bool such that As(target) returns true. As will panic if target is not a non-nil pointer to a type which implements error or is of interface type.

The As method should set the target to its value and return true if err matches the type to which target points.

Example

Code:play 

package main

import (
	"fmt"
	"os"

	"emperror.dev/errors"
)

func main() {
	if _, err := os.Open("non-existing"); err != nil {
		var pathError *os.PathError
		if errors.As(err, &pathError) {
			fmt.Println("Failed at path:", pathError.Path)
		} else {
			fmt.Println(err)
		}
	}

}

Output:

Failed at path: non-existing

func Cause

func Cause(err error) error

Cause returns the last error (root cause) in an error chain. If the error has no cause, it is returned directly.

It supports both Go 1.13 errors.Wrapper and github.com/pkg/errors.Causer interfaces (the former takes precedence).

func Errorf

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

Errorf returns a new error with a formatted message and annotated with stack trace at the point Errorf is called.

func Is

func Is(err, target error) bool

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

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.

func New

func New(message string) error

New returns a new error annotated with stack trace at the point New is called.

func NewPlain

func NewPlain(message string) error

NewPlain returns a simple error without any annotated context, like stack trace. Useful for creating sentinel errors and in testing.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err implements Unwrap. Otherwise, Unwrap returns nil.

It supports both Go 1.13 Unwrap and github.com/pkg/errors.Causer interfaces (the former takes precedence).

func WithMessage

func WithMessage(err error, message string) error

WithMessage annotates err with a new message. If err is nil, WithMessage returns nil.

func WithMessagef

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

WithMessagef annotates err with the format specifier. If err is nil, WithMessagef returns nil.

func WithStack

func WithStack(err error) error

WithStack annotates err with a stack trace at the point WithStack was called. If err is nil, WithStack returns nil.

func WithStackDepth

func WithStackDepth(err error, depth int) error

WithStackDepth annotates err with a stack trace at the given call depth. Zero identifies the caller of WithStackDepth itself. If err is nil, WithStackDepth returns nil.

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

func Wrapf

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

Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. If err is nil, Wrapf returns nil.

Types

type Frame

type Frame = errors.Frame

Frame represents a program counter inside a stack frame. For historical reasons if Frame is interpreted as a uintptr its value represents the program counter + 1.

It is an alias of the same type in github.com/pkg/errors.

type StackTrace

type StackTrace = errors.StackTrace

StackTrace is stack of Frames from innermost (newest) to outermost (oldest).

It is an alias of the same type in github.com/pkg/errors.

Source Files

errors.go stack.go wrap.go wrap_go1_13.go

Directories

PathSynopsis
tests
Version
v0.1.0
Published
Jul 12, 2019
Platform
windows/amd64
Imports
5 packages
Last checked
now

Tools for package owners.