oops

package
v0.0.0-...-34c5bf1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Is

func Is(err error, code Code) bool

Is returns whether the code matches that of the error

Types

type Code

type Code string

Code is an identifier of a particular class of error

const (
	ErrBadRequest         Code = "bad_request"
	ErrForbidden          Code = "forbidden"
	ErrInternalService    Code = "internal_service"
	ErrNotFound           Code = "not_found"
	ErrPreconditionFailed Code = "precondition_failed"
	ErrTimeout            Code = "timeout"
	ErrUnauthorized       Code = "unauthorized"
)

Generic error codes. Each of these has their own constructor for convenience.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is a custom error type that implements Go's error interface

func BadRequest

func BadRequest(format string, a ...interface{}) *Error

BadRequest creates a new error to represent an error caused by the client sending an invalid request. This is non-retryable unless the request is modified.

func Forbidden

func Forbidden(format string, a ...interface{}) *Error

Forbidden creates a new error representing a resource that cannot be accessed with the current authorisation credentials. The user may need authorising, or if authorised, may not be permitted to perform this action.

func FromHTTPStatus

func FromHTTPStatus(status int, format string, a ...interface{}) *Error

FromHTTPStatus returns an error where the code is derived from the HTTP status code

func InternalService

func InternalService(format string, a ...interface{}) *Error

InternalService creates a new error to represent an internal service error

func NotFound

func NotFound(format string, a ...interface{}) *Error

NotFound creates a new error representing a resource that cannot be found

func PreconditionFailed

func PreconditionFailed(format string, a ...interface{}) *Error

PreconditionFailed creates a new error indicating that one or more conditions given in the request evaluated to false when tested on the server

func Timeout

func Timeout(format string, a ...interface{}) *Error

Timeout creates a new error representing a timeout from client to server

func Unauthorized

func Unauthorized(format string, a ...interface{}) *Error

Unauthorized creates a new error indicating that authentication is required, but has either failed or not been provided.

func WithCode

func WithCode(err interface{}, code Code) *Error

WithCode wraps the error with a new code

func WithMessage

func WithMessage(err interface{}, format string, a ...interface{}) *Error

WithMessage wraps the error with an extra message

func WithMetadata

func WithMetadata(err interface{}, metadata map[string]string) *Error

WithMetadata will wrap the error with extra metadata

func Wrap

func Wrap(err interface{}, code Code, format string, a ...interface{}) *Error

Wrap wraps the given error. Optionally, the last parameter can be a map[string]string containing metadata. If the error-to-wrap is already an *Error, the metadata will me merged and the existing code will remain the same. If the error is not an *Error, the code will default to ErrInternalService.

func (*Error) Error

func (e *Error) Error() string

Error returns a string message of the error

func (*Error) GetCode

func (e *Error) GetCode() Code

GetCode unwinds the error stack and returns the first code encountered. If no codes exist, then ErrInternalService is returned.

func (*Error) GetMessage

func (e *Error) GetMessage() string

GetMessage returns the error's message. If there is a cause, its error message is appended after a colon, and so on.

func (*Error) GetMetadata

func (e *Error) GetMetadata() map[string]string

GetMetadata returns the metadata map of the error

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus returns an appropriate HTTP status code to use when returning the error in a response

func (*Error) Matches

func (e *Error) Matches(match string) bool

Matches returns whether the string returned from error.Error() contains the given param string

func (*Error) StackTrace

func (e *Error) StackTrace() pkgerrors.StackTrace

StackTrace returns the stack of Frames from innermost (newest) to outermost (oldest)

Jump to

Keyboard shortcuts

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