errors

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: GPL-2.0 Imports: 3 Imported by: 0

README

github.com/Nigel2392/errors

A comprehensive utility package for error handling in Go. It provides a rich, structured error type that supports error codes, multiple related errors, and underlying causes, while seamlessly integrating with and extending the capabilities of both standard errors and github.com/pkg/errors.

Features

  • Structured Errors: The Error struct allows you to define errors with specific GoCode constants, clear messages, and underlying reasons.
  • Error Codes: Easily categorize and identify errors by their code rather than relying strictly on string matching.
  • Cause Tracking: Chain errors and preserve the original cause using WithCause or Wrap.
  • Related Errors: Store multiple related errors alongside the primary error.
  • Drop-in Utility: Provides top-level wrapper functions for common operations like Is, As, Wrap, Unwrap, Join, and Cause by leveraging github.com/pkg/errors and standard library errors.

Installation

go get [github.com/Nigel2392/errors](https://github.com/Nigel2392/errors)

Usage

Creating Structured Errors

Create an error with a specific code and message:

package main

import (
    "fmt"
    "os"
    myerrors "github.com/Nigel2392/errors"
)

const ErrNotFound myerrors.GoCode = "NotFound"

func main() {
    err := myerrors.New(ErrNotFound, "user could not be located in the database")
    fmt.Println(err.Error()) 
    err2 = err.Wrap("wrapped")
    fmt.Println(err.Error()) 
    fmt.Println(err2.Error()) 

    f, err := os.Open(...)
    if err != nil && errors.Is(err, os.ErrNotExist) {
        err = ErrNotFound.WithCause(err)
    }
}
Adding Context and Causes

Attach underlying causes to your structured errors without losing the initial code context:

package main

import (
    "database/sql"
    myerrors "github.com/Nigel2392/errors"
)

func getUser() error {
    err := sql.ErrNoRows
  
    // Create a base error, then attach the sql error as the reason
    baseErr := myerrors.New("DB_ERROR", "failed to fetch user")
    return baseErr.WithCause(err)
}
Using Wrapper Utilities

You can use the package as a drop-in replacement for standard errors and github.com/pkg/errors:

package main

import (
    "fmt"
    myerrors "github.com/Nigel2392/errors"
)

func main() {
    err1 := myerrors.New("ERR_1", "first error")
    err2 := myerrors.New("ERR_2", "second error")
  
    // Join multiple errors
    joined := myerrors.Join(err1, err2)
  
    // Wrap an error with a stack trace
    wrapped := myerrors.Wrap(joined, "additional context")
  
    fmt.Println(wrapped)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target interface{}) bool

As finds the first error in err's chain that matches target, and if so, sets target to that error value and returns true.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error matches target if the error's concrete value is assignable to the value pointed to by target, or if the error has a method As(interface{}) bool such that As(target) returns true. In the latter case, the As method is responsible for setting target.

As will panic if target is not a non-nil pointer to either a type that implements error, or to any interface type. As returns false if err is nil.

func Cause

func Cause(err error) error

Cause returns the underlying cause of the error, if possible. An error value has a cause if it implements the following interface:

type causer interface {
       Cause() error
}

If the error does not implement Cause, the original error will be returned. If the error is nil, nil will be returned without further investigation.

func Errorf

func Errorf(code GoCode, format string, args ...interface{}) error

Errorf formats according to a format specifier and returns the string as a value that satisfies error.

func Is

func Is(err, target error) bool

Is reports whether any error in err's chain matches target.

The chain consists of err itself followed by the sequence of errors obtained by repeatedly calling Unwrap.

An error is considered to match a target if it is equal to that target or if it implements a method Is(error) bool such that Is(target) returns true.

func Join

func Join(errs ...error) error

Join returns an error that wraps all the provided errors.

func Unwrap

func Unwrap(err error) error

Unwrap returns the result of calling the Unwrap method on err, if err's type contains an Unwrap method returning error. Otherwise, Unwrap returns nil.

func WithMessage

func WithMessage(err error, message string) error

WithMessage annotates err with a new message. If err is nil, WithMessage returns nil.

func WithMessagef

func WithMessagef(err error, format string, args ...interface{}) error

WithMessagef annotates err with the format specifier. If err is nil, WithMessagef returns nil.

func Wrap

func Wrap(err error, message string) error

Wrap returns an error annotating err with a stack trace at the point Wrap is called, and the supplied message. If err is nil, Wrap returns nil.

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf returns an error annotating err with a stack trace at the point Wrapf is called, and the format specifier. If err is nil, Wrapf returns nil.

Types

type Error

type Error struct {
	Code    GoCode
	Reason  error
	Message string
	Related []error
}

func New

func New(code GoCode, message string, related ...error) Error

func (Error) Cause

func (e Error) Cause() error

func (Error) Error

func (e Error) Error() string

func (Error) Is

func (e Error) Is(chk error) bool

func (Error) Unwrap

func (e Error) Unwrap() error

func (Error) WithCause

func (e Error) WithCause(reason error) Error

func (Error) Wrap

func (e Error) Wrap(message string) Error

func (Error) Wrapf

func (e Error) Wrapf(format string, args ...any) Error

type GoCode

type GoCode string
const (
	CodeUnknown GoCode = "Unknown"
)

Jump to

Keyboard shortcuts

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