// Typed gateway error taxonomy with stable string `code` discriminants. Callers switch on
// `error.code`; they never parse `error.message`. Every message is redacted at construction
// so errors are always safe to log or surface across trust boundaries (ADR-0003).
import { redact } from "../redaction.js";
export const ERROR_CODES = {
    AUTHENTICATION: "GATEWAY_AUTHENTICATION",
    TRANSPORT: "GATEWAY_TRANSPORT",
    MODEL_REFUSAL: "GATEWAY_MODEL_REFUSAL",
    MALFORMED_TOOL_CALL: "GATEWAY_MALFORMED_TOOL_CALL",
    CONTEXT_OVERFLOW: "GATEWAY_CONTEXT_OVERFLOW",
    RATE_LIMIT: "GATEWAY_RATE_LIMIT",
    TIMEOUT: "GATEWAY_TIMEOUT",
    CANCELLED: "GATEWAY_CANCELLED",
    CIRCUIT_OPEN: "GATEWAY_CIRCUIT_OPEN",
    PROVIDER_ERROR: "GATEWAY_PROVIDER_ERROR",
    CONFIG_INVALID: "GATEWAY_CONFIG_INVALID",
    UNKNOWN_MODEL: "GATEWAY_UNKNOWN_MODEL",
};
export class GatewayError extends Error {
    constructor(message, secrets = []) {
        super(redact(message, secrets));
        this.name = new.target.name;
    }
}
export class AuthenticationError extends GatewayError {
    code = ERROR_CODES.AUTHENTICATION;
    retryable = false;
}
export class TransportError extends GatewayError {
    code = ERROR_CODES.TRANSPORT;
    retryable = true;
}
export class ModelRefusalError extends GatewayError {
    code = ERROR_CODES.MODEL_REFUSAL;
    retryable = false;
}
export class MalformedToolCallError extends GatewayError {
    code = ERROR_CODES.MALFORMED_TOOL_CALL;
    retryable = false;
}
export class ContextOverflowError extends GatewayError {
    code = ERROR_CODES.CONTEXT_OVERFLOW;
    retryable = false;
}
export class RateLimitError extends GatewayError {
    code = ERROR_CODES.RATE_LIMIT;
    retryable = true;
    retryAfterMs;
    constructor(message, retryAfterMs = null, secrets = []) {
        super(message, secrets);
        this.retryAfterMs = retryAfterMs;
    }
}
export class TimeoutError extends GatewayError {
    code = ERROR_CODES.TIMEOUT;
    retryable = true;
}
export class CancelledError extends GatewayError {
    code = ERROR_CODES.CANCELLED;
    retryable = false;
}
export class CircuitOpenError extends GatewayError {
    code = ERROR_CODES.CIRCUIT_OPEN;
    retryable = false;
}
export class ProviderError extends GatewayError {
    code = ERROR_CODES.PROVIDER_ERROR;
    retryable = false;
    httpStatus;
    constructor(message, httpStatus, secrets = []) {
        super(message, secrets);
        this.httpStatus = httpStatus;
    }
}
export class ConfigInvalidError extends GatewayError {
    code = ERROR_CODES.CONFIG_INVALID;
    retryable = false;
}
export class UnknownModelError extends GatewayError {
    code = ERROR_CODES.UNKNOWN_MODEL;
    retryable = false;
}
