// SPDX-License-Identifier: Apache-2.0
// Copyright 2024-2026 Vorion LLC
/**
 * Typed Error Classes for Vorion
 *
 * Provides structured error types with HTTP status codes for better error handling,
 * observability, and programmatic error discrimination.
 *
 * @packageDocumentation
 */
/**
 * Base error class for all Vorion errors.
 * Extends Error with structured metadata for API responses and logging.
 */
export class VorionError extends Error {
    /** Error code for programmatic identification */
    code;
    /** HTTP status code for API responses */
    statusCode;
    /** Additional error details for debugging */
    details;
    constructor(message, details) {
        super(message);
        this.name = 'VorionError';
        this.code = 'VORION_ERROR';
        this.statusCode = 500;
        if (details !== undefined) {
            this.details = details;
        }
        // Maintains proper stack trace for where our error was thrown (V8 engines)
        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, this.constructor);
        }
    }
    /**
     * Convert error to a JSON-serializable object for API responses
     */
    toJSON() {
        const json = {
            code: this.code,
            message: this.message,
        };
        if (this.details) {
            json.details = this.details;
        }
        return json;
    }
}
/**
 * Validation error for invalid input data.
 * HTTP 400 Bad Request
 */
export class ValidationError extends VorionError {
    code = 'VALIDATION_ERROR';
    statusCode = 400;
    constructor(message, details) {
        super(message, details);
        this.name = 'ValidationError';
    }
}
/**
 * Not found error for missing resources.
 * HTTP 404 Not Found
 */
export class NotFoundError extends VorionError {
    code = 'NOT_FOUND';
    statusCode = 404;
    constructor(message, details) {
        super(message, details);
        this.name = 'NotFoundError';
    }
}
/**
 * Unauthorized error for authentication failures.
 * HTTP 401 Unauthorized
 */
export class UnauthorizedError extends VorionError {
    code = 'UNAUTHORIZED';
    statusCode = 401;
    constructor(message, details) {
        super(message, details);
        this.name = 'UnauthorizedError';
    }
}
/**
 * Forbidden error for authorization failures.
 * HTTP 403 Forbidden
 */
export class ForbiddenError extends VorionError {
    code = 'FORBIDDEN';
    statusCode = 403;
    constructor(message, details) {
        super(message, details);
        this.name = 'ForbiddenError';
    }
}
/**
 * Conflict error for resource state conflicts.
 * HTTP 409 Conflict
 */
export class ConflictError extends VorionError {
    code = 'CONFLICT';
    statusCode = 409;
    constructor(message, details) {
        super(message, details);
        this.name = 'ConflictError';
    }
}
/**
 * Rate limit error when request limits are exceeded.
 * HTTP 429 Too Many Requests
 */
export class RateLimitError extends VorionError {
    code = 'RATE_LIMIT_EXCEEDED';
    statusCode = 429;
    /** Seconds until rate limit resets */
    retryAfter;
    constructor(message, retryAfter, details) {
        super(message, details);
        this.name = 'RateLimitError';
        if (retryAfter !== undefined) {
            this.retryAfter = retryAfter;
        }
    }
    toJSON() {
        const json = super.toJSON();
        if (this.retryAfter !== undefined) {
            json.retryAfter = this.retryAfter;
        }
        return json;
    }
}
/**
 * Configuration error for invalid or missing configuration.
 * HTTP 500 Internal Server Error
 */
export class ConfigurationError extends VorionError {
    code = 'CONFIGURATION_ERROR';
    statusCode = 500;
    constructor(message, details) {
        super(message, details);
        this.name = 'ConfigurationError';
    }
}
/**
 * Encryption error for encryption/decryption failures.
 * HTTP 500 Internal Server Error
 */
export class EncryptionError extends VorionError {
    code = 'ENCRYPTION_ERROR';
    statusCode = 500;
    constructor(message, details) {
        super(message, details);
        this.name = 'EncryptionError';
    }
}
/**
 * Escalation error for escalation workflow failures.
 * HTTP 400 Bad Request
 */
export class EscalationError extends VorionError {
    code = 'ESCALATION_ERROR';
    statusCode = 400;
    constructor(message, details) {
        super(message, details);
        this.name = 'EscalationError';
    }
}
/**
 * Database error for database operation failures.
 * HTTP 500 Internal Server Error
 */
export class DatabaseError extends VorionError {
    code = 'DATABASE_ERROR';
    statusCode = 500;
    constructor(message, details) {
        super(message, details);
        this.name = 'DatabaseError';
    }
}
/**
 * External service error for third-party service failures.
 * HTTP 502 Bad Gateway
 */
export class ExternalServiceError extends VorionError {
    code = 'EXTERNAL_SERVICE_ERROR';
    statusCode = 502;
    constructor(message, details) {
        super(message, details);
        this.name = 'ExternalServiceError';
    }
}
/**
 * Timeout error for operation timeouts.
 * HTTP 504 Gateway Timeout
 */
export class TimeoutError extends VorionError {
    code = 'TIMEOUT';
    statusCode = 504;
    constructor(message, details) {
        super(message, details);
        this.name = 'TimeoutError';
    }
}
/**
 * Type guard to check if an error is a VorionError
 */
export function isVorionError(error) {
    return error instanceof VorionError;
}
/**
 * Helper to wrap unknown errors in a VorionError
 */
export function wrapError(error, fallbackMessage = 'An unexpected error occurred') {
    if (isVorionError(error)) {
        return error;
    }
    if (error instanceof Error) {
        return new VorionError(error.message, { originalError: error.name });
    }
    return new VorionError(fallbackMessage, { originalError: String(error) });
}
/**
 * Repository error for data access layer failures.
 * HTTP 500 Internal Server Error
 */
export class RepositoryError extends VorionError {
    operation;
    entity;
    code = 'REPOSITORY_ERROR';
    statusCode = 500;
    constructor(message, operation, entity, details) {
        super(message, { operation, entity, ...details });
        this.operation = operation;
        this.entity = entity;
        this.name = 'RepositoryError';
    }
}
/**
 * Service error for business logic failures.
 * HTTP 500 Internal Server Error
 */
export class ServiceError extends VorionError {
    service;
    operation;
    code = 'SERVICE_ERROR';
    statusCode = 500;
    constructor(message, service, operation, details) {
        super(message, { service, operation, ...details });
        this.service = service;
        this.operation = operation;
        this.name = 'ServiceError';
    }
}
/**
 * Integration error for external service failures.
 * HTTP 502 Bad Gateway
 */
export class IntegrationError extends VorionError {
    integration;
    operation;
    code = 'INTEGRATION_ERROR';
    statusCode = 502;
    constructor(message, integration, operation, details) {
        super(message, { integration, operation, ...details });
        this.integration = integration;
        this.operation = operation;
        this.name = 'IntegrationError';
    }
}
/**
 * Queue error for message queue operations.
 * HTTP 500 Internal Server Error
 */
export class QueueError extends VorionError {
    queue;
    operation;
    code = 'QUEUE_ERROR';
    statusCode = 500;
    constructor(message, queue, operation, details) {
        super(message, { queue, operation, ...details });
        this.queue = queue;
        this.operation = operation;
        this.name = 'QueueError';
    }
}
/**
 * Cache error for caching layer failures.
 * HTTP 500 Internal Server Error
 */
export class CacheError extends VorionError {
    operation;
    code = 'CACHE_ERROR';
    statusCode = 500;
    constructor(message, operation, details) {
        super(message, { operation, ...details });
        this.operation = operation;
        this.name = 'CacheError';
    }
}
/**
 * Governance error for policy evaluation failures.
 * HTTP 500 Internal Server Error
 */
export class GovernanceError extends VorionError {
    operation;
    code = 'GOVERNANCE_ERROR';
    statusCode = 500;
    constructor(message, operation, details) {
        super(message, { operation, ...details });
        this.operation = operation;
        this.name = 'GovernanceError';
    }
}
/**
 * Trust engine error for trust calculation failures.
 * HTTP 500 Internal Server Error
 */
export class TrustEngineError extends VorionError {
    operation;
    entityId;
    code = 'TRUST_ENGINE_ERROR';
    statusCode = 500;
    constructor(message, operation, entityId, details) {
        super(message, { operation, entityId, ...details });
        this.operation = operation;
        this.entityId = entityId;
        this.name = 'TrustEngineError';
    }
}
/**
 * Wrap an error with operation context for better debugging.
 * Re-throws VorionErrors with additional context, wraps other errors.
 */
export function wrapErrorWithContext(error, context) {
    const wrappedDetails = {
        operation: context.operation,
        component: context.component,
        ...context.details,
    };
    if (isVorionError(error)) {
        // Add context to existing VorionError
        error.details = { ...error.details, ...wrappedDetails };
        return error;
    }
    if (error instanceof Error) {
        return new VorionError(`${context.component}.${context.operation} failed: ${error.message}`, { originalError: error.name, stack: error.stack, ...wrappedDetails });
    }
    return new VorionError(`${context.component}.${context.operation} failed: ${String(error)}`, { originalError: String(error), ...wrappedDetails });
}
//# sourceMappingURL=errors.js.map