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 ¶
var ( ErrNotFound = New(http.StatusNotFound, "not_found", "resource not found") ErrBadRequest = New(http.StatusBadRequest, "bad_request", "invalid request") ErrForbidden = New(http.StatusForbidden, "auth.forbidden", "access denied") ErrConflict = New(http.StatusConflict, "conflict", "resource already exists") ErrInternal = New(http.StatusInternalServerError, "internal", "internal server error") ErrTimeout = New(http.StatusGatewayTimeout, "timeout", "upstream request timed out") )
Functions ¶
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 ¶
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 ¶
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 Wrap ¶
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 (*Error) Error ¶
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 ¶
HTTPStatus returns e.Status, or 500 if it was not set.