apierr

package
v0.60.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package apierr is the one shape of a failure filex tells somebody about: a stable CODE a program branches on, the values its sentence takes, and the sentence itself, written by the SERVER in the reader's language.

⚠⚠ Why it exists (0.54 audit, A1/A2). The server already wrote most of its sentences in the reader's language (srvtext), but a refusal carried them in three shapes - an English sentence in `error`, a code in `error` with the sentence in `message`, an English sentence in `error` with a code beside it in `code` - and a failed queue job kept Go's own error text. The explorer threw the server's sentence away and rebuilt one from regular expressions matched against the English ("quota exceeded", "storage is read-only", "something with that name already exists here"): a Go error text that changed broke the translation silently, and the CLI and an MCP agent read "HTTP 403: permission_denied".

The envelope (docs/API-ERRORS.md):

	{"error": "<code>", "message": "<sentence, reader's language>", "params": {…}}

  - `error` is the code: lower-case ASCII, `[a-z0-9_]`, stable across
    releases. A refusal that has always answered a code keeps it; older
    clients that match on it keep working.
  - `message` is `server.error.<code>` from the server catalogue (srvtext),
    filled with `params`, in the reader's language (a language pack's too).
    Every surface prints it as it is: the explorer, the admin panel, the
    desktop app, the CLI, an MCP agent.
  - `params` are the values the sentence was filled with, for a program
    that wants them (a name, a limit). Optional.

A failure that is not a refusal (a queue job that failed) carries the same three things: Error is what the worker stores (Encode) instead of Go's text, and the sentence is made when the row is READ, in the language of the person reading it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Codes

func Codes() []string

Codes is every code the catalogue has a sentence for, sorted - for the documentation test that lists them.

func Encode

func Encode(code string, params Params, detail string) string

Encode is what a row keeps for a failure: the code, its params and the English detail when the failure has a code, else the plain text as before.

func EncodeErr

func EncodeErr(err error) string

EncodeErr is Encode for an error: its code when it carries one (CodeOf), its text as the detail.

func Key

func Key(code string) string

Key is the catalogue key of code's sentence.

func Known

func Known(code string) bool

Known reports whether the catalogue has a sentence for code.

func Map

func Map(lang, code string, params Params, extra map[string]any) map[string]any

Map is Envelope as a map, for a refusal that carries fields of its own beside the three (a lock's `plugin`, a draft's `limit`). The three win over an extra field of the same name.

func Text

func Text(lang, code string, params Params) string

Text is code's sentence in lang, filled with params; "" for a code the catalogue does not know (the caller then says nothing rather than print a key). lang is resolved like every server text (srvtext.Pick): a pack's language, its base language, the instance default, English.

func ValidCode

func ValidCode(code string) bool

ValidCode reports whether code has the shape of an error code.

Types

type Body

type Body struct {
	Error   string `json:"error"`
	Message string `json:"message"`
	Params  Params `json:"params,omitempty"`
}

Body is the envelope of a refusal, ready to marshal.

func Envelope

func Envelope(lang, code string, params Params) Body

Envelope is code's refusal said in lang.

type Error

type Error struct {
	Code   string
	Params Params
	// Err is the underlying failure, if any.
	Err error
}

Error is a failure with a code: what a service returns when its caller (a handler, the queue worker) must be able to say it in the reader's language later. Error() is the English detail, for logs and for an administrator's second line - never the sentence a person reads.

func New

func New(code string, params Params, detail error) *Error

New is a coded failure. detail may be nil.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Params

type Params = srvtext.Vars

Params are the values a code's sentence takes (`{name}`, `{max}`). A "count" that is a whole number picks the sentence's plural form.

func CodeOf

func CodeOf(err error) (string, Params)

CodeOf is the code and params of the first *Error in err's chain, or "".

func Decode

func Decode(s string) (code string, params Params, detail string)

Decode reads back what Encode wrote: the code ("" for a plain text), the params and the detail (the plain text itself when there is no code).

Jump to

Keyboard shortcuts

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