mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 11:11:12 -05:00
Implement client-side error timing
This commit is contained in:
@@ -8,6 +8,7 @@ export type ErrorContext = {
|
||||
action: string;
|
||||
userId?: string;
|
||||
metadata?: Record<string, unknown>;
|
||||
duration?: number; // Optional: milliseconds the operation took
|
||||
};
|
||||
|
||||
export class AppError extends Error {
|
||||
@@ -78,6 +79,7 @@ export const handleError = (
|
||||
p_breadcrumbs: JSON.stringify(breadcrumbs),
|
||||
p_timezone: envContext.timezone,
|
||||
p_referrer: document.referrer || undefined,
|
||||
p_duration_ms: context.duration,
|
||||
}).then(({ error: dbError }) => {
|
||||
if (dbError) {
|
||||
logger.error('Failed to log error to database', { dbError });
|
||||
@@ -161,6 +163,7 @@ export const handleNonCriticalError = (
|
||||
p_breadcrumbs: JSON.stringify(breadcrumbs),
|
||||
p_timezone: envContext.timezone,
|
||||
p_referrer: document.referrer || undefined,
|
||||
p_duration_ms: context.duration,
|
||||
}).then(({ error: dbError }) => {
|
||||
if (dbError) {
|
||||
logger.error('Failed to log non-critical error to database', { dbError });
|
||||
@@ -202,3 +205,21 @@ export function hasErrorCode(error: unknown): error is { code: string } {
|
||||
typeof (error as { code: unknown }).code === 'string'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper to wrap async operations with automatic duration tracking
|
||||
* Use this for operations where you want to track how long they took before failing
|
||||
*/
|
||||
export async function withErrorTiming<T>(
|
||||
fn: () => Promise<T>,
|
||||
errorContext: Omit<ErrorContext, 'duration'>
|
||||
): Promise<T> {
|
||||
const start = performance.now();
|
||||
try {
|
||||
return await fn();
|
||||
} catch (error) {
|
||||
const duration = Math.round(performance.now() - start);
|
||||
handleError(error, { ...errorContext, duration });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user