errio

package
Version: v0.33.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2021 License: Apache-2.0 Imports: 4 Imported by: 10

Documentation

Overview

Package errio contains custom error types to easily transfer errors between applications and output them to the user in a consistent way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equals added in v0.23.0

func Equals(a PublicError, b error) bool

Equals returns whether the errors have matching namespace and code.

func EqualsAPIError added in v0.32.0

func EqualsAPIError(apiErr PublicStatusError, err error) bool

EqualsAPIError checks whether the given error has the namespace and code of the given API error. The HTTP status code and error message aren't checked, so this function is compatible with any changes to the message and HTTP status code.

func Error

func Error(err error) error

Error can be called to on any error to convert it to a PublicError if it is not already. If it is not yet a PublicError, an UnexpectedError is returned

func IsKnown

func IsKnown(err error) bool

IsKnown checks whether the given error is known.

func StatusError

func StatusError(err error) error

StatusError can be called to on any error to convert it to a PublicStatusError if it is not already. If it is not yet a PublicError, an UnexpectedError is returned

Types

type ErrorCode

type ErrorCode struct {
	Code      string
	Namespace Namespace
}

ErrorCode contains a code that should be unique to the namespace it belongs to

func (ErrorCode) Error

func (c ErrorCode) Error(message string) PublicError

Error returns a PublicError with the given code and message

func (ErrorCode) ErrorPref

func (c ErrorCode) ErrorPref(message string) func(args ...interface{}) PublicError

ErrorPref returns a function that can be called with arguments to create a formatted error message

func (ErrorCode) Errorf

func (c ErrorCode) Errorf(message string, args ...interface{}) PublicError

Errorf works like fmt.Errorf to create an Error.

func (ErrorCode) StatusError

func (c ErrorCode) StatusError(message string, status int) PublicStatusError

StatusError creates a new PublicStatusError

func (ErrorCode) StatusErrorPref added in v0.21.0

func (c ErrorCode) StatusErrorPref(message string, status int) func(args ...interface{}) PublicStatusError

StatusErrorPref returns a function that can be called with arguments to create a formatted status error message

func (ErrorCode) StatusErrorf

func (c ErrorCode) StatusErrorf(message string, status int, args ...interface{}) PublicStatusError

StatusErrorf works like fmt.Errorf to create a StatusError

type Namespace

type Namespace string

Namespace is a container for different errors and is used to distinguish between error codes. Using different namespaces helps you to keep error codes unique throughout your codebase. Typically, namespaces will carry the name of their package name or file name (without the .go part).

func (Namespace) Code

func (n Namespace) Code(code string) ErrorCode

Code returns a new ErrorCode

type PublicError

type PublicError struct {
	Namespace Namespace `json:"namespace,omitempty"`
	Code      string    `json:"code"`
	Message   string    `json:"message"`
	// contains filtered or unexported fields
}

PublicError is a wrapper around an error code and a error message. This allows clear error messaging and trace ability.

func UnexpectedError

func UnexpectedError(err error) PublicError

UnexpectedError represents an error that we did not expect. Unexpected errors are reported and logged.

func (PublicError) Append

func (e PublicError) Append(errs ...error) PublicError

Append appends multiple errors to an PublicError.

func (PublicError) Error

func (e PublicError) Error() string

PublicError implements the error interface.

func (PublicError) Type

func (e PublicError) Type() string

Type returns the type of the error as to be reported to Sentry.

func (PublicError) Unwrap added in v0.32.0

func (e PublicError) Unwrap() error

Unwrap returns the wrapped error if the PublicError represents an error wrapped as an UnexpectedError.

type PublicStatusError

type PublicStatusError struct {
	PublicError `json:"error"`
	StatusCode  int `json:"-"`
}

PublicStatusError represents an http error. It contains an HTTP status code and can be json encoded in an HTTP response.

func UnexpectedStatusError

func UnexpectedStatusError(err error) PublicStatusError

UnexpectedStatusError is an error we did not expect, with http.StatusInternalServerError attached to it.

func Wrap

func Wrap(base PublicStatusError, errs ...error) PublicStatusError

Wrap wraps multiple errors with a PublicStatusError.

func (PublicStatusError) Append

func (e PublicStatusError) Append(errs ...error) PublicStatusError

Append appends multiple errors to a PublicStatusError

func (PublicStatusError) Error

func (e PublicStatusError) Error() string

Error implements the error interface.

func (PublicStatusError) Type

func (e PublicStatusError) Type() string

Type returns the type of the error as to be reported to Sentry.

Source Files

Jump to

Keyboard shortcuts

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