errors

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package errors defines standard error types used across llm-router services.

Index

Constants

View Source
const (
	CodeInvalidRequest      = "invalid_request_error"
	CodeNotFound            = "not_found"
	CodeProviderNotFound    = "provider_not_found"
	CodeModelNotFound       = "model_not_found"
	CodeEndpointUnsupported = "endpoint_not_supported"
	CodeNoCredential        = "no_credential"
	CodeProviderDisabled    = "provider_disabled"
	CodeModelNotAllowed     = "model_not_allowed"
	CodeProviderNotAllowed  = "provider_not_allowed"
	CodeCredentialNotAllow  = "credential_not_allowed"
	CodeAuthError           = "auth_error"
	CodeMissingToken        = "missing_token"
	CodeInvalidToken        = "invalid_token"
	CodePaymentRequired     = "payment_required"
	CodeRateLimit           = "rate_limit"
	CodeQuotaExceeded       = "quota_exceeded"
	CodeOverloaded          = "overloaded"
	CodeTimeout             = "timeout"
	CodeTransportError      = "transport_error"
	CodeUpstreamError       = "upstream_error"
	CodeModelUnavailable    = "model_unavailable"
	CodeStructuralFault     = "structural_fault"
	CodeServerError         = "server_error"
	CodeInternalError       = "internal_error"
)

Wire error codes. Single source of truth for status/code strings rendered on both API surfaces.

Variables

View Source
var (
	// ErrNotFound is returned when a requested resource does not exist
	ErrNotFound = errors.New("not found")

	// ErrAlreadyExists is returned when attempting to create a duplicate resource
	ErrAlreadyExists = errors.New("already exists")

	// ErrInvalidInput is returned when input validation fails
	ErrInvalidInput = errors.New("invalid input")

	// ErrUnauthorized is returned for authentication failures
	ErrUnauthorized = errors.New("unauthorized")

	// ErrForbidden is returned for authorization failures
	ErrForbidden = errors.New("forbidden")

	// ErrModelNotAllowed is returned when a token's rules forbid the requested model
	ErrModelNotAllowed = errors.New("model not allowed by token rules")

	// ErrProviderNotAllowed is returned when a token's rules forbid the requested provider
	ErrProviderNotAllowed = errors.New("provider not allowed by token rules")

	// ErrCredentialNotAllowed is returned when a token's rules forbid all credentials for a provider
	ErrCredentialNotAllowed = errors.New("no allowed credential available for provider")

	// ErrNoCredential is returned when no credential is available for a provider
	ErrNoCredential = errors.New("no credential available for provider")

	// ErrProviderNotFound is returned when no provider can handle a given ModelId
	ErrProviderNotFound = errors.New("provider not found for model")
	// ErrProviderDisabled is returned when the provider is switched off
	ErrProviderDisabled = errors.New("provider is disabled")

	// ErrModelDisabled is returned when an admin override disables the requested model
	ErrModelDisabled = errors.New("model is disabled by administrator")

	// ErrEndpointNotSupported is returned when the provider or model does
	// not serve the requested endpoint (e.g. audio/transcriptions)
	ErrEndpointNotSupported = errors.New("endpoint not supported")

	// ErrTimeout marks upstream timeouts outside the ProviderError path
	// (e.g. transport-level deadlines). Prefer ProviderError with
	// ErrorTypeUpstream when an upstream response exists.
	ErrTimeout = errors.New("upstream timeout")

	// ErrRateLimited marks upstream rate limits outside the ProviderError
	// path. Prefer ProviderError with ErrorTypeRateLimit when an upstream
	// response exists.
	ErrRateLimited = errors.New("upstream rate limited")
)

Standard sentinel errors

Functions

func AnthropicErrorType added in v0.3.8

func AnthropicErrorType(code string) string

AnthropicErrorType maps a wire code to its Anthropic error type. Single source of truth for the negotiated {type:error,error:{type,message}} envelope served on Anthropic-style requests.

func AnthropicStatus added in v0.3.8

func AnthropicStatus(code string, status int) int

AnthropicStatus maps a wire status to the Anthropic HTTP status: rate limits surface as 429 and congestion as 529; everything else keeps the shared status.

func ErrorTypeForCode added in v0.1.2

func ErrorTypeForCode(code string) string

ErrorTypeForCode maps a wire code to its OpenAI error type.

func NewNotFoundError

func NewNotFoundError(resource, id string) error

NewNotFoundError creates a NotFoundError

func NewValidationError

func NewValidationError(field, message string) error

NewValidationError creates a ValidationError

func SanitizeResponseHeaders added in v0.4.0

func SanitizeResponseHeaders(headers map[string]string) map[string]string

SanitizeResponseHeaders canonicalizes names, drops anything outside the allow-list, and validates values. Retry-After accepts non-negative integer seconds or an HTTP date; anything else is dropped. Returns nil when nothing survives, so callers can range the result unconditionally.

func WriteResponseHeaders added in v0.4.0

func WriteResponseHeaders(w http.ResponseWriter, headers map[string]string)

WriteResponseHeaders sets sanitized plugin-supplied headers on the response. Call before the first WriteHeader only: after streaming starts headers no longer reach the client.

Types

type APIError added in v0.1.2

type APIError struct {
	Status int
	Code   string
}

APIError is the transport mapping of a domain error: HTTP status plus the wire code. Both API surfaces (OpenAI /v1 and dashboard) render from it.

func ToAPIError added in v0.1.2

func ToAPIError(err error) APIError

ToAPIError maps any domain error to its wire representation. Single source of truth for status/code selection; no message-substring matching. ProviderError carries its own status and code: the plugin already produced the terminal OpenAI-shaped error, so the router renders it verbatim after sanitizing the status into range.

type NotFoundError

type NotFoundError struct {
	Resource string
	ID       string
}

NotFoundError wraps ErrNotFound with context about what was not found

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

func (*NotFoundError) Unwrap

func (e *NotFoundError) Unwrap() error

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError wraps ErrInvalidInput with details about what failed validation

func (*ValidationError) Error

func (e *ValidationError) Error() string

func (*ValidationError) Unwrap

func (e *ValidationError) Unwrap() error

Jump to

Keyboard shortcuts

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