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

# README

Package errors

Project status Build Status Go Report Card GoDoc License

Package errors is an errors wrapping package to help propagate and chain errors as well as attach stack traces, tags(additional information) and even a Type classification system to categorize errors into types eg. Permanent vs Transient.

It is a drop in replacement for the std Go errors package. It is also 100% compatible with the Is, As and Unwrap interfaces used within the std library and can be mixed with the built-in error wrapping.

Common Questions

Why another package? There are two main reasons.

  • I think that the programs generating the original error(s) should be responsible for adding internal metadata and details to them. Ultimately developers are concerned with:

    • If the error is Transient or Permanent for retries.
    • Additional details for logging.
  • IMO most of the existing packages either don't take the error handling far enough, too far or down right unfriendly to use/consume.

Features

  • works with go-playground/log, the Tags will be added as Field Key Values and Types will be concatenated as well when using WithError
  • helpers to extract and classify error types using RegisterHelper(...), many already existing such as ioerrors, neterrors, awserrors...
  • built in helpers only need to be imported, eg. _ github.com/go-playground/errors/v5/helpers/neterrors allowing libraries to register their own helpers not needing the caller to do or guess what needs to be imported.

Installation

Use go get.

go get -u github.com/go-playground/errors/v5

Usage

package main

import (
	"fmt"
	"io"

	"github.com/go-playground/errors/v5"
)

func main() {
	err := level1("testing error")
	fmt.Println(err)
	if errors.HasType(err, "Permanent") {
		// os.Exit(1)
		fmt.Println("it is a permanent error")
	}

	// root error
	cause := errors.Cause(err)
	fmt.Println("CAUSE:", cause)

	// can even still inspect the internal error
	fmt.Println(errors.Cause(err) == io.EOF) // will extract the cause for you
	fmt.Println(errors.Cause(cause) == io.EOF)

	// and still in a switch
	switch errors.Cause(err) {
	case io.EOF:
		fmt.Println("EOF error")
	default:
		fmt.Println("unknown error")
	}
}

func level1(value string) error {
	if err := level2(value); err != nil {
		return errors.Wrap(err, "level2 call failed")
	}
	return nil
}

func level2(value string) error {
	err := io.EOF
	return errors.Wrap(err, "failed to do something").AddTypes("Permanent").AddTags(errors.T("value", value))
}

Package Versioning

Using Go modules and proper semantic version releases (as always).

License

Distributed under MIT License, please see license file in code for more details.

# Packages

No description provided by the author

# Functions

As is to allow this library to be a drop-in replacement to the std library.
Cause extracts and returns the root wrapped error (the naked error with no additional information).
HasType is a helper function that will recurse up from the root error and check that the provided type is present using an equality check.
Is allows this library to be a drop-in replacement to the std library.
Join allows this library to be a drop-in replacement to the std library.
LookupTag recursively searches for the provided tag and returns its value or nil.
New creates an error with the provided text and automatically wraps it with line information.
Newf creates an error with the provided text and automatically wraps it with line information.
RegisterErrorFormatFn sets a custom error formatting function in order for the error output to be customizable.
RegisterHelper adds a new helper function to extract Type and Tag information.
T is a shortcut to make a Tag.
Wrap encapsulates the error, stores a contextual prefix and automatically obtains a stack trace.
Wrapf encapsulates the error, stores a contextual prefix and automatically obtains a stack trace.
WrapSkipFrames is a special version of Wrap that skips extra n frames when determining error location.

# Structs

Link contains a single error entry contained in an error Chain.
Tag contains a single key value combination to be attached to your error.

# Type aliases

Chain contains the chained errors, the links, of the chains if you will.
ErrorFormatFn represents the error formatting function for a Chain of errors.
Helper is a function which will automatically extract Type and Tag information based on the supplied err and add it to the supplied *Link error; this can be used independently or by registering using errors.RegisterHelper(...), which will run the registered helper every time errors.Wrap(...) is called.