errors

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: May 2, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package errors is brainjar's single domain-error type. Every app and handler returns either a wrapped *Error or *RefError; HTTP and CLI layers each map the Code to their own surface (status code, exit code, user message).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromHTTP

func FromHTTP(resp *http.Response) error

FromHTTP decodes a brainjar error envelope from a non-2xx HTTP response. Returns *RefError when the envelope carries invalid_refs, otherwise *Error. Unknown envelope codes map to CodeUnknown. Non-JSON or missing envelopes fall back to deriving a code from the HTTP status.

Consumes resp.Body — callers should not read it afterward.

func FromJSON

func FromJSON(raw []byte) error

FromJSON parses a brainjar error envelope. Returns *RefError when invalid_refs is present, *Error otherwise. Non-envelope bytes become CodeUnknown carrying the raw text so callers still see something.

func IsConflict

func IsConflict(err error) bool

IsConflict reports whether err (or any error wrapped by it) is an Error with CodeConflict.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err (or any error wrapped by it) is an Error with CodeNotFound.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized reports whether err (or any error wrapped by it) is an Error with CodeUnauthorized.

func ToHTTP

func ToHTTP(err error, w http.ResponseWriter)

ToHTTP writes a structured JSON error response. Handlers call this in their error paths — no error-handling middleware needed.

func ToJSON

func ToJSON(err error) []byte

ToJSON returns the JSON bytes of the brainjar error envelope. A nil or non-Error input is wrapped as CodeInternal with a generic message.

Types

type Code

type Code int

Code is an enum of domain error codes. Maps to HTTP status codes for the server, and to exit codes / user messages for the CLI.

const (
	CodeUnknown Code = iota
	CodeNotFound
	CodeConflict
	CodeBadRequest
	CodeUnauthorized
	CodeForbidden
	CodeInternal
)

Domain error codes. CodeUnknown is the zero value and is treated as INTERNAL when mapped to a surface.

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is the single error type for all domain errors. Handlers use ToHTTP() to convert it; CLI commands inspect Code() for UX.

func Conflict

func Conflict(resource, slug string) *Error

Conflict builds a CodeConflict error for a resource that already exists under the given identifier.

func New

func New(code Code, message string) *Error

New builds an Error with the given code and message.

func NotFound

func NotFound(resource, slug string) *Error

NotFound builds a CodeNotFound error for the given resource kind and identifier.

func (*Error) Code

func (e *Error) Code() Code

Code returns the domain code for programmatic classification.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface. Returns the raw message only; callers that want structure go through Code() and Hint().

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus returns the HTTP status code associated with e's Code. Unknown codes map to 500 so the server never returns a naked zero.

func (*Error) Hint

func (e *Error) Hint() string

Hint returns the optional recovery hint, or "" when none was set.

func (*Error) WithHint

func (e *Error) WithHint(hint string) *Error

WithHint returns a copy of e carrying a user-facing hint. Hints are surfaced alongside the message in both the CLI and the HTTP envelope and should tell the reader how to recover.

type RefError

type RefError struct {
	Err         *Error
	InvalidRefs map[string]any
}

RefError carries structured invalid-reference details alongside a validation error. Used by RefChecker and surfaced in API responses.

func (*RefError) Error

func (e *RefError) Error() string

Error implements the error interface, appending the invalid-ref map to the underlying Error's message when non-empty.

func (*RefError) Unwrap

func (e *RefError) Unwrap() error

Unwrap exposes the wrapped *Error so errors.As and the Is* helpers keep working on a RefError.

Jump to

Keyboard shortcuts

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