errors

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package errors defines the framework-wide error model: ErrorCode enum, the Error interface, constructor helpers, and HTTP status mapping.

Every exported function in the orjanda framework that can fail must return a value implementing errors.Error (or wrap one via Unwrap()).

See TAD §1.1 for the full contract.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrValidation = &orjandaError{code: CodeValidation}
	ErrAuth       = &orjandaError{code: CodeAuth}
	ErrPermission = &orjandaError{code: CodePermission}
	ErrNotFound   = &orjandaError{code: CodeNotFound}
	ErrConflict   = &orjandaError{code: CodeConflict}
	ErrInternal   = &orjandaError{code: CodeInternal}
)

Functions

func As

func As(err error, target *Error) bool

As reports whether err (or any wrapped error) satisfies the Error interface, and if so, sets target. This is a convenience wrapper so callers do not need to import the standard "errors" package just to call errors.As.

func Is

func Is(err, target error) bool

Is reports whether err matches target. This is a convenience wrapper so callers do not need to import the standard "errors" package just to call errors.Is.

Types

type Error

type Error interface {
	error // Error() string — human-readable, safe for logs.

	// Code returns the stable ErrorCode category.
	Code() ErrorCode

	// Message returns a human- and LLM-safe description of the failure.
	// It MUST NOT expose raw system internals (stack traces, SQL, DSNs).
	Message() string

	// Details returns structured supplementary information, e.g. per-field
	// validation failures: {"email": "invalid format", "name": "required"}.
	// May be nil when there is no additional context.
	Details() map[string]any

	// Unwrap returns the original underlying error for errors.As/Is chaining.
	// May be nil when this error was not wrapping another.
	Unwrap() error
}

Error is the framework-wide error interface. Every exported Orjanda function that can fail must return a value implementing Error (or wrap one via Unwrap so that errors.As/Is traversal still reaches an Error). See TAD §1.1.

func Auth

func Auth(message string) Error

Auth returns a CodeAuth error.

func Conflict

func Conflict(message string) Error

Conflict returns a CodeConflict error.

func Internal

func Internal(message string, cause error) Error

Internal returns a CodeInternal error, wrapping the underlying cause. The message MUST be a generic, safe string — do not include raw cause details.

func New

func New(code ErrorCode, message string, details map[string]any, cause error) Error

New constructs a bare errors.Error with code, message, optional details map, and an optional underlying cause. Prefer the named constructors below.

func NotFound

func NotFound(message string) Error

NotFound returns a CodeNotFound error.

func Permission

func Permission(message string) Error

Permission returns a CodePermission error.

func Validation

func Validation(message string, details map[string]any) Error

Validation returns a CodeValidation error. details may be nil.

func Wrap

func Wrap(code ErrorCode, message string, cause error) Error

Wrap wraps an existing cause under a given code and message. If cause already implements Error, its Code is overridden by code.

type ErrorCode

type ErrorCode string

ErrorCode is a stable, machine-readable identifier for an error category. Every exported Orjanda function that can fail returns an errors.Error whose Code() is one of the six values below. No package may introduce additional codes for conditions these six already cover. See TAD §1.1.

const (
	// CodeValidation maps to HTTP 400. Used for schema violations (required
	// fields, format checks, uniqueness conflicts that are caught before the
	// DB write), invalid query parameters, and malformed request payloads.
	CodeValidation ErrorCode = "VALIDATION_ERROR"

	// CodeAuth maps to HTTP 401. Used when the caller supplies a missing,
	// expired, or cryptographically invalid authentication credential.
	CodeAuth ErrorCode = "AUTH_ERROR"

	// CodePermission maps to HTTP 403. Used when the caller's identity is
	// recognised but lacks the required role or rule for the requested action.
	// Must be returned by perm.Engine before any DAL call is made (PRD §25.1).
	CodePermission ErrorCode = "PERMISSION_DENIED"

	// CodeNotFound maps to HTTP 404. Used when a requested Document ID or
	// DocType does not exist in the Registry or database.
	CodeNotFound ErrorCode = "NOT_FOUND"

	// CodeConflict maps to HTTP 409. Used for optimistic-locking failures,
	// duplicate unique-field violations caught at the DB layer, and invalid
	// workflow transitions from the current state (TAD §8.1 step 2).
	CodeConflict ErrorCode = "CONFLICT"

	// CodeInternal maps to HTTP 500. Used for unexpected infrastructure
	// failures: DB connectivity, internal invariant violations, etc.
	// The Message() must never expose raw system details to end-users or LLMs.
	CodeInternal ErrorCode = "INTERNAL_ERROR"
)

func (ErrorCode) HTTPStatus

func (c ErrorCode) HTTPStatus() int

HTTPStatus returns the HTTP status code that corresponds to c. This is the canonical mapping table used by the API layer (Phase 6) and documented in TAD §1.1.

Jump to

Keyboard shortcuts

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