-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(redux-requests-fetch): fix handling of request abortion with nati…
…ve `AbortController` Add `isNativeAbortError` guard function & tests Rethrow `'REQUEST_ABORTED'` when a native `AbortError` is emitted
- Loading branch information
1 parent
455a95a
commit 7002613
Showing
3 changed files
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// a real browser will throw an instance of `DomException`, polyfills an instance of `AbortError` | ||
// https://developer.mozilla.org/en-US/docs/Web/API/AbortController#examples | ||
// old browsers would set the exception `code` property equal to DOMException.ABORT_ERR | ||
// https://developer.mozilla.org/en-US/docs/Web/API/DOMException/code | ||
export const isNativeAbortError = err => | ||
['DOMException', 'AbortError'].includes(err.constructor.name) && | ||
(err.name === 'AbortError' || err.code === DOMException.ABORT_ERR); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { isNativeAbortError } from './helpers'; | ||
|
||
describe('helpers', () => { | ||
describe('isNativeAbortError', () => { | ||
|
||
it('properly identifies a native `AbortError`', () => { | ||
expect(isNativeAbortError(new DOMException('request aborted', 'AbortError'))).toBe(true); | ||
}); | ||
|
||
it('does not identify another Error', () => { | ||
expect(isNativeAbortError(new Error())).toBe(false); | ||
}); | ||
|
||
it('does not identify a string', () => { | ||
expect(isNativeAbortError('REQUEST_ABORTED')).toBe(false); | ||
}); | ||
}); | ||
}); |