Implement client-side error timing

This commit is contained in:
gpt-engineer-app[bot]
2025-11-05 04:20:55 +00:00
parent acfbf872d2
commit 66bdb36b03
3 changed files with 29 additions and 7 deletions

View File

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