Refactor: Complete error handling overhaul

This commit is contained in:
gpt-engineer-app[bot]
2025-11-02 23:19:46 +00:00
parent d057ddc8cc
commit 35c7c3e957
7 changed files with 303 additions and 26 deletions

View File

@@ -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 },