Documentation
¶
Overview ¶
Package errorz provides various utilities for working with errors.
Index ¶
- func As[T any](err error) (T, bool)
- func Assertf(cond bool, format string, a ...any)
- func Catch0(f func() error) (outErr error)
- func Catch0Ctx(ctx context.Context, f func(ctx context.Context) error) (outErr error)
- func Catch1[T any](f func() (T, error)) (outV T, outErr error)
- func Catch1Ctx[T any](ctx context.Context, f func(ctx context.Context) (T, error)) (outV T, outErr error)
- func Catch2[T1 any, T2 any](f func() (T1, T2, error)) (outV1 T1, outV2 T2, outErr error)
- func Catch2Ctx[T1 any, T2 any](ctx context.Context, f func(ctx context.Context) (T1, T2, error)) (outV1 T1, outV2 T2, outErr error)
- func Catch3[T1 any, T2 any, T3 any](f func() (T1, T2, T3, error)) (outV1 T1, outV2 T2, outV3 T3, outErr error)
- func Catch3Ctx[T1 any, T2 any, T3 any](ctx context.Context, f func(ctx context.Context) (T1, T2, T3, error)) (outV1 T1, outV2 T2, outV3 T3, outErr error)
- func Errorf(format string, a ...any) error
- func IgnoreClose(c io.Closer)
- func MaybeGetMetadata[T any](err error, k any) (T, bool)
- func MaybeMustWrap(err error, outerErrs ...error)
- func MaybeSetMetadata(err error, k, v any)
- func MaybeWrap(err error, outerErrs ...error) error
- func MaybeWrapRecover(r any, outerErrs ...error) error
- func MustClose(c io.Closer)
- func MustErrorf(format string, a ...any)
- func MustGetMetadata[T any](err error, k any) T
- func MustWrap(innerErr error, outerErrs ...error)
- func SDump(err error) string
- func Unwrap(err error) []error
- func Wrap(err error, outerErrs ...error) error
- func WrapRecover(r any, outerErrs ...error) error
- type ErrorDetails
- type ErrorHTTPStatus
- type ErrorName
- type Frame
- type Frames
- type Summary
- type UnwrapMulti
- type UnwrapSingle
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Assertf ¶
Assertf is like MustErrorf if cond is false, does nothing otherwise.
func Catch0Ctx ¶ added in v0.4.0
Catch0Ctx catches panics in a "func(context.Context) error" closure.
func Catch1Ctx ¶ added in v0.4.0
func Catch1Ctx[T any](ctx context.Context, f func(ctx context.Context) (T, error)) (outV T, outErr error)
Catch1Ctx catches panics in a "func(context.Context) (T, error)" closure.
func Catch2Ctx ¶ added in v0.4.0
func Catch2Ctx[T1 any, T2 any]( ctx context.Context, f func(ctx context.Context) (T1, T2, error), ) (outV1 T1, outV2 T2, outErr error)
Catch2Ctx catches panics in a "func(context.Context) (T1, T2, error)" closure.
func Catch3 ¶ added in v0.4.0
func Catch3[T1 any, T2 any, T3 any](f func() (T1, T2, T3, error)) (outV1 T1, outV2 T2, outV3 T3, outErr error)
Catch3 catches panics in a "func() (T1, T2, T3, error)" closure.
func Catch3Ctx ¶ added in v0.4.0
func Catch3Ctx[T1 any, T2 any, T3 any]( ctx context.Context, f func(ctx context.Context) (T1, T2, T3, error), ) (outV1 T1, outV2 T2, outV3 T3, outErr error)
Catch3Ctx catches panics in a "func(context.Context) (T1, T2, T3, error)" closure.
func IgnoreClose ¶
IgnoreClose calls io.Closer.Close, ignoring the returned error. Handy for the "defer Close" pattern.
func MaybeGetMetadata ¶ added in v0.6.0
MaybeGetMetadata tries to get the given metadata key from the error.
func MaybeMustWrap ¶
MaybeMustWrap is like MustWrap, but does nothing if called with a nil error.
func MaybeSetMetadata ¶ added in v0.6.0
MaybeSetMetadata sets the given metadata (k, v) on the error if it has been wrapped, does nothing otherwise.
func MaybeWrapRecover ¶
MaybeWrapRecover is like WrapRecover but returns nil if called with a nil value.
func MustClose ¶
MustClose calls io.Closer.Close, panicking in case of error. Handy for the "defer Close" pattern.
func MustErrorf ¶
MustErrorf is like Errorf but panics with the wrapped error instead of returning it.
func MustGetMetadata ¶ added in v0.6.0
MustGetMetadata gets the given metadata key from the error, panics if not found or wrong type.
func Unwrap ¶ added in v0.8.0
Unwrap is similar to errors.Unwrap but works for errors implementing either UnwrapSingle and UnwrapMulti.
func WrapRecover ¶
WrapRecover takes a recovered value and converts it to a wrapped error.
Types ¶
type ErrorDetails ¶ added in v0.13.0
type ErrorDetails interface { // GetErrorDetails returns some human-readable information about an error. GetErrorDetails() map[string]any }
ErrorDetails can be implemented by errors to export some additional, human-readable information about the error.
type ErrorHTTPStatus ¶ added in v0.8.0
type ErrorHTTPStatus interface { // GetErrorHTTPStatus returns an HTTP status associated with an error. GetErrorHTTPStatus() int }
ErrorHTTPStatus can be implemented by errors to attach an HTTP status to themselves.
type ErrorName ¶ added in v0.8.0
type ErrorName interface { // GetErrorName returns a human-readable name describing an error. GetErrorName() string }
ErrorName can be implemented by errors to return a name different from their Go type.
type Frame ¶
type Frame struct { Summary string `json:"summary,omitempty"` Location string `json:"location,omitempty"` ShortLocation string `json:"shortLocation,omitempty"` Package string `json:"fullPackage,omitempty"` ShortPackage string `json:"package,omitempty"` Function string `json:"function,omitempty"` FileAndLine string `json:"fileAndLine,omitempty"` File string `json:"file,omitempty"` Line int `json:"line,omitempty"` }
Frame describes a frame.
type Frames ¶
type Frames []*Frame
Frames describes a stack of frames.
func GetFrames ¶
GetFrames returns the frames from the error, or the current frames the error is not wrapped or is nil.
func (Frames) ToSummaries ¶
ToSummaries converts the frames to a slice of frame summaries.
type Summary ¶ added in v0.13.0
type Summary struct { Name string `json:"name,omitempty"` Message string `json:"message,omitempty"` HTTPStatus int `json:"httpStatus,omitempty"` Details map[string]any `json:"details,omitempty"` Components []*Summary `json:"components,omitempty"` }
Summary presents an error in human-readable, debug form.
func GetSummary ¶ added in v0.13.0
GetSummary returns a summary of the error.
type UnwrapMulti ¶
type UnwrapMulti interface { // Unwrap returns one or more wrapped errors, if any, nil otherwise. Unwrap() []error }
UnwrapMulti describes a method which returns multiple errors.
type UnwrapSingle ¶
type UnwrapSingle interface { // Unwrap returns a single wrapped error, if any, nil otherwise. Unwrap() error }
UnwrapSingle describes a method which returns a single error.