|
| 1 | +/** |
| 2 | + * Helpers for filtering global error events so we don't suppress site errors. |
| 3 | + * |
| 4 | + * The content script can observe `window` errors/rejections originating from the page. |
| 5 | + * Calling `preventDefault()` on these events suppresses the browser's default reporting, |
| 6 | + * so we must only suppress errors that are clearly caused by LightSession itself. |
| 7 | + */ |
| 8 | + |
| 9 | +export function isLightSessionRejection( |
| 10 | + reason: unknown, |
| 11 | + extensionUrlPrefix?: string, |
| 12 | +): boolean { |
| 13 | + const parts: string[] = []; |
| 14 | + |
| 15 | + if (typeof reason === 'string') { |
| 16 | + parts.push(reason); |
| 17 | + } else if (reason instanceof Error) { |
| 18 | + if (typeof reason.message === 'string') parts.push(reason.message); |
| 19 | + if (typeof reason.stack === 'string') parts.push(reason.stack); |
| 20 | + if (typeof reason.name === 'string') parts.push(reason.name); |
| 21 | + } else if (typeof reason === 'object' && reason !== null) { |
| 22 | + const r = reason as Record<string, unknown>; |
| 23 | + if (typeof r.message === 'string') parts.push(r.message); |
| 24 | + if (typeof r.stack === 'string') parts.push(r.stack); |
| 25 | + if (typeof r.name === 'string') parts.push(r.name); |
| 26 | + // Some browsers use different keys on Error-like objects. |
| 27 | + if (typeof r.filename === 'string') parts.push(r.filename); |
| 28 | + if (typeof r.fileName === 'string') parts.push(r.fileName); |
| 29 | + } |
| 30 | + |
| 31 | + if (parts.length === 0) return false; |
| 32 | + |
| 33 | + const haystack = parts.join('\n'); |
| 34 | + |
| 35 | + // The most reliable signal: our own extension base URL. |
| 36 | + // If we have it, prefer it exclusively to avoid suppressing unrelated site errors. |
| 37 | + if (extensionUrlPrefix) { |
| 38 | + return haystack.includes(extensionUrlPrefix); |
| 39 | + } |
| 40 | + |
| 41 | + // Fallback heuristics (only used if runtime URL isn't available). |
| 42 | + // Our logger prefix sometimes appears in thrown messages. |
| 43 | + if (haystack.includes('LS:')) return true; |
| 44 | + |
| 45 | + // Useful in dev builds / source maps. |
| 46 | + if (haystack.includes('light-session')) return true; |
| 47 | + |
| 48 | + return false; |
| 49 | +} |
0 commit comments