errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 8 Imported by: 2

README

errors — error wrapping with codes and stack traces

import "github.com/downsized-devs/sdk-go/errors"

Stability: Stable — see STABILITY.md

Drop-in replacement for the stdlib errors package. Attaches numeric codes, localised messages from language, and caller information so log lines and HTTP responses can be rendered uniformly.

Features

  • NewWithCode / WrapWithCode — build a coded error or wrap an existing one
  • GetCode(err) codes.Code — extract the code from anywhere in the chain
  • GetCaller(err) — file:line of the original wrap site
  • Compile(err, language string) — render a DisplayMessage for HTTP response bodies
  • Is / As — fully compatible with errors.Is / errors.As semantics
  • Stack frame capture (see errors_stacktrace.go)

Installation

go get github.com/downsized-devs/sdk-go/errors

Quick Start

package main

import (
    "fmt"

    "github.com/downsized-devs/sdk-go/codes"
    "github.com/downsized-devs/sdk-go/errors"
)

func loadUser(id string) error {
    if id == "" {
        return errors.NewWithCode(codes.CodeBadRequest, "user id is empty")
    }
    return nil
}

func main() {
    if err := loadUser(""); err != nil {
        fmt.Println("code:", errors.GetCode(err))
        fmt.Println("caller:", errors.GetCaller(err))
    }
}

API Reference

Symbol Purpose
NewWithCode(code, format, args...) Construct a new coded error.
WrapWithCode(err, code, format, args...) Wrap a lower-level error with a code.
GetCode(err) codes.Code First code found walking the chain.
GetCaller(err) string File:line where the error was first wrapped.
Compile(err, lang) codes.DisplayMessage Build a HTTP-ready message in the chosen language.
Is(err, target) / As(err, target) Standard chain inspection.
App Concrete error type carrying code + message + caller + cause.

Examples

Wrap a SQL error and dispatch by code
row, err := db.Leader(ctx).Get(ctx, &u, "SELECT ... WHERE id = ?", id)
if err != nil {
    return errors.WrapWithCode(err, codes.CodeSQLRecordDoesNotExist, "load user %s", id)
}

// Later, at the HTTP layer:
switch errors.GetCode(err) {
case codes.CodeSQLRecordDoesNotExist:
    http.Error(w, "not found", http.StatusNotFound)
default:
    http.Error(w, "internal error", http.StatusInternalServerError)
}
Render a localised response body
msg := errors.Compile(err, "en")
// msg.StatusCode, msg.Title, msg.Body — ready for JSON encoding

Error Handling

The package produces errors; it does not return any of its own from public functions. GetCode returns codes.NoCode if the chain has none.

Dependencies

Testing

go test ./errors/...

Two test files cover the App type, chain walking, and stack-trace capture.

Contributing

See CONTRIBUTING.md. This package is critical-path — 14 sibling packages depend on it.

  • codes — the code registry.
  • language — locale lookup used by Compile.
  • logger — auto-extracts code + caller when logging an App error.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

Implement golang errors.As, finds the first error in err's chain that matches target, and if one is found, sets target to that error value and returns true

func GetCaller

func GetCaller(err error) (string, int, string, error)

func GetCode

func GetCode(err error) codes.Code

func Is

func Is(err error, target error) bool

Implement golang errors.Is, reports whether any error in err's chain matches target.

func NewWithCode

func NewWithCode(code codes.Code, msg string, val ...interface{}) error

func WrapWithCode added in v0.1.0

func WrapWithCode(err error, code codes.Code, msg string, val ...interface{}) error

WrapWithCode wraps an existing error with a new message and error code, preserving the original error as the cause so that errors.Is / errors.As continue to work across the chain.

Types

type App

type App struct {
	Code  codes.Code `json:"code"`
	Title string     `json:"title"`
	Body  string     `json:"body"`
	// contains filtered or unexported fields
}

func Compile

func Compile(err error, lang string) (int, App)

Compile returns an error and creates new App errors

func (*App) Error

func (e *App) Error() string

func (*App) Unwrap added in v0.1.0

func (e *App) Unwrap() error

Unwrap returns the underlying error so that errors.Is and errors.As can traverse the chain through a *App value.

Jump to

Keyboard shortcuts

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