ST5WLNWFS4AOVPOJHMREC4VGXQMIDRDLBZ2M4FCE5FPKLPQTUTRQC
ETAXJ5YIOUH75V6ZKHUGQWTRWIRZVJAUG5LYXYZEJDS7SXPNEZSAC
TGGXF43HKM7KPF4OS4ANJTSSS3LKMJKPXL7PLY5CZJGIFQW4CQYAC
TKCG7EZ3MYG2FFWYRLGQTQXIRDSNGICDHK6CE6OZJPTBK6CNYFJAC
OBXY6BHNROIV7W4O3MMQ6GTQXU2XUKF5A4DCJYDLUEUHV72E7BNAC
ONSQYCF6NFUEA24ORB62W4P62LKUMV7C5PLYRZQULHFDNROEY2HQC
AUEY3QXCUPYXL354YRB7AQBEOYX3GN5XGE47KX7TE6XT2QFP7G6AC
CF7JTOBNLOMGK36B5CXQP3FWA3RLBYAOCNGXNNLKCCO7TBWR65GQC
IUHUM6OZ5KYEAYQCIYNG5Q4QLQRAQNMBWKYGV2ZDJFNY5W4DOUNQC
/**
* Standardized error handling utilities for API routes
*/
export interface ErrorResponse {
error: string;
details: string;
}
/**
* Extracts a user-friendly error message from various error types
*/
export function getErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
if (typeof error === 'string') {
return error;
}
return 'An unexpected error occurred';
}
/**
* Creates a standardized error response with logging
*/
export function standardErrorResponse(error: unknown, context: string): ErrorResponse {
console.error(`${context}:`, error);
return {
error: getErrorMessage(error),
details: error instanceof Error ? error.message : String(error)
};
}
/**
* Handles database constraint violations (e.g., duplicate keys)
*/
export function handleDatabaseError(error: unknown, context: string): ErrorResponse {
console.error(`Database error in ${context}:`, error);
const errorMessage = getErrorMessage(error);
// Handle common database constraint violations
if (errorMessage.includes('duplicate key') || errorMessage.includes('unique constraint')) {
return {
error: 'A record with these details already exists',
details: 'Duplicate entry'
};
}
if (errorMessage.includes('foreign key constraint')) {
return {
error: 'Referenced record does not exist',
details: 'Foreign key violation'
};
}
if (errorMessage.includes('not null constraint')) {
return {
error: 'Required field is missing',
details: 'Missing required data'
};
}
// Default database error response
return {
error: 'Database operation failed',
details: errorMessage
};
}
/**
* Handles authentication and authorization errors
*/
export function handleAuthError(context: string): ErrorResponse {
console.error(`Authentication failed in ${context}`);
return {
error: 'Authentication required',
details: 'Please log in to access this resource'
};
}
/**
* Handles validation errors
*/
export function handleValidationError(message: string, context: string): ErrorResponse {
console.error(`Validation error in ${context}: ${message}`);
return {
error: 'Invalid request data',
details: message
};
}
/**
* Handles permission errors
*/
export function handlePermissionError(action: string, context: string): ErrorResponse {
console.error(`Permission denied for ${action} in ${context}`);
return {
error: 'Insufficient permissions',
details: `You don't have permission to ${action}`
};
}
if (error instanceof Error) {
if (error.message.includes('File not found')) {
return c.json({
error: "File not found in storage",
details: error.message
}, 404);
}
if (error.message.includes('authentication failed')) {
return c.json({
error: "Storage authentication failed",
details: error.message
}, 500);
}
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes('File not found')) {
return c.json(standardErrorResponse(error, "file not found in storage"), 404);
}
if (errorMessage.includes('authentication failed')) {
return c.json(handleAuthError("storage authentication"), 500);
console.error("Error fetching file selections:", error);
return c.json({
error: "Failed to fetch file selections",
details: error instanceof Error ? error.message : String(error),
}, 500);
return c.json(standardErrorResponse(error, "fetching file selections"), 500);
console.error("Error fetching file context:", error);
return c.json({
error: "Failed to fetch file context",
details: error instanceof Error ? error.message : String(error),
}, 500);
return c.json(standardErrorResponse(error, "fetching file context"), 500);
console.error("Error creating dataset:", error);
// Handle unique constraint violations
if (error instanceof Error && error.message.includes('duplicate key')) {
return c.json({
error: "A dataset with this ID already exists"
}, 400);
// Handle database constraint violations
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes('duplicate key')) {
return c.json(handleDatabaseError(error, "creating dataset"), 400);
console.error("Error creating cluster:", error);
// Handle unique constraint violations
if (error instanceof Error && error.message.includes('duplicate key')) {
return c.json({
error: "A cluster with this ID already exists"
}, 400);
// Handle database constraint violations
const errorMessage = error instanceof Error ? error.message : String(error);
if (errorMessage.includes('duplicate key')) {
return c.json(handleDatabaseError(error, "creating cluster"), 400);