errors

package
v0.11.1 Latest Latest
Warning

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

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

Documentation

Overview

Package errors provides structured error types for better observability and programmatic error handling across the application.

Overview

This package implements a structured error system with error codes for programmatic handling, human-readable messages, cause chaining, and optional context for debugging. It supports the standard errors.Is and errors.As functions through the Unwrap interface.

Error Codes

Predefined error codes align with the API error contract:

  • ErrCodeNotFound: Resource not found (HTTP 404)
  • ErrCodeUnauthorized: Authentication/authorization failure (HTTP 401/403)
  • ErrCodeTimeout: Operation timeout (HTTP 504)
  • ErrCodeInternal: Internal server error (HTTP 500)
  • ErrCodeInvalidRequest: Malformed or invalid input (HTTP 400)
  • ErrCodeRateLimitExceeded: Rate limit exceeded (HTTP 429)
  • ErrCodeMethodNotAllowed: HTTP method not allowed (HTTP 405)
  • ErrCodeUnavailable: Service temporarily unavailable (HTTP 503)

Usage

Create a simple error:

err := errors.New(errors.ErrCodeNotFound, "GPU not found")

Wrap an existing error:

err := errors.Wrap(errors.ErrCodeInternal, "collection failed", originalErr)

Wrap with additional context:

err := errors.WrapWithContext(
    errors.ErrCodeTimeout,
    "failed to collect GPU metrics",
    ctx.Err(),
    map[string]any{
        "command":  "nvidia-smi",
        "node":     nodeName,
        "timeout":  "10s",
    },
)

Error Handling

The StructuredError type implements the standard error interface and supports error unwrapping:

var structErr *errors.StructuredError
if errors.As(err, &structErr) {
    log.Printf("Error code: %s, Message: %s", structErr.Code, structErr.Message)
    if structErr.Context != nil {
        log.Printf("Context: %v", structErr.Context)
    }
}

Thread Safety

All functions in this package are thread-safe and can be called concurrently.

Index

Constants

View Source
const (
	// ExitSuccess indicates successful execution.
	ExitSuccess = 0

	// ExitError is the generic error code for unclassified failures.
	ExitError = 1

	// ExitInvalidInput indicates malformed input, validation failure, or bad arguments.
	// Maps to: ErrCodeInvalidRequest, ErrCodeMethodNotAllowed
	ExitInvalidInput = 2

	// ExitNotFound indicates a requested resource was not found.
	// Maps to: ErrCodeNotFound
	ExitNotFound = 3

	// ExitUnauthorized indicates authentication or authorization failure.
	// Maps to: ErrCodeUnauthorized
	ExitUnauthorized = 4

	// ExitTimeout indicates an operation exceeded its time limit.
	// Maps to: ErrCodeTimeout
	ExitTimeout = 5

	// ExitUnavailable indicates a service or resource is temporarily unavailable.
	// Maps to: ErrCodeUnavailable
	ExitUnavailable = 6

	// ExitRateLimited indicates the client exceeded a rate limit.
	// Maps to: ErrCodeRateLimitExceeded
	ExitRateLimited = 7

	// ExitInternal indicates an internal error (reserved for unexpected failures).
	// Maps to: ErrCodeInternal
	ExitInternal = 8
)

Exit codes for CLI commands, following Unix conventions. These codes enable predictable scripting and automation.

Ranges:

  • 0: Success
  • 1: Generic error (catch-all)
  • 2-63: Application-specific errors

Variables

This section is empty.

Functions

func ExitCodeFromError

func ExitCodeFromError(err error) int

ExitCodeFromError extracts an appropriate exit code from an error. It checks for StructuredError to determine the specific exit code, falling back to ExitError (1) for unstructured errors.

func IsNetworkError added in v0.8.4

func IsNetworkError(err error) bool

IsNetworkError reports whether err indicates a network-level connectivity problem (DNS resolution, TCP dial, TLS handshake, etc.).

It does NOT match context.DeadlineExceeded or context.Canceled — those represent application-level timeouts, not network failures.

Types

type ErrorCode

type ErrorCode string

ErrorCode represents a structured error classification.

const (
	// ErrCodeNotFound indicates a requested resource was not found.
	ErrCodeNotFound ErrorCode = "NOT_FOUND"
	// ErrCodeUnauthorized indicates authentication or authorization failure.
	ErrCodeUnauthorized ErrorCode = "UNAUTHORIZED"
	// ErrCodeTimeout indicates an operation exceeded its time limit.
	ErrCodeTimeout ErrorCode = "TIMEOUT"
	// ErrCodeInternal indicates an internal system error.
	ErrCodeInternal ErrorCode = "INTERNAL"
	// ErrCodeInvalidRequest indicates malformed or invalid input.
	ErrCodeInvalidRequest ErrorCode = "INVALID_REQUEST"
	// ErrCodeRateLimitExceeded indicates the client exceeded an enforced request limit.
	ErrCodeRateLimitExceeded ErrorCode = "RATE_LIMIT_EXCEEDED"
	// ErrCodeMethodNotAllowed indicates the HTTP method is not allowed for the resource.
	ErrCodeMethodNotAllowed ErrorCode = "METHOD_NOT_ALLOWED"
	// ErrCodeUnavailable indicates a service or resource is temporarily unavailable.
	//
	// Note: this value is aligned with the public API error contract.
	ErrCodeUnavailable ErrorCode = "SERVICE_UNAVAILABLE"
)

type StructuredError

type StructuredError struct {
	Code    ErrorCode
	Message string
	Cause   error
	Context map[string]any
}

StructuredError provides structured error information for better observability. It includes an error code for programmatic handling, a human-readable message, the underlying cause, and optional context for debugging.

func New

func New(code ErrorCode, message string) *StructuredError

New creates a new StructuredError with the given code and message.

func NewWithContext

func NewWithContext(code ErrorCode, message string, context map[string]any) *StructuredError

NewWithContext creates a new StructuredError with context information.

func Wrap

func Wrap(code ErrorCode, message string, cause error) *StructuredError

Wrap wraps an existing error with additional context.

func WrapWithContext

func WrapWithContext(code ErrorCode, message string, cause error, context map[string]any) *StructuredError

WrapWithContext wraps an error with additional context information.

func (*StructuredError) Error

func (e *StructuredError) Error() string

Error implements the error interface.

func (*StructuredError) Unwrap

func (e *StructuredError) Unwrap() error

Unwrap returns the underlying cause for errors.Is and errors.As support.

Jump to

Keyboard shortcuts

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