xerrors

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 7 Imported by: 0

README

Error logo

Go xerrors Package

An extended Golang error library.

VSCode Go Linux MacOS Windows
alpha Stability MIT License Community Supported
Go Reference Go Report Card


Table of Contents

👁️ Overview

go.innotegrity.dev/mod/xerrors is an extended version of the built-in Golang error interface and includes an implementation of the interface. This extended version includes an integer code attached to the error as well as any arbitrary attributes you wish to attach to the error along with the ability to capture caller information when the error is created.

Please review the module documentation for details on how to properly use the functions and types contained in this module.

✅ Requirements

This module is supported for Go v1.23.1 and later.

📃 License

This module is distributed under the MIT License.

❓ Questions, Issues and Feature Requests

If you have questions about this project, find a bug or wish to submit a feature request, please submit an issue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureCallerInfo

func CaptureCallerInfo(enable bool)

CaptureCallerInfo controls whether the caller info should be captured when a new error is generated.

This function enables or disables the capture of the caller information globally for this package. This call is thread-safe.

func StripCallerFilePrefixes

func StripCallerFilePrefixes(prefixes ...string)

StripCallerFilePrefixes allows you to specify a list of file prefixes that should be stripped from the file path when capturing the caller information.

Only the first matching prefix will be stripped from the file path.

This function affects all CallerInfo objects generated globally by this package. This call is thread-safe.

Types

type CallerInfo

type CallerInfo struct {
	// File is the name of the file in which the error occurred, relative to the package root.
	File string `json:"file"`

	// Line is the line number at which the error occurred.
	Line int `json:"line"`

	// Func is the name of the function in which the error occurred.
	Func string `json:"func"`
}

CallerInfo holds information about the location from which the error was generated.

func DefaultCallerInfo

func DefaultCallerInfo() *CallerInfo

DefaultCallerInfo returns a default CallerInfo that indicates that no caller information was captured.

func GetCallerInfo

func GetCallerInfo(skip int) *CallerInfo

GetCallerInfo retrieves the file path, line number, and function name of the caller, formatting the file path to be relative to the package directory.

If the caller information is not available, a default CallerInfo is returned.

The 'skip' parameter indicates how many stack frames to ascend with 0 being the immediate caller of this function.

This function does not have to be called directly if you are using the New, Newf, Wrap or Wrapf functions to generate errors and you have enabled caller capture using CaptureCallerInfo.

type Error

type Error interface {
	error
	json.Marshaler

	// Attrs should return a map of attributes associated with the error.
	Attrs() map[string]any

	// Caller should return the information on where the error was generated.
	Caller() CallerInfo

	// Code should return the error code.
	Code() int

	// Is should return true if the wrapped error inside the object matches the given error, false otherwise.
	Is(error) bool

	// String should return a string representation of the error.
	//
	// Unlike the Error() method, this function may include additional information such as the caller details or
	// attributes in any format (eg: plaintext or JSON).
	String() string

	// WithAttr should add an attribute to the error and return itself.
	WithAttr(key string, value any) Error

	// WithAttrs should add attributes to the error and return itself.
	WithAttrs(attrs map[string]any) Error
}

Error is the interface implemented by extended errors.

func New

func New(code int, message string) Error

New creates a new Error with the given code and message.

func Newf

func Newf(code int, format string, args ...any) Error

Newf creates a new Error with the given code and formatted message.

func Wrap

func Wrap(code int, err error, message string) Error

Wrap wraps the given error in a new Error with the given code and message.

func Wrapf

func Wrapf(code int, err error, format string, args ...any) Error

Wrapf wraps the given error in a new Error with the given code and formatted message.

Jump to

Keyboard shortcuts

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