errs

package
v1.114.1 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MPL-2.0 Imports: 10 Imported by: 68

Documentation

Overview

Package errs implements a detailed error object that provides stack traces with source locations, along with nested causes, if any.

Index

Examples

Constants

View Source
const StackTraceKey = "stack_trace"

StackTraceKey is the key used for logging the stack trace.

Variables

View Source
var (

	// RuntimePrefixesToFilter is a list of prefixes to filter out of the stack trace.
	//
	// This variable is not used in a thread-safe manner, so any alterations should be done before any goroutines are
	// started.
	RuntimePrefixesToFilter = []string{
		"runtime.",
		"testing.",
		"github.com/richardwilkes/toolbox/errs.",
	}
)

Functions

func Log added in v1.95.0

func Log(err error, args ...any)

Log an error with a stack trace.

func LogAttrs added in v1.95.0

func LogAttrs(err error, attrs ...slog.Attr)

LogAttrs logs an error with a stack trace.

func LogAttrsContext added in v1.95.0

func LogAttrsContext(ctx context.Context, err error, attrs ...slog.Attr)

LogAttrsContext logs an error with a stack trace.

func LogAttrsContextTo added in v1.95.0

func LogAttrsContextTo(ctx context.Context, logger *slog.Logger, err error, attrs ...slog.Attr)

LogAttrsContextTo logs an error with a stack trace.

func LogAttrsTo added in v1.95.0

func LogAttrsTo(logger *slog.Logger, err error, attrs ...slog.Attr)

LogAttrsTo logs an error with a stack trace.

func LogAttrsWithLevel added in v1.95.0

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 added in v1.95.0

func LogContext(ctx context.Context, err error, args ...any)

LogContext logs an error with a stack trace.

func LogContextTo added in v1.95.0

func LogContextTo(ctx context.Context, logger *slog.Logger, err error, args ...any)

LogContextTo logs an error with a stack trace.

func LogTo added in v1.95.0

func LogTo(logger *slog.Logger, err error, args ...any)

LogTo logs an error with a stack trace.

func LogWithLevel added in v1.95.0

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 added in v1.14.0

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 ...
}

func Wrap

func Wrap(cause error) error

Wrap an error and turn it into a detailed error. If error is already a detailed error or nil, it will be returned as-is.

Types

type Error

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

Error holds the detailed error message.

Example
package main

import (
	"fmt"

	"github.com/richardwilkes/toolbox/errs"
)

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:

recovered from panic
    [github.com/richardwilkes/toolbox/errs_test.ExampleError.func1] errors_test.go:27
    [github.com/richardwilkes/toolbox/errs_test.ExampleError] errors_test.go:28
  Caused by: runtime error: invalid memory address or nil pointer dereference

func Append

func Append(err error, errs ...error) *Error

Append one or more errors to an existing error. err may be nil.

func New

func New(message string) *Error

New creates a new detailed error with the 'message'.

func NewWithCause

func NewWithCause(message string, cause error) *Error

NewWithCause creates a new detailed error with the 'message' and underlying 'cause'.

func NewWithCausef added in v1.21.1

func NewWithCausef(cause error, format string, v ...any) *Error

NewWithCausef creates a new detailed error with an underlying 'cause' and using fmt.Sprintf() to format the message.

func Newf

func Newf(format string, v ...any) *Error

Newf creates a new detailed error using fmt.Sprintf() to format the message.

func WrapTyped added in v1.1.4

func WrapTyped(cause error) *Error

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) CloneWithPrefixMessage added in v1.106.0

func (e *Error) CloneWithPrefixMessage(prefix string) error

CloneWithPrefixMessage clones this error and adds a prefix to its message.

func (*Error) Count

func (e *Error) Count() int

Count returns the number of contained errors, not including causes.

func (*Error) Detail

func (e *Error) Detail(trimRuntime bool) string

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) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) ErrorOrNil

func (e *Error) ErrorOrNil() error

ErrorOrNil returns an error interface if this Error represents one or more errors, or nil if it is empty.

func (*Error) Format

func (e *Error) Format(state fmt.State, verb rune)

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) LogValue added in v1.95.0

func (e *Error) LogValue() slog.Value

LogValue implements the slog.LogValuer interface.

func (*Error) Message

func (e *Error) Message() string

Message returns the message attached to this error.

func (*Error) RawStackTrace added in v1.21.0

func (e *Error) RawStackTrace() []uintptr

RawStackTrace returns the raw call stack pointers for the first error within this error.

func (*Error) StackTrace

func (e *Error) StackTrace(trimRuntime bool) string

StackTrace returns just the stack trace portion of the message.

func (*Error) Unwrap added in v1.21.0

func (e *Error) Unwrap() error

Unwrap implements errors.Unwrap and returns the underlying cause, if any.

func (*Error) WrappedErrors

func (e *Error) WrappedErrors() []error

WrappedErrors returns the contained errors.

type ErrorWrapper

type ErrorWrapper interface {
	error
	Count() int
	WrappedErrors() []error
}

ErrorWrapper contains methods for interacting with the wrapped errors.

type RecoveryHandler added in v1.14.0

type RecoveryHandler func(error)

RecoveryHandler defines the callback used when panics occur with Recovery.

type StackError

type StackError interface {
	error
	Message() string
	Detail(trimRuntime bool) string
	StackTrace(trimRuntime bool) string
}

StackError contains methods with the stack trace and message.

Jump to

Keyboard shortcuts

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