Error Handling Types
Since 1.9.0
Types for advanced error handling patterns.
Overview
Error Handling Types provides type-level error handling utilities. It supports Result types, Either types, Option types, error chains, recovery strategies, and domain-specific errors.
This module enables building type-safe error handling applications with proper constraints for error types, recovery mechanisms, and validation errors.
Basic Usage
import type { Result, Either, Option, ValidationError, RecoveryStrategy } from 'uni-types'
// Define result type
type ApiResult = Result<Data, NetworkError>
// Define either type
type ConfigValue = Either<ValidationError, Config>
// Define option type
type MaybeValue = Option<string>Key Types
Result
Result type (Success or Failure).
type Result<T, E = Error> = Success<T> | Failure<E>Success
Success result.
interface Success<T> {
ok: true
value: T
}Failure
Failure result.
interface Failure<E> {
ok: false
error: E
}Either
Either type (Left or Right).
type Either<L, R> = Left<L> | Right<R>Left / Right
Left and Right values.
interface Left<L> {
type: 'left'
value: L
}
interface Right<R> {
type: 'right'
value: R
}Option
Option type (Some or None).
type Option<T> = Some<T> | NoneSome / None
Some and None values.
interface Some<T> {
type: 'some'
value: T
}
interface None {
type: 'none'
}ErrorBase
Base error structure.
interface ErrorBase<T extends ErrorType = ErrorType> {
type: T
message: ErrorMessage
code?: ErrorCode
stack?: ErrorStack
cause?: unknown
timestamp?: number
context?: Record<string, unknown>
}CustomError
Custom error type.
interface CustomError<T = unknown> extends ErrorBase<string> {
name: string
data?: T
retryable?: boolean
severity?: ErrorSeverity
}ErrorSeverity
Error severity levels.
type ErrorSeverity = 'low' | 'medium' | 'high' | 'critical'RecoveryStrategy
Recovery strategy types.
type RecoveryStrategy =
| 'retry' // Retry the operation
| 'fallback' // Use fallback value
| 'default' // Use default value
| 'skip' // Skip and continue
| 'abort' // Abort operation
| 'escalate' // Escalate to higher handlerRetryConfig
Retry configuration.
interface RetryConfig {
maxAttempts: number
delay: number
backoff?: 'linear' | 'exponential' | 'fixed'
maxDelay?: number
retryOn?: (error: Error) => boolean
onRetry?: (error: Error, attempt: number) => void
}ValidationError
Validation error type.
interface ValidationError<T = unknown> extends CustomError<'validation'> {
type: 'validation'
field?: string
path?: string
value?: T
constraint?: string
}NetworkError
Network error type.
interface NetworkError extends CustomError<'network'> {
type: 'network'
status?: number
statusText?: string
url?: string
retryable?: boolean
timeout?: boolean
}AuthenticationError
Authentication error type.
interface AuthenticationError extends CustomError<'authentication'> {
type: 'authentication'
code?: 'unauthorized' | 'forbidden' | 'token_expired' | 'invalid_token'
}TimeoutError
Timeout error type.
interface TimeoutError extends CustomError<'timeout'> {
type: 'timeout'
operation?: string
timeoutMs?: number
}Panic
Panic type (unrecoverable error).
interface Panic extends CustomError<'panic'> {
type: 'panic'
fatal: true
exitCode?: number
}Related
- Event System - Event handling types
- Functional Programming - Functional patterns
- Validation - Validation types