errors

package
v0.0.0-...-3c5bc29 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusContinue                      = StatusCode(http.StatusContinue)
	StatusSwitchingProtocols            = StatusCode(http.StatusSwitchingProtocols)
	StatusProcessing                    = StatusCode(http.StatusProcessing)
	StatusEarlyHints                    = StatusCode(http.StatusEarlyHints)
	StatusOK                            = StatusCode(http.StatusOK)
	StatusCreated                       = StatusCode(http.StatusCreated)
	StatusAccepted                      = StatusCode(http.StatusAccepted)
	StatusNonAuthoritativeInfo          = StatusCode(http.StatusNonAuthoritativeInfo)
	StatusNoContent                     = StatusCode(http.StatusNoContent)
	StatusResetContent                  = StatusCode(http.StatusResetContent)
	StatusPartialContent                = StatusCode(http.StatusPartialContent)
	StatusMultiStatus                   = StatusCode(http.StatusMultiStatus)
	StatusAlreadyReported               = StatusCode(http.StatusAlreadyReported)
	StatusIMUsed                        = StatusCode(http.StatusIMUsed)
	StatusMultipleChoices               = StatusCode(http.StatusMultipleChoices)
	StatusMovedPermanently              = StatusCode(http.StatusMovedPermanently)
	StatusFound                         = StatusCode(http.StatusFound)
	StatusSeeOther                      = StatusCode(http.StatusSeeOther)
	StatusNotModified                   = StatusCode(http.StatusNotModified)
	StatusUseProxy                      = StatusCode(http.StatusUseProxy)
	StatusTemporaryRedirect             = StatusCode(http.StatusTemporaryRedirect)
	StatusPermanentRedirect             = StatusCode(http.StatusPermanentRedirect)
	StatusBadRequest                    = StatusCode(http.StatusBadRequest)
	StatusUnauthorized                  = StatusCode(http.StatusUnauthorized)
	StatusPaymentRequired               = StatusCode(http.StatusPaymentRequired)
	StatusForbidden                     = StatusCode(http.StatusForbidden)
	StatusNotFound                      = StatusCode(http.StatusNotFound)
	StatusMethodNotAllowed              = StatusCode(http.StatusMethodNotAllowed)
	StatusNotAcceptable                 = StatusCode(http.StatusNotAcceptable)
	StatusProxyAuthRequired             = StatusCode(http.StatusProxyAuthRequired)
	StatusRequestTimeout                = StatusCode(http.StatusRequestTimeout)
	StatusConflict                      = StatusCode(http.StatusConflict)
	StatusGone                          = StatusCode(http.StatusGone)
	StatusLengthRequired                = StatusCode(http.StatusLengthRequired)
	StatusPreconditionFailed            = StatusCode(http.StatusPreconditionFailed)
	StatusRequestEntityTooLarge         = StatusCode(http.StatusRequestEntityTooLarge)
	StatusRequestURITooLong             = StatusCode(http.StatusRequestURITooLong)
	StatusUnsupportedMediaType          = StatusCode(http.StatusUnsupportedMediaType)
	StatusRequestedRangeNotSatisfiable  = StatusCode(http.StatusRequestedRangeNotSatisfiable)
	StatusExpectationFailed             = StatusCode(http.StatusExpectationFailed)
	StatusTeapot                        = StatusCode(http.StatusTeapot)
	StatusMisdirectedRequest            = StatusCode(http.StatusMisdirectedRequest)
	StatusUnprocessableEntity           = StatusCode(http.StatusUnprocessableEntity)
	StatusLocked                        = StatusCode(http.StatusLocked)
	StatusFailedDependency              = StatusCode(http.StatusFailedDependency)
	StatusTooEarly                      = StatusCode(http.StatusTooEarly)
	StatusUpgradeRequired               = StatusCode(http.StatusUpgradeRequired)
	StatusPreconditionRequired          = StatusCode(http.StatusPreconditionRequired)
	StatusTooManyRequests               = StatusCode(http.StatusTooManyRequests)
	StatusRequestHeaderFieldsTooLarge   = StatusCode(http.StatusRequestHeaderFieldsTooLarge)
	StatusUnavailableForLegalReasons    = StatusCode(http.StatusUnavailableForLegalReasons)
	StatusInternalServerError           = StatusCode(http.StatusInternalServerError)
	StatusNotImplemented                = StatusCode(http.StatusNotImplemented)
	StatusBadGateway                    = StatusCode(http.StatusBadGateway)
	StatusServiceUnavailable            = StatusCode(http.StatusServiceUnavailable)
	StatusGatewayTimeout                = StatusCode(http.StatusGatewayTimeout)
	StatusHTTPVersionNotSupported       = StatusCode(http.StatusHTTPVersionNotSupported)
	StatusVariantAlsoNegotiates         = StatusCode(http.StatusVariantAlsoNegotiates)
	StatusInsufficientStorage           = StatusCode(http.StatusInsufficientStorage)
	StatusLoopDetected                  = StatusCode(http.StatusLoopDetected)
	StatusNotExtended                   = StatusCode(http.StatusNotExtended)
	StatusNetworkAuthenticationRequired = StatusCode(http.StatusNetworkAuthenticationRequired)
)

Known status codes.

Variables

This section is empty.

Functions

func Assertf

func Assertf(cond bool, format string, options ...Option)

Assertf is like MustErrorf if cond is false, does nothing otherwise.

func Errorf

func Errorf(format string, options ...Option) error

Errorf formats a new error and wraps it. Note: arguments implementing Option are applied on wrapping, the others are passed to fmt.Errorf().

func FormatStackTrace

func FormatStackTrace(callers []uintptr) []string

FormatStackTrace formats the given raw stack trace.

func GetCallers

func GetCallers(err error) []uintptr

GetCallers returns the raw stack trace from the error, or the current raw stack trace if not found.

func IgnoreClose

func IgnoreClose(c io.Closer)

IgnoreClose calls Close on the given io.Closer, ignoring the returned error. Handy for the defer Close pattern.

func MaybeMustWrap

func MaybeMustWrap(err error, options ...Option)

MaybeMustWrap is like MustWrap, but does nothing if called with a nil error.

func MaybeWrap

func MaybeWrap(err error, options ...Option) error

MaybeWrap is like Wrap, but returns nil if called with a nil error.

func MaybeWrapRecover

func MaybeWrapRecover(r interface{}, options ...Option) error

MaybeWrapRecover is like WrapRecover but returns nil if called with a nil recover.

func MustErrorf

func MustErrorf(format string, options ...Option)

MustErrorf is like Errorf but panics instead of returning the error.

func MustWrap

func MustWrap(err error, options ...Option)

MustWrap is like Wrap, but panics if the given error is non-nil.

func Safe

func Safe(f func() error) func() error

Safe calls the function catching any panic and returning it as error.

func Unwrap

func Unwrap(err error) error

Unwrap undoes Wrap, returning the original error.

func Wrap

func Wrap(err error, options ...Option) error

Wrap wraps the given error, applying the given options.

func WrapRecover

func WrapRecover(r interface{}, options ...Option) error

WrapRecover takes a recovered interface{} and converts it to a wrapped error.

Types

type Args

type Args []interface{}

Args describes a list of args used for formatting an error message.

func A

func A(a ...interface{}) Args

A is a shorthand builder for args.

func (Args) Apply

func (a Args) Apply(err error)

Apply implements the Option interface.

type ErrorResponse

type ErrorResponse struct {
	ErrorID    ID                     `json:"errorId,omitempty"`
	StatusCode StatusCode             `json:"statusCode,omitempty"`
	Metadata   map[string]interface{} `json:"metadata,omitempty"`
	Message    string                 `json:"message,omitempty"`
	StackTrace []string               `json:"stackTrace,omitempty"`
}

ErrorResponse describes an error response.

func ToResponse

func ToResponse(err error) *ErrorResponse

ToResponse converts an error to ErrorResponse.

type ID

type ID string

ID describes an error id.

func GetID

func GetID(err error) ID

GetID gets the id from the error, or an empty id if not set.

func (ID) Apply

func (id ID) Apply(err error)

Apply implements the Option interface.

func (ID) In

func (id ID) In(err error) bool

In returns true if the given error has this ID.

func (ID) String

func (id ID) String() string

String implements the fmt.Stringer interface.

type Metadata

type Metadata map[string]interface{}

Metadata describes metadata which can be attached to errors.

func GetMetadata

func GetMetadata(err error) Metadata

GetMetadata gets the metadata from the error, nil if not found.

func (Metadata) Apply

func (m Metadata) Apply(err error)

Apply implements the Option interface.

func (Metadata) Get

func (m Metadata) Get(k string) interface{}

Get gets a value from Metadata, nil if not found or Metadata is nil.

func (Metadata) GetString

func (m Metadata) GetString(k string) string

GetString gets a value from Metadata as string, empty if not found or of different type.

type Option

type Option interface {
	Apply(err error)
}

Option describes an option which can be applied to an error.

type OptionFunc

type OptionFunc func(err error)

OptionFunc describes an option which can be applied to an error.

func M

func M(k string, v interface{}) OptionFunc

M is a shorthand for providing metadata to errors.

func Prefix

func Prefix(format string, a ...interface{}) OptionFunc

Prefix adds a prefix to the error message.

func Skip

func Skip() OptionFunc

Skip skips the caller from the stack trace.

func (OptionFunc) Apply

func (f OptionFunc) Apply(err error)

Apply implements the Option interface.

type StatusCode

type StatusCode int

StatusCode describes an HTTP status code.

func GetStatusCode

func GetStatusCode(err error) StatusCode

GetStatusCode gets the status code from the error, or 0 if not set.

func GetStatusCodeOr500

func GetStatusCodeOr500(err error) StatusCode

GetStatusCodeOr500 gets the status code from the error, or InternalServerError if not set.

func (StatusCode) Apply

func (s StatusCode) Apply(err error)

Apply implements the Option interface.

func (StatusCode) In

func (s StatusCode) In(err error) bool

In returns true if the given error has this status code.

func (StatusCode) Int

func (s StatusCode) Int() int

Int returns the status code as "int".

func (StatusCode) String

func (s StatusCode) String() string

String implements the fmt.Stringer interface.

Jump to

Keyboard shortcuts

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