Documentation ¶
Overview ¶
Package errs implements a detailed error object that provides stack traces with source locations, along with nested causes, if any.
Index ¶
- Constants
- func Log(err error, args ...any)
- func LogAttrs(err error, attrs ...slog.Attr)
- func LogAttrsContext(ctx context.Context, err error, attrs ...slog.Attr)
- func LogAttrsContextTo(ctx context.Context, logger *slog.Logger, err error, attrs ...slog.Attr)
- func LogAttrsTo(logger *slog.Logger, err error, attrs ...slog.Attr)
- func LogAttrsWithLevel(ctx context.Context, level slog.Level, logger *slog.Logger, err error, ...)
- func LogContext(ctx context.Context, err error, args ...any)
- func LogContextTo(ctx context.Context, logger *slog.Logger, err error, args ...any)
- func LogTo(logger *slog.Logger, err error, args ...any)
- func LogWithLevel(ctx context.Context, level slog.Level, logger *slog.Logger, err error, ...)
- func Recovery(handler RecoveryHandler)
- func Wrap(cause error) error
- type Error
- func (e *Error) Count() int
- func (e *Error) Detail(trimRuntime bool) string
- func (e *Error) Error() string
- func (e *Error) ErrorOrNil() error
- func (e *Error) Format(state fmt.State, verb rune)
- func (e *Error) LogValue() slog.Value
- func (e *Error) Message() string
- func (e *Error) RawStackTrace() []uintptr
- func (e *Error) StackTrace(trimRuntime bool) string
- func (e *Error) Unwrap() error
- func (e *Error) WrappedErrors() []error
- type ErrorWrapper
- type RecoveryHandler
- type StackError
Examples ¶
Constants ¶
const StackTraceKey = "stack_trace"
StackTraceKey is the key used for logging the stack trace.
Variables ¶
This section is empty.
Functions ¶
func LogAttrsContext ¶
LogAttrsContext logs an error with a stack trace.
func LogAttrsContextTo ¶
LogAttrsContextTo logs an error with a stack trace.
func LogAttrsTo ¶
LogAttrsTo logs an error with a stack trace.
func LogAttrsWithLevel ¶
func LogAttrsWithLevel(ctx context.Context, level slog.Level, logger *slog.Logger, err error, attrs ...slog.Attr)
LogAttrsWithLevel logs an error with a stack trace.
func LogContext ¶
LogContext logs an error with a stack trace.
func LogContextTo ¶
LogContextTo logs an error with a stack trace.
func LogWithLevel ¶
func LogWithLevel(ctx context.Context, level slog.Level, logger *slog.Logger, err error, args ...any)
LogWithLevel logs an error with a stack trace.
func Recovery ¶
func Recovery(handler RecoveryHandler)
Recovery provides an easy way to run code that may panic. 'handler' will be called with the panic turned into an error. Pass in nil to silently ignore any panic.
Typical usage:
func runSomeCode(handler errs.RecoveryHandler) { defer errs.Recovery(handler) // ... run the code here ... }
Types ¶
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error holds the detailed error message.
Example ¶
package main import () func main() { //var bad *int //func() int { // defer errs.Recovery(func(err error) { fmt.Println(err) }) // return *bad // trigger a panic due to a nil pointer dereference //}()
Output:
func NewWithCause ¶
NewWithCause creates a new detailed error with the 'message' and underlying 'cause'.
func NewWithCausef ¶
NewWithCausef creates a new detailed error with an underlying 'cause' and using fmt.Sprintf() to format the message.
func WrapTyped ¶
WrapTyped wraps an error and turns it into a detailed error. If error is already a detailed error or nil, it will be returned as-is. This method returns the error as an *Error. Use Wrap() to receive a generic error.
func (*Error) Detail ¶
Detail returns the fully detailed error message, which includes the primary message, the call stack, and potentially one or more chained causes. Note that any included stack trace will be only for the first error in the case where multiple errors were accumulated into one via calls to .Append().
func (*Error) ErrorOrNil ¶
ErrorOrNil returns an error interface if this Error represents one or more errors, or nil if it is empty.
func (*Error) Format ¶
Format implements the fmt.Formatter interface.
Supported formats:
- "%s" Just the message
- "%q" Just the message, but quoted
- "%v" The message plus a stack trace, trimmed of golang runtime calls
- "%+v" The message plus a stack trace
func (*Error) RawStackTrace ¶
RawStackTrace returns the raw call stack pointers for the first error within this error.
func (*Error) StackTrace ¶
StackTrace returns just the stack trace portion of the message.
func (*Error) WrappedErrors ¶
WrappedErrors returns the contained errors.
type ErrorWrapper ¶
ErrorWrapper contains methods for interacting with the wrapped errors.
type RecoveryHandler ¶
type RecoveryHandler func(error)
RecoveryHandler defines the callback used when panics occur with Recovery.