errors

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2025 License: MPL-2.0 Imports: 7 Imported by: 14

README

go-errors: Structured, contextual error handling for Go

an AGILira library

go-errors is a fast, structured, and context-aware error handling library for Go. Built for Styx, it includes error codes, stack traces, user messages, and JSON support — with zero overhead.

CI Security Go Report Card Coverage

Features

  • Structured error type: code, message, context, cause, severity
  • Stacktrace support (optional, lightweight)
  • User and technical messages
  • Custom error codes (user-defined)
  • JSON serialization for API/microservices
  • Retryable and interface-based error handling
  • Helpers for wrapping, root cause, code search
  • 100% Go standard library, no external dependencies
  • Modular, fully tested, high coverage

Installation

go get github.com/agilira/go-errors

Quick Example

import "github.com/agilira/go-errors"

const ErrCodeValidation = "VALIDATION_ERROR"

func validateUser(username string) error {
    if username == "" {
        return errors.New(ErrCodeValidation, "Username is required").WithUserMessage("Please enter a username.")
    }
    return nil
}

Testing & Coverage

Run all tests:

go test -v ./...

Check coverage:

go test -cover ./...
  • Write tests for all custom error codes and logic in your application.
  • Use table-driven tests for error scenarios.
  • Aim for high coverage to ensure reliability.

Documentation

Comprehensive documentation is available in the docs folder:


go-errors • an AGILira library

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasCode

func HasCode(err error, code ErrorCode) bool

HasCode checks if any error in the chain has the given code.

func RootCause

func RootCause(err error) error

RootCause returns the original error in the chain.

Types

type Error

type Error struct {
	Code      ErrorCode              `json:"code"`
	Message   string                 `json:"message"`
	Field     string                 `json:"field,omitempty"`
	Value     string                 `json:"value,omitempty"`
	Context   map[string]interface{} `json:"context,omitempty"`
	Timestamp time.Time              `json:"timestamp"`
	Cause     error                  `json:"cause,omitempty"`
	Severity  string                 `json:"severity"`
	Stack     *Stacktrace            `json:"stack,omitempty"`
	UserMsg   string                 `json:"user_msg,omitempty"`
	Retryable bool                   `json:"retryable,omitempty"`
}

Error represents a structured error with context and cause

func New

func New(code ErrorCode, message string) *Error

New creates a new error with code and message

func NewWithContext

func NewWithContext(code ErrorCode, message string, context map[string]interface{}) *Error

NewWithContext creates a new error with additional context

func NewWithField

func NewWithField(code ErrorCode, message, field, value string) *Error

NewWithField creates a new error with field and value

func Wrap

func Wrap(err error, code ErrorCode, message string) *Error

Wrap wraps an existing error with code and message, capturing stacktrace.

func (*Error) As

func (e *Error) As(target interface{}) bool

As implements errors.As compatibility. It delegates the check to the underlying Cause. Note: It will not match the *Error instance itself, only errors in its cause chain.

func (*Error) AsRetryable added in v1.0.2

func (e *Error) AsRetryable() *Error

AsRetryable marks the error as retryable.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface for *Error.

func (*Error) ErrorCode

func (e *Error) ErrorCode() ErrorCode

ErrorCode returns the error code.

func (*Error) Is

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

Is implements errors.Is compatibility.

func (*Error) IsRetryable

func (e *Error) IsRetryable() bool

IsRetryable returns whether the error is retryable.

func (*Error) MarshalJSON

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

MarshalJSON implements custom JSON marshaling for Error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause.

func (*Error) UserMessage

func (e *Error) UserMessage() string

UserMessage returns the user-friendly message if set, otherwise the technical message.

func (*Error) WithContext added in v1.0.2

func (e *Error) WithContext(key string, value interface{}) *Error

WithContext adds or updates context information on the error.

func (*Error) WithSeverity added in v1.0.2

func (e *Error) WithSeverity(severity string) *Error

WithSeverity sets the severity level of the error.

func (*Error) WithUserMessage

func (e *Error) WithUserMessage(msg string) *Error

WithUserMessage sets a user-friendly message on the error.

type ErrorCode

type ErrorCode string

ErrorCode represents a custom error code (user-defined)

type ErrorCoder

type ErrorCoder interface {
	ErrorCode() ErrorCode
}

ErrorCoder allows extracting a code from an error.

type Retryable

type Retryable interface {
	IsRetryable() bool
}

Retryable marks an error as retryable.

type Stacktrace

type Stacktrace struct {
	Frames []uintptr
}

Stacktrace holds a slice of program counters for error tracing.

func CaptureStacktrace

func CaptureStacktrace(skip int) *Stacktrace

CaptureStacktrace returns a new Stacktrace from the current call stack.

func (*Stacktrace) String

func (s *Stacktrace) String() string

String returns a human-readable stacktrace.

type UserMessager

type UserMessager interface {
	UserMessage() string
}

UserMessager allows extracting a user-friendly message from an error.

Jump to

Keyboard shortcuts

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