errors

package
v2.0.0-alpha.16 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2025 License: MIT Imports: 16 Imported by: 1

README

Errors Module

The errors module provides enhanced error handling capabilities for Go applications, offering rich context, stack traces, and metadata while maintaining compatibility with the standard error interface.

Features

  • Rich Error Context: Attach metadata and tags to errors
  • Stack Traces: Automatic stack trace capture for debugging
  • Error Wrapping: Preserve error chains with additional context
  • gRPC Compatibility: Integration with gRPC status codes
  • Panic Recovery: Safe recovery from panics with context preservation

Installation

go get github.com/pubgo/funk/v2/errors

Quick Start

Creating Errors
import "github.com/pubgo/funk/v2/errors"

// Simple error creation
err := errors.New("database connection failed")

// Error with metadata
err := errors.New("database connection failed", errors.Tags{
    "component": "database",
    "host": "localhost:5432",
})
Error Wrapping
// Wrap with additional context
err = errors.Wrap(err, "failed to initialize service")

// Wrap with formatted message
err = errors.Wrapf(err, "failed to initialize service after %d attempts", retryCount)

// Wrap with stack trace
err = errors.WrapStack(err)
Error Inspection
// Standard error checking
if err != nil {
    // Handle error
}

// Type assertion
if dbErr, ok := errors.AsA[*DatabaseError](err); ok {
    // Handle database-specific error
}

// Error chain traversal
for err != nil {
    fmt.Println(err.Error())
    err = errors.Unwrap(err)
}

Core Concepts

Error Types

The module provides two primary error types:

  1. Err: Base error implementation with message, detail, and tags
  2. ErrWrap: Wrapper that adds context and optionally stack traces
Error Creation Functions
  • New(msg string, tags ...Tags): Create a new error with optional metadata
  • Errorf(format string, args ...any): Create a formatted error
  • Wrap(err error, msg string): Wrap an error with additional context
  • Wrapf(err error, format string, args ...any): Wrap with formatted context
  • WrapStack(err error): Wrap with stack trace information
Error Inspection
  • As(err error, target any): Type assertion with error chain traversal
  • Is(err, target error): Check if error chain contains a specific error
  • Unwrap(err error): Retrieve the underlying error

Advanced Usage

Custom Error Types
type DatabaseError struct {
    Op string
    Table string
    Err error
}

func (e *DatabaseError) Error() string {
    return fmt.Sprintf("database error in %s.%s: %v", e.Op, e.Table, e.Err)
}

func (e *DatabaseError) Unwrap() error {
    return e.Err
}
Error Metadata
// Attach metadata to errors
err := errors.New("payment processing failed", errors.Tags{
    "transaction_id": "txn_12345",
    "amount": 99.99,
    "currency": "USD",
})

// Access metadata
if tags := errors.GetTags(err); tags != nil {
    txnID := tags["transaction_id"]
    // Process transaction ID
}
Stack Trace Analysis
// Capture stack trace
err := errors.WrapStack(errors.New("critical failure"))

// Print detailed error information
errors.DebugPrint(err)

// Extract structured error data
data := errors.ErrJsonify(err)

Integration

With gRPC
import "github.com/pubgo/funk/v2/errors/errcode"

// Convert to gRPC status
status := errcode.ConvertErr2Status(err)

// Create error code
code := &errorpb.ErrCode{
    Code: 500,
    StatusCode: errorpb.Code_Internal,
    Name: "INTERNAL_ERROR",
    Message: "Internal server error",
}
With Logging
import "github.com/pubgo/funk/v2/log"

// Log errors with context
log.Error().Err(err).Msg("operation failed")

// Structured error logging
log.Error().RawJSON("error", errors.JsonPrint(err)).Send()

Best Practices

  1. Always Wrap Errors: Provide context when passing errors up the call stack
  2. Use Stack Traces Sparingly: Only in critical paths or for debugging
  3. Attach Relevant Metadata: Include information that aids debugging
  4. Preserve Error Chains: Use Unwrap() to maintain error causality
  5. Handle Errors at Appropriate Levels: Don't catch and rethrow without adding value

API Reference

Error Creation
Function Description
New(msg string, tags ...Tags) Create new error with optional tags
Errorf(format string, args ...any) Create formatted error
Wrap(err error, msg string) Wrap error with context
Wrapf(err error, format string, args ...any) Wrap with formatted context
WrapStack(err error) Wrap with stack trace
WrapTags(err error, tags Tags) Wrap with metadata tags
Error Inspection
Function Description
As(err error, target any) Type assertion with chain traversal
Is(err, target error) Check error chain for specific error
Unwrap(err error) Get underlying error
GetErrorId(err error) Get unique error identifier
Utility Functions
Function Description
DebugPrint(err error) Pretty print error with stack trace
JsonPrint(err error) Serialize error to JSON
ErrJsonify(err error) Convert error to structured data

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

func AsA

func AsA[T any](err error) (*T, bool)

func DebugPrint

func DebugPrint(err error)

func ErrJsonify

func ErrJsonify(err error) map[string]any

func ErrStringify

func ErrStringify(buf *bytes.Buffer, err error)

func Errorf

func Errorf(msg string, args ...any) error

func GetErrorId

func GetErrorId(err error) string

func IfErr

func IfErr(err error, fn func(err error) error) error

func Is

func Is(err, target error) bool

func Join

func Join(errs ...error) error

func JsonPrint

func JsonPrint(err error) []byte

func New

func New(msg string, tags ...Tags) error

func NewErrorId

func NewErrorId() string

func PrintFormat

func PrintFormat(f fmt.State, verb rune, err Error)

func Unwrap

func Unwrap(err error) error

func Wrap

func Wrap(err error, msg string) error

func WrapCaller

func WrapCaller(err error, skip ...int) error

func WrapFn

func WrapFn(err error, fn func() Tags) error

func WrapKV

func WrapKV(err error, key string, value any) error

func WrapStack

func WrapStack(err error) error

func WrapTags

func WrapTags(err error, tags Tags) error

func WrapTagsCaller

func WrapTagsCaller(err error, tags Tags, skip ...int) error

func Wrapf

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

Types

type Err

type Err struct {
	Msg    string `json:"msg,omitempty"`
	Detail string `json:"detail,omitempty"`
	Tags   Tags   `json:"tags,omitempty"`
	// contains filtered or unexported fields
}

func (Err) Error

func (e Err) Error() string

func (Err) Format

func (e Err) Format(f fmt.State, verb rune)

func (Err) ID

func (e Err) ID() string

func (Err) MarshalJSON

func (e Err) MarshalJSON() ([]byte, error)

func (Err) String

func (e Err) String() string

type ErrAs

type ErrAs interface {
	As(any) bool
}

type ErrIs

type ErrIs interface {
	Is(error) bool
}

type ErrUnwrapper

type ErrUnwrapper interface {
	Unwrap() error
}

type ErrWrap

type ErrWrap struct {
	Err    error
	Tags   Tags
	Caller string
	Stacks []string
	// contains filtered or unexported fields
}

func (*ErrWrap) Error

func (e *ErrWrap) Error() string

func (*ErrWrap) Format

func (e *ErrWrap) Format(f fmt.State, verb rune)

func (*ErrWrap) ID

func (e *ErrWrap) ID() string

func (*ErrWrap) MarshalJSON

func (e *ErrWrap) MarshalJSON() ([]byte, error)

func (*ErrWrap) String

func (e *ErrWrap) String() string

func (*ErrWrap) Unwrap

func (e *ErrWrap) Unwrap() error

type Error

type Error interface {
	ErrorID
	String() string
	MarshalJSON() ([]byte, error)
}

type ErrorID

type ErrorID interface {
	error
	ID() string
}

type Tags

type Tags map[string]any

func (Tags) ToMapString

func (t Tags) ToMapString() map[string]string

Directories

Path Synopsis
Package errcode provides error code handling with gRPC compatibility.
Package errcode provides error code handling with gRPC compatibility.

Jump to

Keyboard shortcuts

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