Help us shape the future of Go! Take the 2026 Go Developer Survey

errctx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2025 License: MIT Imports: 3 Imported by: 4

README

errctx

The errctx package allows for setting and retrieving contextual information using error objects.

Usage

err := errors.New("something bad happened")
// set the userID that caused this error
return errctx.Set(err, "userID", userID)

Later on you can get the userID back:

userID, ok := errctx.Get(err, "userID").(int64)

If you want to check the wrapped error object:

if errors.Is(err, underlyingError) {

}

Additionally, if you want to store the source of the original error you can use Mark and Line:

err := errors.New("something bad happened")
// store the filename and line number where Mark was called
return errctx.Mark(err)

Later you can use Line to get the filename:line where Mark was first called:

fileLine, ok := errctx.Line(err)

Documentation

Overview

Package errctx allows for setting and retrieving contextual information on error objects

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Base

func Base(err error) error

Base returns the underlying error object that was prevoiusly wrapped in a call to Set. If the error did not come from Set it is returned as-is.

func Get

func Get(err error, k interface{}) interface{}

Get retrieves the value associated with the key by a previous call to Set, which this error should have been returned from. Returns nil if the key isn't set, or if the error wasn't previously wrapped by Set at all.

This will traverse the error chain until it finds an error returned by Set that contains the key.

func Line

func Line(err error) (string, bool)

Line returns the file and line number where Mark was first called on the error and a boolean indicating if any line was found.

func Mark

func Mark(err error) error

Mark records the filename and line number that called Mark and sets it on the error. Future calls to Mark will NOT overwrite the previous line.

func MarkSkip

func MarkSkip(err error, skip int) error

MarkSkip is like Mark but allows you to skip an arbitrary amount of functions from the stack. Sending skip of 0 means to Mark the caller of this function.

Returns nil if the sent error is nil.

func Set

func Set(err error, kvs ...interface{}) error

Set takes in an error and one or more key/value pairs. It returns an error instance which can have Get called on it with one of those passed in keys to retrieve the associated value later.

Errors returned from Set are immutable. For example:

err := errors.New("ERR")
fmt.Println(errctx.Get(err, "foo")) // ""

err2 := errctx.Set(err, "foo", "a")
fmt.Println(errctx.Get(err2, "foo")) // "a"

err3 := errctx.Set(err2, "foo", "b")
fmt.Println(errctx.Get(err2, "foo")) // "a"
fmt.Println(errctx.Get(err3, "foo")) // "b"

Types

This section is empty.

Jump to

Keyboard shortcuts

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