// Error handling utilities
export class DriveError extends Error {
constructor(
message: string,
public code: string,
public details?: unknown
) {
super(message);
this.name = 'DriveError';
}
}
export class AuthenticationError extends DriveError {
constructor(message: string, details?: unknown) {
super(message, 'AUTH_ERROR', details);
this.name = 'AuthenticationError';
}
}
export class NotFoundError extends DriveError {
constructor(resource: string, id: string) {
super(`${resource} not found: ${id}`, 'NOT_FOUND', { resource, id });
this.name = 'NotFoundError';
}
}
export class PermissionError extends DriveError {
constructor(message: string, details?: unknown) {
super(message, 'PERMISSION_DENIED', details);
this.name = 'PermissionError';
}
}
export class ValidationError extends DriveError {
constructor(message: string, details?: unknown) {
super(message, 'VALIDATION_ERROR', details);
this.name = 'ValidationError';
}
}
/**
* Format error for MCP response
*/
export function formatError(error: unknown): { error: string; code?: string } {
if (error instanceof DriveError) {
return {
error: error.message,
code: error.code,
};
}
if (error instanceof Error) {
return { error: error.message };
}
return { error: String(error) };
}
/**
* Wrap async operations with consistent error handling
*/
export async function withErrorHandling<T>(
operation: () => Promise<T>,
context: string
): Promise<T> {
try {
return await operation();
} catch (error: unknown) {
if (error instanceof DriveError) {
throw error;
}
const message = error instanceof Error ? error.message : String(error);
throw new DriveError(`${context}: ${message}`, 'OPERATION_FAILED', error);
}
}