mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 09:31:13 -05:00
Fix error logging issues
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
import { toast } from 'sonner';
|
||||
import { logger } from './logger';
|
||||
import { supabase } from '@/integrations/supabase/client';
|
||||
import { breadcrumbManager } from './errorBreadcrumbs';
|
||||
import { captureEnvironmentContext } from './environmentContext';
|
||||
|
||||
export type ErrorContext = {
|
||||
action: string;
|
||||
@@ -21,9 +24,10 @@ export class AppError extends Error {
|
||||
export const handleError = (
|
||||
error: unknown,
|
||||
context: ErrorContext
|
||||
): string => { // Now returns error ID
|
||||
const errorId = context.metadata?.requestId as string | undefined;
|
||||
const shortErrorId = errorId ? errorId.slice(0, 8) : undefined;
|
||||
): string => {
|
||||
// Generate or use existing error ID
|
||||
const errorId = (context.metadata?.requestId as string) || crypto.randomUUID();
|
||||
const shortErrorId = errorId.slice(0, 8);
|
||||
|
||||
const errorMessage = error instanceof AppError
|
||||
? error.userMessage || error.message
|
||||
@@ -39,15 +43,41 @@ export const handleError = (
|
||||
errorId,
|
||||
});
|
||||
|
||||
// Log to database with breadcrumbs (non-blocking)
|
||||
try {
|
||||
const envContext = captureEnvironmentContext();
|
||||
const breadcrumbs = breadcrumbManager.getAll();
|
||||
|
||||
// Fire-and-forget database logging
|
||||
supabase.rpc('log_request_metadata', {
|
||||
p_request_id: errorId,
|
||||
p_user_id: context.userId || undefined,
|
||||
p_endpoint: context.action,
|
||||
p_method: 'ERROR',
|
||||
p_status_code: 500,
|
||||
p_error_type: error instanceof Error ? error.name : 'UnknownError',
|
||||
p_error_message: errorMessage,
|
||||
p_error_stack: error instanceof Error ? error.stack : undefined,
|
||||
p_user_agent: navigator.userAgent,
|
||||
p_breadcrumbs: JSON.stringify(breadcrumbs),
|
||||
p_timezone: envContext.timezone,
|
||||
p_referrer: document.referrer || undefined,
|
||||
}).then(({ error: dbError }) => {
|
||||
if (dbError) {
|
||||
logger.error('Failed to log error to database', { dbError });
|
||||
}
|
||||
});
|
||||
} catch (logError) {
|
||||
logger.error('Failed to capture error context', { logError });
|
||||
}
|
||||
|
||||
// Show user-friendly toast with error ID
|
||||
toast.error(context.action, {
|
||||
description: shortErrorId
|
||||
? `${errorMessage}\n\nReference ID: ${shortErrorId}`
|
||||
: errorMessage,
|
||||
description: `${errorMessage}\n\nReference ID: ${shortErrorId}`,
|
||||
duration: 5000,
|
||||
});
|
||||
|
||||
return errorId || 'unknown';
|
||||
return errorId;
|
||||
};
|
||||
|
||||
export const handleSuccess = (
|
||||
|
||||
Reference in New Issue
Block a user