ctxerr

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package ctxerr provides functions to extract context information from errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ctx

func Ctx(ctx context.Context, ctxErr error) context.Context

Ctx adds the context error to ctx. Use this to pass the context error to the logger. Logger will automatically extract the error with its context values and log them. Example:

logger.Error().Ctx(ctxerr.Ctx(context.Background(), err)).Msg("an error occurred")

func With

func With(err error, errCtx map[string]any) error

With adds the error context values by wrapping err into ctxerr.Error. Use this to propagate contextual information up the call stack.

Example:

return ctxerr.With(err, map[string]any{"user_id": 1})

Types

type CtxErr

type CtxErr struct {
	// Err is the original wrapped error.
	Err error

	// Ctx holds contextual information associated with Err.
	Ctx map[string]any
}

CtxErr is an error type that contains contextual information fields related to the error. It's used to propagate fields up the call stack.

func From

func From(ctx context.Context) CtxErr

From reads the error from ctx, if any.

func (CtxErr) Error

func (e CtxErr) Error() string

func (CtxErr) Unwrap

func (e CtxErr) Unwrap() error

Jump to

Keyboard shortcuts

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