Categorygithub.com/go-errors/errors
modulepackage
1.5.1
Repository: https://github.com/go-errors/errors.git
Documentation: pkg.go.dev

# README

go-errors/errors

Build Status

Package errors adds stacktrace support to errors in go.

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

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

Usage

Full documentation is available on godoc, but here's a simple example:

package crashy

import "github.com/go-errors/errors"

var Crashed = errors.Errorf("oh dear")

func Crash() error {
    return errors.New(Crashed)
}

This can be called as follows:

package main

import (
    "crashy"
    "fmt"
    "github.com/go-errors/errors"
)

func main() {
    err := crashy.Crash()
    if err != nil {
        if errors.Is(err, crashy.Crashed) {
            fmt.Println(err.(*errors.Error).ErrorStack())
        } else {
            panic(err)
        }
    }
}

Meta-fu

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

This package is licensed under the MIT license, see LICENSE.MIT for details.

Changelog

  • v1.1.0 updated to use go1.13's standard-library errors.Is method instead of == in errors.Is
  • v1.2.0 added errors.As from the standard library.
  • v1.3.0 BREAKING updated error methods to return error instead of *Error.

Code that needs access to the underlying *Error can use the new errors.AsError(e)

  // before
  errors.New(err).ErrorStack()
  // after
.  errors.AsError(errors.Wrap(err)).ErrorStack()
  • v1.4.0 BREAKING v1.4.0 reverted all changes from v1.3.0 and is identical to v1.2.0
  • v1.4.1 no code change, but now without an unnecessary cover.out file.
  • v1.4.2 performance improvement to ErrorStack() to avoid unnecessary work https://github.com/go-errors/errors/pull/40
  • v1.5.0 add errors.Join() and errors.Unwrap() copying the stdlib https://github.com/go-errors/errors/pull/40
  • v1.5.1 fix build on go1.13..go1.19 (broken by adding Join and Unwrap with wrong build constraints)

# Functions

As finds the first error in err's tree that matches target, and if one is found, sets target to that error value and returns true.
Errorf creates a new error with the given message.
Is detects whether the error is equal to a given error.
Join returns an error that wraps the given errors.
New makes an Error from the given value.
NewStackFrame popoulates a stack frame object from the program counter.
ParsePanic allows you to get an error object from the output of a go program that panicked.
Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error.
Wrap makes an Error from the given value.
WrapPrefix makes an Error from the given value.

# Variables

The maximum number of stackframes on any error.

# Structs

Error is an error with an attached stacktrace.
A StackFrame contains all necessary information about to generate a line in a callstack.