errors

package module
v0.0.0-...-6ed4b1a Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2016 License: MIT Imports: 10 Imported by: 1

README

Since this page would just duplicate the documentation, simply go to https://godoc.org/github.com/soul9/errors to see examples and documentation.

Documentation

Overview

Package errors provides a list of errors that have stack-traces, and formattable errors that can be compared.

This is particularly useful when you want to understand the state of execution when an error was returned unexpectedly.

It provides the type *Errors which implements the standard golang error interface, so you can use this library interchangably with code that is expecting a normal error return.

It also provides two extra types: *Errf and *Errors. I wanted to be able to have comparable, more helpful errors so *Errf is a closure for Errorf that stores an error type, so each time an error made with Newf is called, it will return an error that compares as true to the original Errf, while adding extra information to the error message. I also wanted to be able to chain errors together for the case when an error is not fatal for a function, but I still want to store it which is what the *Errors type does.

The Error() function of the *Errors type displays the StackTrace by default.

For example:

package crashy

import "github.com/soul9/errors"

var Crashed = errors.Newf("oh %s")

func Crash(s string) error {
    return Crashed(s)
}

This can be called as follows:

package main

import (
    "crashy"
    "fmt"
    "github.com/soul9/errors"
)

func main() {
    err := crashy.Crash("dear")
    err = errors.Add(err, crashy.Crash("my"))
    if err != nil {
        if errors.Is(err, crashy.Crashed()) {
            fmt.Println(err)
        } else {
            panic(err)
        }
    }
}

This package was original written to allow reporting to Bugsnag, but after I found similar packages by Facebook and Dropbox, it was moved to one canonical location so everyone can benefit.

Index

Constants

This section is empty.

Variables

View Source
var (

	// The maximum number of stackframes on any error.
	MaxStackDepth = 50
)

Functions

func Add

func Add(e interface{}, ee interface{}) error

Add returns a list of errors that contains both parameters, no matter their error type.

func Errorf

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

Errorf creates a new error with the given message. You can use it as a drop-in replacement for fmt.Errorf() to provide descriptive errors in return values.

func Is

func Is(e error, original error) bool

Is detects whether the error is equal to a given error. Errors are considered equal by this function if they are the same object, or if they both contain the same error inside an errors.Error, or if they have the same id inside an errors.Error, or if one of the errors in an errors.Errors Is the same as the error.

func IsExist

func IsExist(err error) bool

IsExist checks for os.IsExist on provided error, whether it is an *Errors, *Error, or error

func IsFunc

func IsFunc(fn func(error) bool, err error) bool

IsFunc try-casts err for *Error or *Errors, and checks the underlying error(s) against provided fn. If error is not of type *Error or *Errors, IsFunc simply calls fn(err)

func IsNotExist

func IsNotExist(err error) bool

IsNotExist checks for os.IsNotExist on provided error, whether it is an *Errors, *Error, or error

func IsPermission

func IsPermission(err error) bool

IsPermission checks for os.IsPermission on provided error, whether it is an *Errors, *Error, or error

func New

func New(err interface{}) error

New returns a list of errors with the parameter added to the list.

func NewError

func NewError(e interface{}) error

NewError makes an Error from the given value. If that value is already an error then it will be used directly, if not, it will be passed to fmt.Errorf("%v"). The stacktrace will point to the line of code that called New.

func Wrap

func Wrap(e interface{}, skip int) error

Wrap makes an Error from the given value. If that value is already an error then it will be used directly, if not, it will be passed to fmt.Errorf("%v"). The skip parameter indicates how far up the stack to start the stacktrace. 0 is from the current call, 1 from its caller, etc.

func WrapPrefix

func WrapPrefix(e interface{}, prefix string, skip int) error

WrapPrefix makes an Error from the given value. If that value is already an error then it will be used directly, if not, it will be passed to fmt.Errorf("%v"). The prefix parameter is used to add a prefix to the error message when calling Error(). The skip parameter indicates how far up the stack to start the stacktrace. 0 is from the current call, 1 from its caller, etc.

Types

type Errf

type Errf func(...interface{}) error

Errf is a closure around Errorf to provide comparable but descriptive errors

func Newf

func Newf(s string) Errf

Newf with a format string returns a closure on top of Errorf that can be called with parameters to provide helpful error messages, while retaining the ability to be compared.

func (Errf) Error

func (e Errf) Error() string

type Error

type Error struct {
	Err error
	// contains filtered or unexported fields
}

Error is an error with an attached stacktrace. It can be used wherever the builtin error interface is expected.

func ParsePanic

func ParsePanic(text string) (*Error, error)

ParsePanic allows you to get an error object from the output of a go program that panicked. This is particularly useful with https://github.com/mitchellh/panicwrap.

func (*Error) Error

func (err *Error) Error() string

Error returns the underlying error's message.

func (*Error) ErrorStack

func (err *Error) ErrorStack() string

ErrorStack returns a string that contains both the error message and the callstack. Satisfies the ErrorStacker interface.

func (*Error) Is

func (e *Error) Is(original error) bool

Is satisfies the ErrorStacker interface, it is a wrapper to Is(error, error)

func (*Error) Stack

func (err *Error) Stack() []byte

Stack returns the callstack formatted the same way that go does in runtime/debug.Stack()

func (*Error) StackFrames

func (err *Error) StackFrames() []StackFrame

StackFrames returns an array of frames containing information about the stack.

func (*Error) TypeName

func (err *Error) TypeName() string

TypeName returns the type this error. e.g. *errors.stringError.

type ErrorStacker

type ErrorStacker interface {
	error
	ErrorStack() string
	Is(error) bool
}

ErrorStacker is an interface satisfying the error interface that is also aware of the stackcall of the error, printed with ErrorStack(), and able to compare itself to another error with Is(error)

type Errors

type Errors struct {
	// contains filtered or unexported fields
}

Errors is a list of errors with stack traces. It implements the error interface

func (*Errors) Add

func (e *Errors) Add(ee interface{}) error

Add returns a list of errors with the parameter added to the receiver, it will behave correctly with a simple error, as well as with an errors.Error and an errors.Errors as parameters. It will also log the error using glog if a verbosity of 3 or more is specified.

func (*Errors) Addf

func (e *Errors) Addf(fmts string, args ...interface{}) error

Addf is a wrapper around Add to simply add a descriptive error to the list.

func (*Errors) Error

func (e *Errors) Error() string

Error returns all the error messages of the included errors.

func (*Errors) ErrorStack

func (e *Errors) ErrorStack() string

ErrorStack returns all the stack traces and error messages of the included errors. Satisfies the ErrorStacker interface.

func (*Errors) Is

func (e *Errors) Is(ee error) bool

Is checks whether the parameter error is contained in the list of errors. If the parameter is an errors.Errors, it will check whether at least one of their errors match. Satisfies the ErrorStacker interface.

type StackFrame

type StackFrame struct {
	// The path to the file containing this ProgramCounter
	File string
	// The LineNumber in that file
	LineNumber int
	// The Name of the function that contains this ProgramCounter
	Name string
	// The Package that contains this function
	Package string
	// The underlying ProgramCounter
	ProgramCounter uintptr
}

A StackFrame contains all necessary information about to generate a line in a callstack.

func NewStackFrame

func NewStackFrame(pc uintptr) (frame StackFrame)

NewStackFrame popoulates a stack frame object from the program counter.

func (*StackFrame) Func

func (frame *StackFrame) Func() *runtime.Func

Func returns the function that contained this frame.

func (*StackFrame) SourceLine

func (frame *StackFrame) SourceLine() (string, error)

SourceLine gets the line of code (from File and Line) of the original source if possible.

func (*StackFrame) String

func (frame *StackFrame) String() string

String returns the stackframe formatted in the same way as go does in runtime/debug.Stack()

Jump to

Keyboard shortcuts

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