Skip to content

Commit c3b9e5b

Browse files
committed
fix: compatible reportError on errors in event listeners
1 parent fe50574 commit c3b9e5b

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/partial/events.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
type LocaleChangeEvent,
1313
type LocaleLoadEvent,
1414
} from '../events/index.js'
15+
import { cReportError } from '../utils/compat.js'
1516
import type { IntlController } from './types.js'
1617

1718
export type ControllerEvents =
@@ -134,8 +135,7 @@ export function useEventTargetPartial<T>(controllerBox: {
134135
eventType === 'error' ||
135136
(eventRegister.get('error')?.size ?? 0) < 1
136137
) {
137-
// eslint-disable-next-line no-console
138-
console.error('Uncaught', err)
138+
cReportError(err)
139139
} else {
140140
const wrappedError = new Error(
141141
`An error occurred while calling the event listener for "${eventType}"`,

src/utils/compat.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
declare const reportError: ((err: any) => void) | undefined
2+
3+
declare const console:
4+
| {
5+
error(...args: any[]): void
6+
}
7+
| undefined
8+
9+
export function cReportError(err: any) {
10+
if (typeof reportError === 'function') {
11+
reportError(err)
12+
return
13+
}
14+
15+
if (typeof console === 'object' && typeof console.error === 'function') {
16+
console.error('Uncaught', err)
17+
return
18+
}
19+
20+
setTimeout(() => {
21+
throw err
22+
}, 0)
23+
}

0 commit comments

Comments
 (0)