/**
 * @fileoverview Defines standardized error codes, a custom error class, and related schemas
 * for handling errors within the Model Context Protocol (MCP) server and its components.
 * This module provides a structured way to represent and communicate errors, ensuring
 * consistency and clarity for both server-side operations and client-side error handling.
 * @module src/types-global/errors
 */
import { z } from 'zod';
/**
 * Defines JSON-RPC 2.0 error codes, including standard and implementation-defined codes.
 * @see https://www.jsonrpc.org/specification#error_object
 */
export var JsonRpcErrorCode;
(function (JsonRpcErrorCode) {
    // Standard JSON-RPC 2.0 Errors
    JsonRpcErrorCode[JsonRpcErrorCode["ParseError"] = -32700] = "ParseError";
    JsonRpcErrorCode[JsonRpcErrorCode["InvalidRequest"] = -32600] = "InvalidRequest";
    JsonRpcErrorCode[JsonRpcErrorCode["MethodNotFound"] = -32601] = "MethodNotFound";
    JsonRpcErrorCode[JsonRpcErrorCode["InvalidParams"] = -32602] = "InvalidParams";
    JsonRpcErrorCode[JsonRpcErrorCode["InternalError"] = -32603] = "InternalError";
    // Implementation-defined server-errors (-32000 to -32099)
    JsonRpcErrorCode[JsonRpcErrorCode["ServiceUnavailable"] = -32000] = "ServiceUnavailable";
    JsonRpcErrorCode[JsonRpcErrorCode["NotFound"] = -32001] = "NotFound";
    JsonRpcErrorCode[JsonRpcErrorCode["Conflict"] = -32002] = "Conflict";
    JsonRpcErrorCode[JsonRpcErrorCode["RateLimited"] = -32003] = "RateLimited";
    JsonRpcErrorCode[JsonRpcErrorCode["Timeout"] = -32004] = "Timeout";
    JsonRpcErrorCode[JsonRpcErrorCode["Forbidden"] = -32005] = "Forbidden";
    JsonRpcErrorCode[JsonRpcErrorCode["Unauthorized"] = -32006] = "Unauthorized";
    JsonRpcErrorCode[JsonRpcErrorCode["ValidationError"] = -32007] = "ValidationError";
    JsonRpcErrorCode[JsonRpcErrorCode["ConfigurationError"] = -32008] = "ConfigurationError";
    JsonRpcErrorCode[JsonRpcErrorCode["InitializationFailed"] = -32009] = "InitializationFailed";
    JsonRpcErrorCode[JsonRpcErrorCode["DatabaseError"] = -32010] = "DatabaseError";
    JsonRpcErrorCode[JsonRpcErrorCode["SerializationError"] = -32070] = "SerializationError";
    JsonRpcErrorCode[JsonRpcErrorCode["UnknownError"] = -32099] = "UnknownError";
})(JsonRpcErrorCode || (JsonRpcErrorCode = {}));
/**
 * Custom error class for MCP-specific errors, extending the built-in `Error` class.
 * It standardizes error reporting by encapsulating a `JsonRpcErrorCode`, a descriptive
 * human-readable message, and optional structured data for more context.
 *
 * This class is central to error handling within the MCP framework, allowing for
 * consistent error creation and propagation.
 */
export class McpError extends Error {
    /**
     * The standardized error code from {@link JsonRpcErrorCode}.
     */
    code;
    /**
     * Optional additional data about the error, conforming to the JSON-RPC 2.0 specification.
     * This can be any structured data that helps in understanding or debugging the error.
     * @see https://www.jsonrpc.org/specification#error_object
     */
    data;
    /**
     * Creates an instance of McpError.
     *
     * @param code - The standardized error code that categorizes the error.
     * @param message - A human-readable description of the error.
     * @param data - Optional. A record containing additional structured data about the error.
     */
    constructor(code, message, data, options) {
        super(message, options);
        this.code = code;
        if (data) {
            this.data = data;
        }
        this.name = 'McpError';
        // Maintain a proper prototype chain.
        Object.setPrototypeOf(this, McpError.prototype);
        // Capture the stack trace, excluding the constructor call from it, if available.
        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, McpError);
        }
    }
}
/** Create an InvalidParams (-32602) error. */
export const invalidParams = (message, data, options) => new McpError(JsonRpcErrorCode.InvalidParams, message, data, options);
/** Create an InvalidRequest (-32600) error. */
export const invalidRequest = (message, data, options) => new McpError(JsonRpcErrorCode.InvalidRequest, message, data, options);
/** Create a NotFound (-32001) error. */
export const notFound = (message, data, options) => new McpError(JsonRpcErrorCode.NotFound, message, data, options);
/** Create a Forbidden (-32005) error. */
export const forbidden = (message, data, options) => new McpError(JsonRpcErrorCode.Forbidden, message, data, options);
/** Create an Unauthorized (-32006) error. */
export const unauthorized = (message, data, options) => new McpError(JsonRpcErrorCode.Unauthorized, message, data, options);
/** Create a ValidationError (-32007) error. */
export const validationError = (message, data, options) => new McpError(JsonRpcErrorCode.ValidationError, message, data, options);
/** Create a Conflict (-32002) error. */
export const conflict = (message, data, options) => new McpError(JsonRpcErrorCode.Conflict, message, data, options);
/** Create a RateLimited (-32003) error. */
export const rateLimited = (message, data, options) => new McpError(JsonRpcErrorCode.RateLimited, message, data, options);
/** Create a Timeout (-32004) error. */
export const timeout = (message, data, options) => new McpError(JsonRpcErrorCode.Timeout, message, data, options);
/** Create a ServiceUnavailable (-32000) error. */
export const serviceUnavailable = (message, data, options) => new McpError(JsonRpcErrorCode.ServiceUnavailable, message, data, options);
/** Create a ConfigurationError (-32008) error. */
export const configurationError = (message, data, options) => new McpError(JsonRpcErrorCode.ConfigurationError, message, data, options);
/** Create an InternalError (-32603) error. */
export const internalError = (message, data, options) => new McpError(JsonRpcErrorCode.InternalError, message, data, options);
/** Create a SerializationError (-32070) error — JSON/XML/parser failures. */
export const serializationError = (message, data, options) => new McpError(JsonRpcErrorCode.SerializationError, message, data, options);
/** Create a DatabaseError (-32010) error. */
export const databaseError = (message, data, options) => new McpError(JsonRpcErrorCode.DatabaseError, message, data, options);
/**
 * Zod schema for validating error objects. This schema can be used for:
 * - Validating error structures when parsing error responses from external services.
 * - Ensuring consistency when creating or handling error objects internally.
 * - Generating TypeScript types for error objects.
 *
 * The schema enforces the presence of a `code` (from {@link JsonRpcErrorCode}) and a `message`,
 * and allows for optional `data`.
 */
export const ErrorSchema = z
    .object({
    /**
     * The error code, corresponding to one of the {@link JsonRpcErrorCode} enum values.
     * This field is required and helps in programmatically identifying the error type.
     */
    code: z
        .nativeEnum(JsonRpcErrorCode)
        .describe('Standardized error code from JsonRpcErrorCode enum'),
    /**
     * A human-readable, descriptive message explaining the error.
     * This field is required and provides context to developers or users.
     */
    message: z
        .string()
        .min(1, 'Error message cannot be empty.')
        .describe('Detailed human-readable error message'),
    /**
     * Optional. A record containing additional structured data or context about the error,
     * conforming to the JSON-RPC 2.0 `data` field.
     */
    data: z
        .record(z.string(), z.unknown())
        .optional()
        .describe('Optional structured data providing more context about the error'),
})
    .describe('Schema for validating structured error objects, ensuring consistency in error reporting.');
//# sourceMappingURL=errors.js.map