errors

package module
v0.10.0 Latest Latest
Warning

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

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

Documentation

Overview

Package errors extends the standard library errors package with additional APIs to: - wrap errors with additional tags - read attached tags from errors

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	New    = errors.New
	Is     = errors.Is
	As     = errors.As
	Unwrap = errors.Unwrap
	Join   = errors.Join
)

Aliases to the standard error types

Functions

func Wrap

func Wrap(err error, attrs ...string) error

Wrap attaches additional tags to an error.

Example
package main

import (
	"fmt"

	"github.com/gojekfarm/xtools/errors"
)

func main() {
	// Create a generic error
	err := errors.New("record not found")

	// Wrap the error with key-value pairs
	wrapped := errors.Wrap(
		err,
		"table", "users",
		"id", "123",
	)

	// Add more tags as the error propagates
	wrapped = errors.Wrap(
		wrapped,
		"experiment_id", "456",
	)

	// errors.Is will check for not found error
	fmt.Println(errors.Is(wrapped, err))

	// Use errors.As to read attached tags.
	var errTags *errors.ErrorTags

	errors.As(wrapped, &errTags)

	// Use the tags to construct detailed error messages,
	// log additional context, or return structured errors.
	fmt.Println(errTags.All())
}

Types

type ErrorTags

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

ErrorTags is an error that has key-value tags attached to it.

func (*ErrorTags) All

func (e *ErrorTags) All() map[string]string

All returns all the tags attached to the error.

func (*ErrorTags) Error

func (e *ErrorTags) Error() string

Error returns the error message with the tags attached.

func (*ErrorTags) Is

func (e *ErrorTags) Is(target error) bool

Is returns true if the error is the same as the target error.

func (*ErrorTags) Unwrap

func (e *ErrorTags) Unwrap() error

Unwrap returns the underlying error.

Jump to

Keyboard shortcuts

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