deepsource-mcp-server
Version:
Model Context Protocol server for DeepSource
144 lines • 4.22 kB
JavaScript
/**
* @fileoverview Factory functions for creating common MCP errors
*/
import { MCPError, MCPErrorCode, MCPErrorCategory } from './mcp-errors.js';
/**
* Creates an authentication error
*/
export function createAuthenticationError(message = 'Authentication failed', details) {
const errorInfo = {
code: MCPErrorCode.AUTHENTICATION_ERROR,
category: MCPErrorCategory.AUTHENTICATION_ERROR,
message,
retryable: false,
userFriendly: true,
};
if (details !== undefined) {
errorInfo.details = details;
}
return new MCPError(errorInfo);
}
/**
* Creates a resource not found error
*/
export function createResourceNotFoundError(resource, details) {
const errorInfo = {
code: MCPErrorCode.RESOURCE_NOT_FOUND,
category: MCPErrorCategory.RESOURCE_ERROR,
message: `Resource not found: ${resource}`,
details: { resource, ...(details || {}) },
retryable: false,
userFriendly: true,
};
return new MCPError(errorInfo);
}
/**
* Creates a validation error
*/
export function createValidationError(message, details) {
const errorInfo = {
code: MCPErrorCode.VALIDATION_ERROR,
category: MCPErrorCategory.VALIDATION_ERROR,
message: `Validation failed: ${message}`,
retryable: false,
userFriendly: true,
};
if (details !== undefined) {
errorInfo.details = details;
}
return new MCPError(errorInfo);
}
/**
* Creates a server error
*/
export function createServerError(message = 'Internal server error', cause, details) {
const errorInfo = {
code: MCPErrorCode.INTERNAL_ERROR,
category: MCPErrorCategory.SERVER_ERROR,
message,
retryable: true,
userFriendly: false,
};
if (cause !== undefined) {
errorInfo.cause = cause;
}
if (details !== undefined) {
errorInfo.details = details;
}
return new MCPError(errorInfo);
}
/**
* Creates a timeout error
*/
export function createTimeoutError(operation, timeoutMs) {
const errorInfo = {
code: MCPErrorCode.TIMEOUT_ERROR,
category: MCPErrorCategory.NETWORK_ERROR,
message: `Operation timed out: ${operation}`,
details: { operation, timeoutMs },
retryable: true,
userFriendly: true,
};
return new MCPError(errorInfo);
}
/**
* Creates a rate limiting error
*/
export function createRateLimitedError(resetTime) {
const errorInfo = {
code: MCPErrorCode.RATE_LIMITED,
category: MCPErrorCategory.CLIENT_ERROR,
message: 'Rate limit exceeded',
retryable: true,
userFriendly: true,
};
if (resetTime) {
errorInfo.details = { resetTime: resetTime.toISOString() };
}
return new MCPError(errorInfo);
}
/**
* Creates a configuration error
*/
export function createConfigurationError(message, details) {
const errorInfo = {
code: MCPErrorCode.CONFIGURATION_ERROR,
category: MCPErrorCategory.SERVER_ERROR,
message: `Configuration error: ${message}`,
retryable: false,
userFriendly: false,
};
if (details !== undefined) {
errorInfo.details = details;
}
return new MCPError(errorInfo);
}
/**
* Creates a dependency error
*/
export function createDependencyError(service, cause) {
const errorInfo = {
code: MCPErrorCode.DEPENDENCY_ERROR,
category: MCPErrorCategory.SERVER_ERROR,
message: `Dependency error: ${service} is unavailable`,
details: { service },
retryable: true,
userFriendly: false,
};
if (cause !== undefined) {
errorInfo.cause = cause;
}
return new MCPError(errorInfo);
}
// For backward compatibility, export a namespace-like object
export const MCPErrorFactory = {
authentication: createAuthenticationError,
resourceNotFound: createResourceNotFoundError,
validation: createValidationError,
server: createServerError,
timeout: createTimeoutError,
rateLimited: createRateLimitedError,
configuration: createConfigurationError,
dependency: createDependencyError,
};
//# sourceMappingURL=mcp-error-factory.js.map