-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.ts
More file actions
51 lines (48 loc) · 1.54 KB
/
Copy pathhandler.ts
File metadata and controls
51 lines (48 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { captureException } from '@sentry/browser'
import { ClientScriptError } from './ClientScriptError'
import { isProd } from '@components/scripts/utils/environmentClient'
type ScriptErrorExtraValue = string | number | boolean | null
export interface ScriptErrorContext {
scriptName: string
operation?: string
extra?: Record<string, ScriptErrorExtraValue>
}
/**
* Error boundary for non-fatal script execution exceptions
*
* Transforms any error into a ClientScriptError, logs in development,
* and reports to Sentry in production (via beforeSend filter).
*
* @param error - The caught error (any type)
* @param context - Script name and optional operation context
* @returns Transformed ClientScriptError instance
*
* @example
* ```typescript
* try {
* script.init()
* } catch (error) {
* handleScriptError(error, { scriptName: script.scriptName, operation: 'init' })
* }
* ```
*/
export function handleScriptError(error: unknown, context: ScriptErrorContext): ClientScriptError {
// Transform to ClientScriptError (normalizes message internally)
const clientError = new ClientScriptError(error)
if (isProd()) {
captureException(clientError, {
tags: {
scriptName: context.scriptName,
...(context.operation && { operation: context.operation }),
},
...(context.extra && { extra: context.extra }),
})
} else {
// Log it for debugging
console.error(
`[${context.scriptName}]${context.operation ? ` ${context.operation}` : ''}:`,
clientError
)
}
return clientError
}