errx

package
v0.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package errx provides structured, code-based errors for MCP runtime and CLI tooling.

The package implements a code-based error system where each error has:

  • A stable 5-digit error code (e.g., "72000" for registry errors)
  • A category description (e.g., "Registry error")
  • A user-facing message
  • Optional structured context (key-value pairs)
  • Optional cause and base sentinel errors

Error codes follow a scheme where the first two digits represent the domain:

  • 70xxx: CLI/argument validation errors
  • 71xxx: Cluster/provisioning errors
  • 72xxx: Registry errors
  • 73xxx: Operator errors
  • 74xxx: Pipeline errors
  • 75xxx: Build errors
  • 76xxx: Server definition errors
  • 77xxx: Certificate/TLS errors
  • 78xxx: Setup/installation errors
  • 79xxx: Configuration errors

The last three digits are reserved for subcodes (future use).

Example usage:

err := errx.Registry("failed to connect to registry").
	WithContext("url", "registry.mcpruntime.com").
	WithBase(sentinelErr)

if errors.Is(err, sentinelErr) {
	// Handle specific error
}

fmt.Println(errx.UserString(err))  // User-friendly message
fmt.Println(errx.DebugString(err)) // Full debug details

Index

Constants

View Source
const (
	CodeCLI      = "70000"
	CodeCluster  = "71000"
	CodeRegistry = "72000"
	CodeOperator = "73000"
	CodePipeline = "74000"
	CodeBuild    = "75000"
	CodeServer   = "76000"
	CodeCert     = "77000"
	CodeSetup    = "78000"
	CodeConfig   = "79000"
	CodeAuth     = "80000"
)

Error codes follow a stable 5-digit scheme where the first two digits are the domain and the last three digits are reserved for subcodes.

View Source
const (
	DescCLI      = "CLI/argument validation error"
	DescCluster  = "Cluster/provisioning error"
	DescRegistry = "Registry error"
	DescOperator = "Operator error"
	DescPipeline = "Pipeline error"
	DescBuild    = "Build error"
	DescServer   = "Server definition error"
	DescCert     = "Certificate/TLS error"
	DescSetup    = "Setup/installation error"
	DescConfig   = "Configuration error"
	DescAuth     = "Authentication / credential error"
)

Variables

This section is empty.

Functions

func DebugString

func DebugString(err error) string

DebugString returns a verbose error string with codes, context, and chain.

func DescriptionFor

func DescriptionFor(code string) (string, bool)

DescriptionFor returns the registry description for a code.

func IsError

func IsError(err error) bool

IsError checks if the given error is an errx.Error.

func IsValidCode

func IsValidCode(code string) bool

IsValidCode checks if the given error code is registered.

func LogrKV

func LogrKV(err error) ([]any, bool)

LogrKV returns controller-runtime logr-style key/value pairs for an errx.Error. The second return value is false when err is nil, not an *Error, or a typed nil *Error stored in an error interface (errors.As succeeds but the pointer value is nil).

func UserString

func UserString(err error) string

UserString returns a user-safe error message. It extracts the most user-friendly message from an errx.Error, falling back to the standard error message for non-errx errors.

Types

type Error

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

Error is the base error type for MCP runtime errors.

func CLI

func CLI(message string) *Error

CLI creates a CLI/argument validation error with code 70000. Use this for errors related to command-line argument validation, invalid user input, or CLI-specific issues. This is heavily used in internal/cli/errors.go for CLI sentinel errors.

func CreateByCode

func CreateByCode(code, description, message string, cause error) *Error

CreateByCode creates an Error using the provided code, description, and message. This is a convenience function that directly calls New() or Wrap().

func FromSentinel

func FromSentinel(sentinel error, lookup func(error) (code, description string), message string, cause error) *Error

FromSentinel creates an Error from a sentinel error and optional message/cause. This is useful when you have a sentinel error and want to create an errx.Error with the same category. The sentinel is used to determine the category via a lookup function. Panics if sentinel is nil (sentinel is required for error identification).

func New

func New(code, description, message string) *Error

New creates a new Error with the provided code, description, and message. Panics if code is empty (code is required for error identification).

func Operator

func Operator(message string) *Error

Operator creates an operator error. This is used in internal/operator/errors.go for operator-specific errors.

func Wrap

func Wrap(code, description, message string, cause error) *Error

Wrap creates a new Error and attaches a cause error. Panics if code is empty (code is required for error identification).

func WrapCLI

func WrapCLI(message string, cause error) *Error

WrapCLI wraps a cause with a CLI/argument validation error. Use this when a CLI error is caused by another error that should be preserved.

func WrapOperator

func WrapOperator(message string, cause error) *Error

WrapOperator wraps a cause with an operator error. This is used in internal/operator/errors.go for operator-specific errors.

func (*Error) Base

func (e *Error) Base() error

Base returns the sentinel base error, if any.

func (*Error) Cause

func (e *Error) Cause() error

Cause returns the wrapped error, if any.

func (*Error) Code

func (e *Error) Code() string

Code returns the stable error code.

func (*Error) Context

func (e *Error) Context() map[string]any

Context returns a copy of the structured context.

func (*Error) Description

func (e *Error) Description() string

Description returns the category description.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Is

func (e *Error) Is(target error) bool

Is implements error matching for sentinel errors. This allows errors.Is(err, sentinel) to match the base sentinel even though Unwrap() returns the cause.

func (*Error) Message

func (e *Error) Message() string

Message returns the user-facing message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the immediate wrapped error (cause). This follows Go's error wrapping convention where Unwrap() returns the direct cause, not the base sentinel.

func (*Error) WithBase

func (e *Error) WithBase(base error) *Error

WithBase sets the sentinel base error used for errors.Is matching. Returns a new error with the base set to avoid mutating the original. Panics if called on a nil receiver.

func (*Error) WithContext

func (e *Error) WithContext(key string, value any) *Error

WithContext adds a context key/value pair. Returns a new error with the added context to avoid mutating the original. Panics if called on a nil receiver or if key is empty.

func (*Error) WithContextMap

func (e *Error) WithContextMap(ctx map[string]any) *Error

WithContextMap merges a context map into the error context. Returns a new error with the merged context to avoid mutating the original. Always returns a clone to maintain immutability, even if ctx is empty. Panics if called on a nil receiver or if any key in ctx is empty.

type RegistryEntry

type RegistryEntry struct {
	Code        string
	Description string
}

RegistryEntry describes a registered error code.

func ErrorRegistry

func ErrorRegistry() []RegistryEntry

ErrorRegistry returns the error registry in deterministic order. This provides a list of all registered error codes and their descriptions.

Jump to

Keyboard shortcuts

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