export class McpError extends Error {
    code;
    details;
    constructor(message, code, details) {
        super(message);
        this.name = this.constructor.name;
        this.code = code;
        this.details = details;
        if (Error.captureStackTrace) {
            Error.captureStackTrace(this, this.constructor);
        }
    }
    toJSON() {
        return {
            error: this.name,
            message: this.message,
            code: this.code,
            details: this.details,
        };
    }
}
export class ClientError extends McpError {
    constructor(message, details) {
        super(message, 1, details);
    }
}
export class ServerError extends McpError {
    constructor(message, details) {
        super(message, 2, details);
    }
}
export class NetworkError extends McpError {
    constructor(message, details) {
        super(message, 3, details);
    }
}
export class AuthError extends McpError {
    constructor(message, details) {
        super(message, 4, details);
    }
}
export function isAuthenticationError(errorMessage) {
    return /invalid_token|unauthorized|missing.*token|access.*token|authentication|401|403/i.test(errorMessage);
}
export function createServerAuthError(target, options) {
    const sessionHint = options?.sessionName
        ? `Then recreate the session:\n  mcpc ${target} session ${options.sessionName}`
        : `Then run your command again.`;
    return new AuthError(`Authentication required by server.\n\n` +
        `To authenticate, run:\n` +
        `  mcpc ${target} login\n\n` +
        sessionHint, options?.originalError ? { originalError: options.originalError } : undefined);
}
export function isMcpError(error) {
    return error instanceof McpError;
}
export function isShutdownError(error) {
    if (!error)
        return false;
    if (error instanceof Error) {
        if (error.name === 'AbortError')
            return true;
        if (error.message.includes('AbortError'))
            return true;
        if (error.message.includes('aborted'))
            return true;
        if (error.message.includes('Not connected'))
            return true;
        if (error.message.includes('Failed to send an error response'))
            return true;
    }
    if (typeof error === 'object' && 'name' in error && error.name === 'AbortError') {
        return true;
    }
    return false;
}
export function toMcpError(error) {
    if (isMcpError(error)) {
        return error;
    }
    if (error instanceof Error) {
        return new ClientError(error.message, { originalError: error.name });
    }
    return new ClientError(String(error));
}
export function formatHumanError(error, verbose = false) {
    const mcpError = toMcpError(error);
    let output = `Error: ${mcpError.message}`;
    if (verbose && mcpError.details) {
        output += `\n\nDetails:\n${JSON.stringify(mcpError.details, null, 2)}`;
    }
    if (verbose && mcpError.stack) {
        output += `\n\nStack trace:\n${mcpError.stack}`;
    }
    return output;
}
//# sourceMappingURL=errors.js.map