errors

package module
v0.0.0-...-b54e4f3 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2025 License: Apache-2.0 Imports: 3 Imported by: 0

README

go.errors

A lightweight Go error handling library that enhances standard errors with contextual key-value pairs and visual error chain tracking.

Features

  • Contextual Errors: Attach key-value pairs to errors for better debugging
  • Error Wrapping: Build error chains with clear visualization using |> separator
  • Clean API: Fluent interface for easy error construction
  • Standard Compatible: Implements the standard error interface

Installation

go get github.com/nxtcoder17/go.errors

Usage

Basic Error Creation
import "github.com/nxtcoder17/go.errors"

func processUser() error {
    return errors.New("failed to process user").KV("userID", 123)
}
// Output: failed to process user <userID=123>
Error Wrapping

Build error chains to track the flow through your application:

func A() error {
    return errors.New("failed to process A").KV("func", "A")
}

func B() error {
    if err := A(); err != nil {
        return errors.New("failed to process B").Wrap(err).KV("func", "B")
    }
    return nil
}

// Output: failed to process B |> failed to process A <func=B, func=A>

The |> separator visually shows the error propagation path, making it easy to trace errors through your call stack.

Multiple Key-Value Pairs

Add multiple contextual fields to your errors:

err := errors.New("database query failed").
    KV("table", "users", "operation", "SELECT", "timeout", "5s")
// Output: database query failed <table=users, operation=SELECT, timeout=5s>

API Reference

New(msg string) *Error

Creates a new error with the given message.

KV(kvPairs ...any) *Error

Adds key-value pairs to the error for context. Keys should be strings, followed by their values.

Wrap(err error) *Error

Wraps another error, creating an error chain. The wrapped error is appended with the |> separator.

Error() string

Returns the formatted error message with all context and wrapped errors.

License

See LICENSE file for details.

Author

nxtcoder17

Documentation

Index

Constants

This section is empty.

Variables

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

Below functions provide compatibility with std go errors

Functions

This section is empty.

Types

type Error

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

func New

func New(msg string) *Error

New creates a new error with the given message

func (*Error) AsKeyValues

func (e *Error) AsKeyValues() []any

func (*Error) Error

func (e *Error) Error() string

Error implements error.

func (*Error) GetErrMessage

func (e *Error) GetErrMessage() string

func (*Error) GetKV

func (e *Error) GetKV() []any

func (*Error) KV

func (e *Error) KV(kvPairs ...any) *Error

func (*Error) Wrap

func (e *Error) Wrap(err error) *Error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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