Categorygithub.com/monzo/terrors
modulepackage
0.0.0-20240903123122-f93c6352a31a
Repository: https://github.com/monzo/terrors.git
Documentation: pkg.go.dev

# README

terrors

Build Status GoDoc

Terrors is a package for wrapping Golang errors. Terrors provides additional context to an error, such as an error code and a stack trace.

Terrors is built and used at Monzo.

Usage

Terrors can be used to wrap any object that satisfies the error interface:

terr := terrors.Wrap(err, map[string]string{"context": "my_context"})

Terrors can be instantiated directly:

err := terrors.New("not_found", "object not found", map[string]string{
	"context": "my_context"
})

Terrors offers built-in functions for instantiating Errors with common codes:

err := terrors.NotFound("config_file", "config file not found", map[string]string{
	"context": my_context
})

Terrors provides functions for matching specific Errors:

err := NotFound("handler_missing", "Handler not found", nil)
fmt.Println(Matches(err, "not_found.handler_missing")) // true

Retryability

Terrors contains the ability to declare whether or not an error is retryable. This property is derived from the error code if not specified explicitly.

When using the the wrapping functionality (e.g. Wrap, Augment, Propagate), the retryability of an error is preserved as expected. Importantly, it is also preserved when constructing a new error from a causal error with NewInternalWithCause.

API

Full API documentation can be found on godoc

Install

$ go get -u github.com/monzo/terrors

License

Terrors is licenced under the MIT License

# Packages

totally stolen from https://github.com/stvp/rollbar/blob/master/stack.go.

# Functions

Augment adds context to an existing error.
BadRequest creates a new error to represent an error caused by the client sending an invalid request.
BadResponse creates a new error representing a failure to response with a valid response Examples of this would be a handler returning an invalid message format.
Forbidden creates a new error representing a resource that cannot be accessed with the current authorisation credentials.
InternalService creates a new error to represent an internal service error.
Is checks whether an error is a given code.
IsRetryable returns true if the error is a terror and whether the error was caused by an action which can be retried.
Marshal an error into a protobuf for transmission.
Matches returns true if the error is a terror error and the string returned from error.Error() contains the given param string.
New creates a new error for you.
NewInternalWithCause creates a new Terror from an existing error.
NonRetryableInternalService creates a new error to represent an internal service error.
NotFound creates a new error representing a resource that cannot be found.
PreconditionFailed creates a new error indicating that one or more conditions given in the request evaluated to false when tested on the server.
PrefixMatches returns true if the error is a terror and the string returned from error.Error() starts with the given param string.
Propagate an error without changing it.
RateLimited creates a new error indicating that the request has been rate-limited, and that the caller should back-off.
Timeout creates a new error representing a timeout from client to server.
Unauthorized creates a new error indicating that authentication is required, but has either failed or not been provided.
Unmarshal a protobuf error into a local error.
Wrap takes any error interface and wraps it into an Error.
WrapWithCode wraps an error with a custom error code.

# Constants

Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.
Generic error codes.

# Variables

GenericErrorCodes is a list of all well known generic error codes.

# Structs

Error is terror's error.