errors

package
v0.116.5 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 3 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package errors defines a typed Error value that carries an HTTP status code, a stable machine-readable error code, a human message, and an optional cause. It integrates with response.Envelope so handlers return a consistent JSON shape without a per-handler switch.

Design goals:

  • errors.Is / errors.As compatible (Unwrap chain preserved).
  • Machine code is a dotted string, e.g. "auth.scope_mismatch". Clients should switch on Code, not on Status or Msg.
  • HTTP status lives on the error so response helpers can map it automatically.

Usage:

return errors.New(http.StatusForbidden, "auth.scope_mismatch",
    "token scope does not cover this resource")

// wrap an existing error:
return errors.Wrap(err, http.StatusBadGateway, "upstream.timeout",
    "upstream service did not respond in time")

// check in a handler:
var apiErr *errors.Error
if errors.As(err, &apiErr) {
    http.Error(w, apiErr.Msg, apiErr.Status)
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound     = New(http.StatusNotFound, "not_found", "resource not found")
	ErrBadRequest   = New(http.StatusBadRequest, "bad_request", "invalid request")
	ErrUnauthorized = New(http.StatusUnauthorized, "auth.missing", "authentication required")
	ErrForbidden    = New(http.StatusForbidden, "auth.forbidden", "access denied")
	ErrConflict     = New(http.StatusConflict, "conflict", "resource already exists")
	ErrInternal     = New(http.StatusInternalServerError, "internal", "internal server error")
	ErrUnavailable  = New(http.StatusServiceUnavailable, "unavailable", "service temporarily unavailable")
	ErrTimeout      = New(http.StatusGatewayTimeout, "timeout", "upstream request timed out")
)

Functions

func As

func As(err error, target any) bool

As wraps errors.As.

func Is

func Is(err, target error) bool

Is wraps errors.Is.

func Unwrap

func Unwrap(err error) error

Unwrap wraps errors.Unwrap.

Types

type Error

type Error struct {
	// Code is a stable, dotted, machine-readable identifier.
	// Clients should match on this, not on Msg.
	// Convention: "<domain>.<snake_case>" e.g. "auth.token_missing"
	Code string

	// Status is the HTTP status code this error maps to.
	// Defaults to 500 if not set.
	Status int

	// Msg is a human-readable description, safe to surface in API responses.
	Msg string

	// Cause wraps the underlying error (accessible via errors.Unwrap).
	Cause error
}

Error is a typed fleet error. It satisfies the standard error interface and is compatible with errors.Is / errors.As / errors.Unwrap.

func FromError

func FromError(err error) (*Error, bool)

FromError extracts an *Error from err. Returns the error and true if err (or any cause in its chain) is *Error. Returns a synthetic 500 *Error and false otherwise.

func New

func New(status int, code, msg string) *Error

New creates a new *Error with the given status, code, and message.

Example

A typed error carries the HTTP status and a stable code next to the message. Clients match on the code, never on the message.

package main

import (
	"fmt"
	"net/http"

	fleeterrors "github.com/baditaflorin/go-common/errors"
)

func main() {
	err := fleeterrors.New(http.StatusNotFound, "not_found", "no such item")
	fmt.Println(err)
	fmt.Println(err.HTTPStatus())
}
Output:
not_found: no such item
404

func Newf

func Newf(status int, code, format string, args ...any) *Error

Newf creates a new *Error with a formatted message.

func Wrap

func Wrap(cause error, status int, code, msg string) *Error

Wrap creates a new *Error wrapping cause. If cause is already an *Error its Code is used when code is empty.

Example

Wrap keeps the cause reachable for errors.Is and errors.As.

package main

import (
	stderrors "errors"
	"fmt"
	"net/http"

	fleeterrors "github.com/baditaflorin/go-common/errors"
)

func main() {
	cause := stderrors.New("connection refused")
	err := fleeterrors.Wrap(cause, http.StatusBadGateway, "upstream.unreachable", "fetch failed")
	fmt.Println(err)
	fmt.Println(stderrors.Is(err, cause))
}
Output:
upstream.unreachable: fetch failed: connection refused
true

func Wrapf

func Wrapf(cause error, status int, code, format string, args ...any) *Error

Wrapf creates a new *Error wrapping cause with a formatted message.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface. The string includes the code and message; the HTTP status is omitted to avoid leaking transport concerns into logs.

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus returns e.Status, or 500 if it was not set.

func (*Error) Is

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

Is reports whether target is an *Error with the same Code. This lets callers use errors.Is(err, ErrAuthMissing) when ErrAuthMissing is a sentinel *Error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the wrapped cause for errors.Is / errors.As chains.

Jump to

Keyboard shortcuts

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