errors

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package errors provides shared sentinel errors and gRPC mapping for paper-board services. All public service errors should wrap one of these sentinels so callers can use errors.Is for routing.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound         = errors.New("not found")
	ErrConflict         = errors.New("conflict")
	ErrUnauthorized     = errors.New("unauthorized")
	ErrPermissionDenied = errors.New("permission denied")
	ErrInvalidInput     = errors.New("invalid input")
	ErrRateLimited      = errors.New("rate limited")
	ErrInternal         = errors.New("internal error")
	ErrUnavailable      = errors.New("service unavailable")
)

Sentinel errors.

Functions

func As

func As(err error, target any) bool

As re-exports errors.As.

func FromGRPCStatus

func FromGRPCStatus(err error) error

FromGRPCStatus converts a gRPC status error back to a wrapped sentinel. Useful in client code to use errors.Is across the wire.

func Is

func Is(err, target error) bool

Is is a re-export so callers don't need to import stdlib errors separately.

func Kind added in v0.2.0

func Kind(err error) string

Kind returns a snake_case slug identifying the sentinel an error wraps. Used by sdk/httpmw to derive HTTP error codes ("<svc>.<kind>") and by sdk/log.ErrorAttrs to populate the "error.kind" attribute.

Returns "internal" for nil-wrapped or unknown errors so callers do not need to pre-classify.

func Message added in v0.2.0

func Message(err error) string

Message returns the canonical user-facing message for the sentinel an error wraps. Mirrors Kind. Use for HTTP error envelopes; the wrapped chain detail goes only to logs (avoid leaking internal context to clients).

func ToGRPCStatus

func ToGRPCStatus(err error) error

ToGRPCStatus maps a wrapped sentinel error to a gRPC status. Used by service handlers to convert internal errors into RPC responses.

if err := repo.Get(ctx, id); err != nil {
    return nil, errors.ToGRPCStatus(err)
}

func ToHTTPStatus added in v0.1.2

func ToHTTPStatus(err error) int

ToHTTPStatus maps a wrapped sentinel error to an HTTP status code. Used by sdk/httpmw.HandleErr as the central HTTP error boundary.

Mapping:

ErrNotFound          → 404
ErrConflict          → 409
ErrUnauthorized      → 401
ErrPermissionDenied  → 403
ErrInvalidInput      → 400
ErrRateLimited       → 429
ErrUnavailable       → 503
ErrInternal          → 500
nil or unknown       → 500

Example:

if err := repo.Get(ctx, id); err != nil {
    http.Error(w, err.Error(), errors.ToHTTPStatus(err))
    return
}

func Wrap

func Wrap(sentinel error, msg string, kvs ...any) error

Wrap annotates a sentinel with context. The sentinel remains accessible via errors.Is.

if u == nil {
    return errors.Wrap(errors.ErrNotFound, "user", "id", id)
}

Output: "user (id=...): not found"

Types

This section is empty.

Jump to

Keyboard shortcuts

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