Skip to content

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

License

Notifications You must be signed in to change notification settings

agilira/go-errors

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
}

JSON Output

Errors automatically serialize to structured JSON:

err := errors.New("VALIDATION_ERROR", "Email format invalid").
    WithUserMessage("Please enter a valid email address").
    WithContext("field", "email").
    WithSeverity("warning")

jsonData, _ := json.Marshal(err)
// Output: {"code":"VALIDATION_ERROR","message":"Email format invalid","user_msg":"Please enter a valid email address","context":{"field":"email"},"severity":"warning","timestamp":"2025-01-27T10:30:00Z",...}

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

About

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

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages