Documentation
¶
Overview ¶
Package failure provides an error handling library for Go. It allows you to create, wrap, and handle errors with additional context and features.
Index ¶
- Constants
- func CauseOf(err error) error
- func CodeOf(err error) any
- func ForceUnwrap(err error) error
- func Is[C Code](err error, code ...C) bool
- func MarkUnexpected(err error, fields ...Field) error
- func New[C Code](c C, fields ...Field) error
- func OriginValue[K comparable](err error, key K) any
- func OriginValueAs[V any, K comparable](err error, key K) (zero V, _ bool)
- func Translate[C Code](err error, c C, fields ...Field) error
- func Unexpected(text string, fields ...Field) error
- func Value[K comparable](err error, key K) any
- func ValueAs[V any, K comparable](err error, key K) (zero V, _ bool)
- func Wrap(err error, fields ...Field) error
- type CallStack
- type Code
- type Context
- type ErrorFormatter
- type ErrorWriter
- type Failure
- type Field
- type FieldSetter
- type Frame
- type Message
Constants ¶
const ( KeyCode key = iota + 1 KeyContext KeyMessage KeyCallStack )
Variables ¶
This section is empty.
Functions ¶
func CauseOf ¶
CauseOf retrieves the cause error in the error chain. The errors wrapped with MarkUnexpected is not returned. To unwrap these errors, use ForceUnwrap in a loop.
func ForceUnwrap ¶
ForceUnwrap unwraps the error, returning the underlying error. If the error does not implement ForceUnwrap method, it uses errors.Unwrap. It is useful for logging the actual cause of the error.
func Is ¶
Is checks if the error has any of the specified codes. It returns true if a matching code is found.
func MarkUnexpected ¶
MarkUnexpected creates a new error that cannot be unwrapped using the standard Unwrap method. Use this function when you want to prevent propagating data like error codes or context to the caller. However, using ForceUnwrap will still allow retrieving the original error.
func OriginValue ¶
func OriginValue[K comparable](err error, key K) any
OriginValue retrieves the original (firstly set) value associated with the specified key from the given error. It forcefully unwraps the error using ForceUnwrap until it finds a matching key or reaches the end of the error chain.
func OriginValueAs ¶
func OriginValueAs[V any, K comparable](err error, key K) (zero V, _ bool)
OriginValueAs is utility for OriginValue that also asserts that the value has the specified type V. If the value is not of the expected type, it panics.
func Translate ¶
Translate creates a new error by translating the error code of an existing error. It wraps the original error with the new error code and optional fields.
func Unexpected ¶
Unexpected creates a new error with the provided text and optional fields. Use this function when you want to create without an error code. This function should only be used when the error is not expected to occur.
func Value ¶
func Value[K comparable](err error, key K) any
Value retrieves the value associated with the specified key from the given error. It unwraps the error until it finds a matching key or reaches the end of the error chain.
Types ¶
type CallStack ¶
type CallStack []uintptr
CallStack represents a stack of program counters. It implements the Field interface.
func CallStackOf ¶
CallStackOf retrieves a CallStack associated with the given error.
func Callers ¶
Callers returns a CallStack of the caller's goroutine stack. The skip parameter determines the number of stack frames to skip before capturing the CallStack.
func NewCallStack ¶
NewCallStack creates a new CallStack from the provided program counters.
func (CallStack) Frames ¶
Frames is a method of CallStack that returns a slice of Frame objects representing the CallStack's frames.
func (CallStack) HeadFrame ¶
HeadFrame is a method of CallStack that returns the first frame in the CallStack. If the CallStack is empty, it returns an empty frame.
func (CallStack) SetErrorField ¶
func (cs CallStack) SetErrorField(setter FieldSetter)
SetErrorField implements the Field interface.
type Code ¶
type Code comparable
Code represents an error code. Any comparable type can be used as an error code.
type Context ¶
Context represents additional contextual information associated with an error. It implements the Field interface.
func (Context) FormatError ¶
func (c Context) FormatError(w ErrorWriter)
FormatError implements the ErrorFormatter interface.
func (Context) SetErrorField ¶
func (c Context) SetErrorField(setter FieldSetter)
SetErrorField implements the Field interface.
type ErrorFormatter ¶
type ErrorFormatter interface {
FormatError(ErrorWriter)
}
ErrorFormatter is an interface for formatting errors. Implement this interface to format errors in custom ways.
type ErrorWriter ¶
ErrorWriter is used by ErrorFormatter to write errors with custom formats. It may have additional fields to specify output format in the future.
type Failure ¶
type Failure interface {
error
Value(key any) any
fmt.Formatter
Unwrap() error
As(target any) bool
// contains filtered or unexported methods
}
Failure represents a error with additional information. It cannot be implemented by external types, but can be embedded within custom structs to implement custom methods.
func ForceUnwrapFailure ¶
ForceUnwrapFailure force unwraps the error, returning the first Failure found in the error chain and the remaining tail of the error. Unlike UnwrapFailure, it pops Failure even if the error is opaqued.
func NewFailure ¶
NewFailure creates a new Failure from an underlying error and optional fields. It panics if both the underlying error and fields are empty.
func UnwrapFailure ¶
UnwrapFailure unwraps the error, returning the first Failure found in the error chain and the remaining tail of the error.
type Field ¶
type Field interface {
SetErrorField(FieldSetter)
}
Field represents an error field. Implement this interface to define your own error fields and attach them to your errors.
type FieldSetter ¶
type FieldSetter interface {
Set(key, value any)
}
FieldSetter is used by Field to set key-value pairs to an error.
type Frame ¶
type Frame struct {
// contains filtered or unexported fields
}
Frame represents a single frame in a CallStack.
type Message ¶
type Message string
Message represents a human-readable error message. It implements the Field interface.
func (Message) SetErrorField ¶
func (m Message) SetErrorField(setter FieldSetter)
SetErrorField implements the Field interface.