mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-24 13:31:13 -05:00
feat: Implement comprehensive error logging
This commit is contained in:
@@ -100,6 +100,64 @@ export const handleInfo = (
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Handle non-critical errors (background failures) that should be logged
|
||||
* to the database WITHOUT showing user toasts
|
||||
* Use this for fire-and-forget operations where user shouldn't be interrupted
|
||||
*/
|
||||
export const handleNonCriticalError = (
|
||||
error: unknown,
|
||||
context: ErrorContext
|
||||
): string => {
|
||||
const errorId = crypto.randomUUID();
|
||||
const shortErrorId = errorId.slice(0, 8);
|
||||
|
||||
const errorMessage = error instanceof AppError
|
||||
? error.userMessage || error.message
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: 'An unexpected error occurred';
|
||||
|
||||
// Log to console/monitoring (same as handleError)
|
||||
logger.error('Non-critical error occurred', {
|
||||
...context,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
stack: error instanceof Error ? error.stack : undefined,
|
||||
errorId,
|
||||
severity: 'low',
|
||||
});
|
||||
|
||||
// Log to database with breadcrumbs (non-blocking, fire-and-forget)
|
||||
try {
|
||||
const envContext = captureEnvironmentContext();
|
||||
const breadcrumbs = breadcrumbManager.getAll();
|
||||
|
||||
supabase.rpc('log_request_metadata', {
|
||||
p_request_id: errorId,
|
||||
p_user_id: context.userId || undefined,
|
||||
p_endpoint: context.action,
|
||||
p_method: 'NON_CRITICAL_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 non-critical error to database', { dbError });
|
||||
}
|
||||
});
|
||||
} catch (logError) {
|
||||
logger.error('Failed to capture non-critical error context', { logError });
|
||||
}
|
||||
|
||||
// NO TOAST - This is the key difference from handleError()
|
||||
return errorId;
|
||||
};
|
||||
|
||||
/**
|
||||
* Type-safe error message extraction utility
|
||||
* Use this instead of `error: any` in catch blocks
|
||||
|
||||
@@ -7,6 +7,7 @@ import { supabase } from '@/integrations/supabase/client';
|
||||
import { requestContext, type RequestContext } from './requestContext';
|
||||
import { breadcrumbManager } from './errorBreadcrumbs';
|
||||
import { captureEnvironmentContext } from './environmentContext';
|
||||
import { handleNonCriticalError } from './errorHandler';
|
||||
import { logger } from './logger';
|
||||
|
||||
export interface RequestTrackingOptions {
|
||||
@@ -55,7 +56,16 @@ export async function trackRequest<T>(
|
||||
parentRequestId: options.parentRequestId,
|
||||
traceId: context.traceId,
|
||||
}).catch(err => {
|
||||
logger.error('Failed to log request metadata', { error: err, context: 'RequestTracking' });
|
||||
handleNonCriticalError(err, {
|
||||
action: 'Log request metadata (success)',
|
||||
userId: options.userId,
|
||||
metadata: {
|
||||
endpoint: options.endpoint,
|
||||
method: options.method,
|
||||
statusCode: 200,
|
||||
requestId: context.requestId
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Cleanup context
|
||||
@@ -96,7 +106,17 @@ export async function trackRequest<T>(
|
||||
timezone: envContext.timezone,
|
||||
referrer: typeof document !== 'undefined' ? document.referrer : undefined,
|
||||
}).catch(err => {
|
||||
logger.error('Failed to log error metadata', { error: err, context: 'RequestTracking' });
|
||||
handleNonCriticalError(err, {
|
||||
action: 'Log request metadata (error)',
|
||||
userId: options.userId,
|
||||
metadata: {
|
||||
endpoint: options.endpoint,
|
||||
method: options.method,
|
||||
statusCode: 500,
|
||||
requestId: context.requestId,
|
||||
errorType: errorInfo.type
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Cleanup context
|
||||
|
||||
Reference in New Issue
Block a user