errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2024 License: MPL-2.0 Imports: 3 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(current error, next ...error) error

Append appends the given errors to the current error.

func Embed

func Embed[Data any](err error, key string, value Data) error

Embed returns a new error with the supplied data embedded in the provided error.

func Expand added in v0.2.0

func Expand(err error) []error

Expand will expand the provider error into multiple errors if it is a multi error. Otherwise, it will return a slice containing the single error.

func GetAllEmbeddedData added in v0.2.0

func GetAllEmbeddedData(err error) map[string]interface{}

GetAllEmbeddedData returns all embedded data from an error if it exists. This function will automatically unwrap errors that are wrapped in other errors to find all metadata associated with the error.

func GetEmbeddedData

func GetEmbeddedData[Data any](err error, key string) (Data, bool)

GetEmbeddedData returns the embedded data from an error if it exists. This function will automatically unwrap errors that are wrapped in other errors to find all metadata associated with the error.

func GetEmbeddedDataUnsafe added in v0.2.0

func GetEmbeddedDataUnsafe(err error, key string) interface{}

GetEmbeddedDataUnsafe returns the embedded data from an error if it exists. This function will automatically unwrap errors that are wrapped in other errors to find all metadata associated with the error.

func Is

func Is(err error, code ErrorCode) bool

Is validates the error has the supplied error code.

func New

func New(err error, code ErrorCode, text string) error

New returns an error with a new error code, optionally wrapping an existing error.

For example: errors.New(nil, ErrorCodeNotFound, "resource not found") For example: errors.New(err, ErrorCodeNotFound, "resource not found")

func Newf

func Newf(err error, code ErrorCode, format string, args ...any) error

Newf returns an error with a new error code, optionally wrapping an existing error.

This matches New, but with a formatted message.

func Unwrap

func Unwrap(err error) error

Unwrap returns the unwrapped error if it exists.

func Wrap

func Wrap(err error, text string) error

Wrap returns an error wrapping an existing error.

For example: errors.Wrap(err, "failed to read file")

func Wrapf

func Wrapf(err error, format string, args ...any) error

Wrapf returns an error wrapping an existing error.

This matches Wrap, but with a formatted message.

Types

type Codeable

type Codeable interface {
	error
	GetErrorCode() ErrorCode
}

Codeable is an interface that can be implemented by errors that have an error code.

type DataContainer

type DataContainer interface {
	error
	EmbeddedData() map[string]interface{}
}

DataContainer is an interface for errors that contain embedded data.

type DataContainingError

type DataContainingError struct {
	*WrappingError
	// contains filtered or unexported fields
}

DataContainingError is an extension to the WrappingError struct that contains embedded data.

func (*DataContainingError) EmbeddedData added in v0.2.0

func (d *DataContainingError) EmbeddedData() map[string]interface{}

EmbeddedData implements the DataContainer interface.

type ErrorCode

type ErrorCode string

ErrorCode represents a code that can be used to identify the type of error.

const (
	// ErrorCodeUnknown is the default error code returned when an error hasn't been assigned a code.
	ErrorCodeUnknown ErrorCode = "ErrorCodeUnknown"

	// ErrorCodeOk is the error code returned when an error is nil.
	ErrorCodeOk ErrorCode = "ErrorCodeOk"

	// ErrorCodeWrapped is the error code returned when an error is wrapped by another error.
	ErrorCodeWrapped ErrorCode = "ErrorCodeWrapped"

	// ErrorCodeMulti is the error code returned when an error contains multiple errors.
	ErrorCodeMulti ErrorCode = "ErrorCodeMulti"
)

func GetErrorCode

func GetErrorCode(err error) ErrorCode

GetErrorCode returns the error code of the given error.

type Unwrappable

type Unwrappable interface {
	error
	Unwrap() error
}

Unwrappable is an interface for errors that can be unwrapped.

type WrappingError

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

WrappingError is an error that can wrap another error.

func (*WrappingError) Error

func (e *WrappingError) Error() string

Error implements the error interface.

func (*WrappingError) GetErrorCode

func (e *WrappingError) GetErrorCode() ErrorCode

GetErrorCode implements the Codeable interface.

func (*WrappingError) Unwrap

func (e *WrappingError) Unwrap() error

Unwrap implements the Unwrappable interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳