Category: error Since: 1.1.0 Tags: error, abort, dom-exception, type-guard, cancellation
isAbortError
Checks whether a value is a DOMException with name === "AbortError".
Usage
import { isAbortError } from "@petr-ptacek/js-core";
try {
await fetch("/api/data", { signal });
} catch (error) {
if (isAbortError(error)) {
return; // request was cancelled — ignore
}
throw error;
}Why This Utility Exists
AbortError is not a dedicated class — it is a DOMException instance with name === "AbortError". Checking this inline is repetitive and error-prone. This utility provides a reusable, readable predicate that also acts as a TypeScript type guard.
Signature
function isAbortError(error: unknown): error is DOMException;Parameters
error(unknown): The value to test.
Return Type
Returns true if error is a DOMException with name === "AbortError", otherwise false.
When true, TypeScript narrows error to DOMException.
Design Notes
AbortError is thrown by browser APIs that support cancellation via AbortSignal — most notably fetch. The check requires both an instanceof DOMException guard and a name comparison, since DOMException uses the name property to differentiate error subtypes rather than separate subclasses.
When To Use
Use isAbortError when you need to:
- distinguish abort errors from other errors in a
catchblock - silently ignore aborted requests in UI components
- handle cancellation cleanly without inline
instanceofchecks
When Not To Use
Avoid when:
- you want to handle all errors uniformly without branching
- you need to detect timeouts or other
DOMExceptionsubtypes (checkerror.namedirectly)
Summary
isAbortError is a minimal type guard for identifying AbortError instances, eliminating repetitive inline checks in async error handling.
Snippets
basic.ts
import { isAbortError } from "@petr-ptacek/js-core";
// Basic check
console.log(isAbortError(new DOMException("Aborted", "AbortError"))); // true
console.log(isAbortError(new Error("Aborted"))); // false
console.log(isAbortError(null)); // false
// Type guard usage in a try/catch block
const controller = new AbortController();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
async function _fetchWithAbort(url: string) {
try {
const response = await fetch(url, { signal: controller.signal });
return response.json();
} catch (error) {
if (isAbortError(error)) {
// error is narrowed to DOMException
console.log("Request was aborted:", error.message);
return null;
}
throw error;
}
}