errors

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errors provides a structured error implementation that makes it easy to set and inspect error attributes like an error code, message or stack trace.

Errors can be build incrementally by chaining:

New(nil, "abc", InvalidArgument).WithPublicCode(1).WithPublicMessage("message")

Packages/components can define their own functions to create errors more conveniently. A package "xyz" that e.g. usually sets an error code and public message could use the following helper function:

func NewError(code ErrorCode, message string) error {
  return New(nil, "xyz", code).WithPublicMessage(message)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasInternalCode

func HasInternalCode(err error, code int) bool

Returns true if the given error is of type Error and has the given code set as the internal error code.

func HasPublicCode

func HasPublicCode(err error, code int) bool

Returns true if the given error is of type Error and has the given code set as the public error code.

func Is

func Is(err error, code ErrorCode) bool

Retunrs true if the given error is of type Error and has the given ErrorCode set.

func IsAbortedError

func IsAbortedError(err error) bool

func IsAlreadyExistsError

func IsAlreadyExistsError(err error) bool

func IsCancelledError

func IsCancelledError(err error) bool

func IsDeadlineExceededError

func IsDeadlineExceededError(err error) bool

func IsFailedPreconditionError

func IsFailedPreconditionError(err error) bool

func IsInternalError

func IsInternalError(err error) bool

func IsInvalidArgumentError

func IsInvalidArgumentError(err error) bool

func IsNotFoundError

func IsNotFoundError(err error) bool

func IsOutOfRangeError

func IsOutOfRangeError(err error) bool

func IsPermissionDeniedError

func IsPermissionDeniedError(err error) bool

func IsUnauthenticatedError

func IsUnauthenticatedError(err error) bool

func IsUnavailableError

func IsUnavailableError(err error) bool

func IsUnimplementedError

func IsUnimplementedError(err error) bool

func IsUnknownError

func IsUnknownError(err error) bool

func UnstackErrors

func UnstackErrors(e error) []error

Return a slice of all the errors found by traversing inner errors.

Types

type Error

type Error struct {
	Origin string
	// Wrap another error with additional context.
	Inner      error
	StackTrace []byte

	// General error code, see comments on type ErrorCode.
	Code ErrorCode

	// Code or message that is safe to be provided to outside systems/clients/users,
	// e.g. an http handler might return these in the response body if a request fails.
	// Packages can use this to provide more specific error codes, e.g. 5 = "invalid username" or 6 = "password not strong enough".
	// Note that a code should not be 0 since a zero value will be interpreted as no error code set.
	PublicCode    int
	PublicMessage string

	// Code or message for internal use only. These could e.g. be written to application logs.
	// Note that a code should not be 0 since a zero value will be interpreted as no error code set.
	InternalCode    int
	InternalMessage string

	// Any additional key-value pairs can be added here.
	KeyVals map[string]interface{}
}

Structured error that can contain additional context about an error, e.g. the component the error originated in, a strack trace or an internal error message. All fields are optional, although it makes sense to at least provide the origin and a general error code.

func New

func New(inner error, origin string, code ErrorCode) Error

A stack trace is added automatically if the inner error is nil or not of type Error.

func (Error) Error

func (e Error) Error() string

Implement the error interface.

func (Error) ToMap

func (e Error) ToMap() map[string]interface{}

Returns a map containing the non-zero field values of the error. Useful e.g. to log an error in JSON format.

func (Error) With

func (e Error) With(key string, value interface{}) Error

func (Error) WithCode

func (e Error) WithCode(code ErrorCode) Error

func (Error) WithInner

func (e Error) WithInner(inner error) Error

func (Error) WithInternalCode

func (e Error) WithInternalCode(code int) Error

func (Error) WithInternalMessage

func (e Error) WithInternalMessage(message string) Error

func (Error) WithOrigin

func (e Error) WithOrigin(origin string) Error

func (Error) WithPublicCode

func (e Error) WithPublicCode(code int) Error

func (Error) WithPublicMessage

func (e Error) WithPublicMessage(message string) Error

type ErrorCode

type ErrorCode int

Instead of every package defining their own error codes, we can use a list of common error codes that apply to most situations. E.g. an error with code InvalidArgument might be returned by a function if one of its parameter values is invalid or by a http handler if the request body cannot be decoded because it is not in the correct format. Having a small set of common error codes is also useful to e.g. automatically determine an appropriate HTTP response code by inspecting the error returned from some service method. This approach is copied from protobuf/grpc (see e.g. https://grpc.github.io/grpc/core/md_doc_statuscodes.html).

const (
	Unknown ErrorCode = iota
	Cancelled
	// Invalid argument was provided, e.g. an invalid date or malformed string.
	// In contrast to FailedPrecondition the argument is invalid regardless of the state of the system.
	InvalidArgument
	DeadlineExceeded
	NotFound
	AlreadyExists
	// Caller does not have permission to execute the operation.
	PermissionDenied
	// No or invalid authentication credentials were provided.
	Unauthenticated
	// System is not in the correct state to execute the operation.
	// E.g. in a banking application we cannot perform a transfer if an account doesn't contain any money.
	FailedPrecondition
	Aborted
	OutOfRange
	Unimplemented
	Internal
	Unavailable
)

func (ErrorCode) String

func (e ErrorCode) String() string

Jump to

Keyboard shortcuts

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