deepsource-mcp-server
Version:
Model Context Protocol server for DeepSource
117 lines • 4.3 kB
JavaScript
/**
* @fileoverview Error utilities for handling API errors
* This module provides utilities for classifying and handling errors.
*/
/**
* Possible error categories for API errors
* @enum {string}
*/
// This enum is part of the public API and is used by consumers, even if not all values are used in this file
export var ErrorCategory;
(function (ErrorCategory) {
/** Error related to authentication or authorization */
ErrorCategory["AUTH"] = "AUTH";
/** Error with network connectivity */
ErrorCategory["NETWORK"] = "NETWORK";
/** Error with server processing */
ErrorCategory["SERVER"] = "SERVER";
/** Error with client input */
ErrorCategory["CLIENT"] = "CLIENT";
/** Error with request timing out */
ErrorCategory["TIMEOUT"] = "TIMEOUT";
/** Error with rate limiting */
ErrorCategory["RATE_LIMIT"] = "RATE_LIMIT";
/** Error with the GraphQL schema */
ErrorCategory["SCHEMA"] = "SCHEMA";
/** Error with data not found */
ErrorCategory["NOT_FOUND"] = "NOT_FOUND";
/** Error with data formatting */
ErrorCategory["FORMAT"] = "FORMAT";
/** Other uncategorized errors */
ErrorCategory["OTHER"] = "OTHER";
})(ErrorCategory || (ErrorCategory = {}));
/**
* Create a new classified error with additional metadata
* @param message The error message
* @param category The error category
* @param originalError The original error that caused this error, useful for debugging and error tracing
* @param metadata Additional contextual information about the error as key-value pairs
* @returns A new classified error with the specified properties
* @example
* const error = createClassifiedError(
* 'Failed to fetch project data',
* ErrorCategory.NETWORK,
* originalError,
* { projectId: '123', endpoint: '/api/projects' }
* );
*/
export function createClassifiedError(message, category, originalError, metadata) {
const error = new Error(message);
error.category = category;
error.originalError = originalError;
if (metadata !== undefined) {
error.metadata = metadata;
}
return error;
}
/**
* Check if an error is a classified error
* @param error The error to check
* @returns True if the error is a classified error
*/
export function isClassifiedError(error) {
return Boolean(error &&
typeof error === 'object' &&
'message' in error &&
'category' in error &&
typeof error.category === 'string');
}
/**
* Error classifier for GraphQL errors
* @param error The error to classify
* @returns The classified error category
*/
export function classifyGraphQLError(error) {
const message = error.message.toLowerCase();
// Define error patterns and their corresponding categories
const errorPatterns = {
[ErrorCategory.AUTH]: [
'authentication',
'unauthorized',
'access denied',
'not authorized',
'forbidden',
'token',
'api key',
],
[ErrorCategory.RATE_LIMIT]: ['rate limit', 'too many requests', 'throttled'],
[ErrorCategory.NETWORK]: ['network', 'connection', 'econnreset', 'econnrefused'],
[ErrorCategory.TIMEOUT]: ['timeout', 'timed out', 'etimedout'],
[ErrorCategory.SCHEMA]: [
'cannot query field',
'unknown argument',
'unknown type',
'field not defined',
],
[ErrorCategory.NOT_FOUND]: ['not found', 'nonetype', 'does not exist'],
[ErrorCategory.SERVER]: ['server error', 'internal error', '500'],
[ErrorCategory.CLIENT]: [],
[ErrorCategory.FORMAT]: [],
[ErrorCategory.OTHER]: [],
};
// Check each category's patterns
for (const [category, patterns] of Object.entries(errorPatterns)) {
if (patterns.some((pattern) => message.includes(pattern))) {
return category;
}
}
// Default to OTHER
return ErrorCategory.OTHER;
}
// This code helps document the intended use of the enum values
// Each category has a specific use case in the error handling flow
// For example:
// - AUTH errors are for authentication-related failures
// - NETWORK errors happen when network connectivity is lost
// - etc.
//# sourceMappingURL=errors.js.map