mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 12:51:14 -05:00
Refactor: Complete error handling overhaul
This commit is contained in:
@@ -24,8 +24,13 @@ export async function invokeWithTracking<T = any>(
|
||||
payload: any = {},
|
||||
userId?: string,
|
||||
parentRequestId?: string,
|
||||
traceId?: string
|
||||
traceId?: string,
|
||||
timeout: number = 30000 // Default 30s timeout
|
||||
): Promise<{ data: T | null; error: any; requestId: string; duration: number }> {
|
||||
// Create AbortController for timeout
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
||||
|
||||
try {
|
||||
const { result, requestId, duration } = await trackRequest(
|
||||
{
|
||||
@@ -39,6 +44,7 @@ export async function invokeWithTracking<T = any>(
|
||||
// Include client request ID in payload for correlation
|
||||
const { data, error } = await supabase.functions.invoke<T>(functionName, {
|
||||
body: { ...payload, clientRequestId: context.requestId },
|
||||
signal: controller.signal, // Add abort signal for timeout
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
@@ -46,10 +52,25 @@ export async function invokeWithTracking<T = any>(
|
||||
}
|
||||
);
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
return { data: result, error: null, requestId, duration };
|
||||
} catch (error: unknown) {
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
// Handle AbortError specifically
|
||||
if (error instanceof Error && error.name === 'AbortError') {
|
||||
return {
|
||||
data: null,
|
||||
error: {
|
||||
message: `Request timeout: ${functionName} took longer than ${timeout}ms to respond`,
|
||||
code: 'TIMEOUT',
|
||||
},
|
||||
requestId: 'timeout',
|
||||
duration: timeout,
|
||||
};
|
||||
}
|
||||
|
||||
const errorMessage = getErrorMessage(error);
|
||||
// On error, we don't have tracking info, so create basic response
|
||||
return {
|
||||
data: null,
|
||||
error: { message: errorMessage },
|
||||
|
||||
Reference in New Issue
Block a user