Skip to content

Commit 9af97ca

Browse files
authored
fix: use a normal Error when DOMException isn't available (#5161)
* use a normal Error when DOMException isn't available * fix instanceof check * sometimes DOMExceptions aren't Errors, apparently * account for undefined DOMException
1 parent 3615d1a commit 9af97ca

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/toolkit/src/query/fetchBaseQuery.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,10 @@ export function fetchBaseQuery({
305305
return {
306306
error: {
307307
status:
308-
e instanceof DOMException && e.name === 'TimeoutError'
308+
(e instanceof Error ||
309+
(typeof DOMException !== 'undefined' &&
310+
e instanceof DOMException)) &&
311+
e.name === 'TimeoutError'
309312
? 'TIMEOUT_ERROR'
310313
: 'FETCH_ERROR',
311314
error: String(e),

packages/toolkit/src/query/utils/signals.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
// AbortSignal.timeout() is currently baseline 2024
22
export const timeoutSignal = (milliseconds: number) => {
33
const abortController = new AbortController()
4-
setTimeout(
5-
() => abortController.abort(new DOMException('', 'TimeoutError')),
6-
milliseconds,
7-
)
4+
setTimeout(() => {
5+
const message = 'signal timed out'
6+
const name = 'TimeoutError'
7+
abortController.abort(
8+
// some environments (React Native, Node) don't have DOMException
9+
typeof DOMException !== 'undefined'
10+
? new DOMException(message, name)
11+
: Object.assign(new Error(message), { name }),
12+
)
13+
}, milliseconds)
814
return abortController.signal
915
}
1016

0 commit comments

Comments
 (0)