envelope

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package envelope is the one error shape every ovdb interface shares: the local API writes it, the CLI prints it (as JSON with --json, or as the "Couldn't …/Why/What you can do" problem pattern otherwise), and later the TUI and web console render it. Presentations never build one themselves; services and the shared client do.

See spec/features/configuration-parity#REQ:error-envelope and spec/features/first-run-onboarding#REQ:problem-pattern.

Index

Constants

View Source
const Schema = 1

Schema is the version of every local API body and --json document.

Variables

Codes lists every valid code, in the spec's order.

Functions

func Marshal

func Marshal(v any) []byte

Marshal renders any schema-1 document as compact JSON followed by one newline. The local API and the CLI both use it, which is what makes a command's --json output byte-for-byte equal to the API response body (configuration-parity#REQ:json-equals-api).

func MarshalError

func MarshalError(e *Error) []byte

MarshalError renders e as the failure document.

func Write

func Write(w http.ResponseWriter, e *Error)

Write sends e as an HTTP response with the status its code maps to.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

WriteJSON sends a schema-1 success document.

func WriteStatus

func WriteStatus(w http.ResponseWriter, status int, e *Error)

WriteStatus sends e with an explicit status, for the few responses whose status is dictated by a route rather than by the code.

Types

type Code

type Code string

Code is one of the closed set of error codes.

const (
	InvalidArgument       Code = "invalid_argument"
	ConfirmationRequired  Code = "confirmation_required"
	NotFound              Code = "not_found"
	AlreadyExists         Code = "already_exists"
	LocationNotEmpty      Code = "location_not_empty"
	PortInUse             Code = "port_in_use"
	PortUnavailable       Code = "port_unavailable"
	ServerNotRunning      Code = "server_not_running"
	ServerStartFailed     Code = "server_start_failed"
	ServerVersionMismatch Code = "server_version_mismatch"
	ServerConfigMismatch  Code = "server_config_mismatch"
	Unauthorized          Code = "unauthorized"
	Forbidden             Code = "forbidden"
	StorageUnavailable    Code = "storage_unavailable"
	SchemaRequired        Code = "schema_required"
	ValidationFailed      Code = "validation_failed"
	Unsupported           Code = "unsupported"
	DependencyMissing     Code = "dependency_missing"
	Timeout               Code = "timeout"
	Internal              Code = "internal"
)

The closed list of codes. Adding one is a spec change.

func (Code) HTTPStatus

func (c Code) HTTPStatus() int

HTTPStatus is the local API status for code. Client-side-only codes (port_in_use, server_start_failed, …) never cross HTTP; they map to 500 so a misuse is loud rather than silently successful.

type Error

type Error struct {
	Code    Code   `json:"code"`
	Message string `json:"message"`
	Reason  string `json:"reason,omitempty"`
	Next    []Next `json:"next"`
	// Targets is what a command that failed for some of its targets did to
	// the others (skills install: the outcomes of the folders that did
	// change), as the same list its success document carries. It is absent
	// when nothing changed, so a failure that touched nothing is the
	// document it always was.
	Targets json.RawMessage `json:"targets,omitempty"`
}

Error is a failure in envelope form. It is a Go error, so services return it through ordinary error paths and presentations recover it with As.

func As

func As(err error) *Error

As returns the *Error in err's chain, or nil.

func Decode

func Decode(data []byte) *Error

Decode parses a failure document, returning nil when data is not one.

func New

func New(code Code, message string) *Error

New returns an Error with code and message and an empty next list.

func (*Error) Error

func (e *Error) Error() string

func (*Error) WithNext

func (e *Error) WithNext(next ...Next) *Error

WithNext appends next actions and returns e for chaining.

func (*Error) WithReason

func (e *Error) WithReason(reason string) *Error

WithReason sets the "Why" line and returns e for chaining.

func (*Error) WithTargets added in v0.22.0

func (e *Error) WithTargets(v any) *Error

WithTargets sets the outcomes of the targets that did change before the failure; v is marshalled as the success document's list would be.

type Next

type Next struct {
	Label   string `json:"label"`
	Command string `json:"command,omitempty"`
	Action  string `json:"action,omitempty"`
}

Next is one thing the person (or agent) can do about a result or failure. Command is always runnable as written; Action names an in-UI remedy such as "use_port" that a TUI or web console offers as a button.

Jump to

Keyboard shortcuts

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