|
| 1 | +import { captureException } from "@sentry/browser" |
| 2 | + |
| 3 | +export function setupGlobalErrorHandlers() { |
| 4 | + // Handle resource loading failures (scripts, images, stylesheets, etc.) |
| 5 | + window.addEventListener( |
| 6 | + "error", |
| 7 | + event => { |
| 8 | + const target = event.target as |
| 9 | + | (HTMLElement & { src?: string; href?: string }) |
| 10 | + | null |
| 11 | + |
| 12 | + // Only track resource loading errors, not JavaScript runtime errors |
| 13 | + if (target && target !== (window as any)) { |
| 14 | + const resourceUrl = target.src || target.href || "unknown" |
| 15 | + const resourceType = target.tagName?.toLowerCase() || "unknown" |
| 16 | + |
| 17 | + captureException( |
| 18 | + new Error(`Resource load failed: ${event.message || "Load failed"}`), |
| 19 | + { |
| 20 | + tags: { |
| 21 | + source: "resource_load_failure", |
| 22 | + resource_type: resourceType, |
| 23 | + }, |
| 24 | + extra: { |
| 25 | + resourceUrl, |
| 26 | + resourceType, |
| 27 | + errorMessage: event.message, |
| 28 | + filename: event.filename, |
| 29 | + lineno: event.lineno, |
| 30 | + colno: event.colno, |
| 31 | + targetElement: target.outerHTML, |
| 32 | + }, |
| 33 | + }, |
| 34 | + ) |
| 35 | + } |
| 36 | + }, |
| 37 | + // Use capture phase to catch all resource errors |
| 38 | + true, |
| 39 | + ) |
| 40 | + |
| 41 | + // Handle unhandled promise rejections (network failures, etc.) |
| 42 | + window.addEventListener("unhandledrejection", event => { |
| 43 | + const reason = event.reason |
| 44 | + |
| 45 | + // Check if it's a network-related error |
| 46 | + if ( |
| 47 | + reason instanceof Error && |
| 48 | + (reason.message.includes("Load failed") || |
| 49 | + reason.message.includes("Failed to fetch") || |
| 50 | + reason.message.includes("Network request failed") || |
| 51 | + reason.name === "NetworkError") |
| 52 | + ) { |
| 53 | + captureException(reason, { |
| 54 | + tags: { |
| 55 | + source: "network_failure", |
| 56 | + error_type: "unhandled_rejection", |
| 57 | + }, |
| 58 | + extra: { |
| 59 | + promiseRejectionReason: reason.message, |
| 60 | + stack: reason.stack, |
| 61 | + }, |
| 62 | + }) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + // TODO: Consider adding additional error tracking in follow-up PRs: |
| 67 | + // - Fetch tracking (safer alternatives: interceptor library, wrapper function, Service Worker) |
| 68 | + // - CSP violations: addEventListener("securitypolicyviolation") for blocked resources |
| 69 | + // - Network connectivity: addEventListener("offline/online") for connection issues |
| 70 | + // - Chunk loading failures: track dynamic import() errors for code splitting |
| 71 | + // - Service Worker errors: track SW registration/update failures |
| 72 | + // - WebSocket connection failures: track real-time connection issues |
| 73 | + // - Media loading errors: track video/audio loading failures if relevant |
| 74 | +} |
0 commit comments