I'm using an AbortController
and catching the error it throws when it aborts. My debugger says that the error is DOMException: Aborted
. I want to capture that in an IF statement, but I'm not sure how. I tried if (e == "DOMException: Aborted")
and if (e == "Aborted")
but they don't capture it.
My code:
controller = new AbortController();
const signal = controller.signal;
fetch(url, { signal })
.then(function(response) {
console.log('Download complete', response);
})
.catch(function(e) {
console.log('Download error: ' + e.message);
});
then I abort this controller with controller.abort()
It's a DOMException
instance, an object not a string. The proper condition to test for it is
.catch(function(e) {
if (e instanceof DOMException && e.name == "AbortError") {
// …
} else {
console.log('Download error: ' + e.message);
}
});
You can also check whether e.code == DOMException.ABORT_ERR
.