Skip to content

Commit ab30c5c

Browse files
committed
Throw custom AbortError when aborting a fetch
1 parent 6beb2e2 commit ab30c5c

5 files changed

Lines changed: 18 additions & 27 deletions

File tree

packages/app/src/ErrorFallback.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type FallbackProps } from 'react-error-boundary';
22

33
import styles from './App.module.css';
4-
import { CANCELLED_BY_USER } from './providers/utils';
4+
import { AbortError, CANCELLED_BY_USER } from './providers/utils';
55

66
interface Props extends FallbackProps {
77
className?: string;
@@ -11,7 +11,7 @@ interface Props extends FallbackProps {
1111
function ErrorFallback(props: Props) {
1212
const { className = '', error, resetErrorBoundary } = props;
1313

14-
if (error instanceof Error && error.message === CANCELLED_BY_USER) {
14+
if (error instanceof AbortError && error.message === CANCELLED_BY_USER) {
1515
return (
1616
<p className={`${styles.error} ${className}`}>
1717
Request cancelled

packages/app/src/providers/h5grove/h5grove-api.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import axios, {
2424

2525
import { DataProviderApi } from '../api';
2626
import { type ValuesStoreParams } from '../models';
27-
import { createAxiosProgressHandler } from '../utils';
27+
import { AbortError, createAxiosProgressHandler } from '../utils';
2828
import {
2929
type H5GroveAttrValuesResponse,
3030
type H5GroveDataResponse,
@@ -92,14 +92,7 @@ export class H5GroveApi extends DataProviderApi {
9292
return await this.fetchData(params, abortSignal, onProgress);
9393
} catch (error) {
9494
if (error instanceof AxiosError && axios.isCancel(error)) {
95-
// Throw abort reason instead of axios `CancelError`
96-
// https://github.com/axios/axios/issues/5758
97-
throw new Error(
98-
typeof abortSignal?.reason === 'string'
99-
? abortSignal.reason
100-
: 'cancelled',
101-
{ cause: error },
102-
);
95+
throw new AbortError(abortSignal, error);
10396
}
10497

10598
throw error;

packages/app/src/providers/hsds/hsds-api.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import axios, { AxiosError, type AxiosInstance } from 'axios';
2626

2727
import { DataProviderApi } from '../api';
2828
import { type ValuesStoreParams } from '../models';
29-
import { createAxiosProgressHandler } from '../utils';
29+
import { AbortError, createAxiosProgressHandler } from '../utils';
3030
import {
3131
type BaseHsdsEntity,
3232
type HsdsAttribute,
@@ -270,14 +270,7 @@ export class HsdsApi extends DataProviderApi {
270270
return data.value;
271271
} catch (error) {
272272
if (error instanceof AxiosError && axios.isCancel(error)) {
273-
// Throw abort reason instead of axios `CancelError`
274-
// https://github.com/axios/axios/issues/5758
275-
throw new Error(
276-
typeof abortSignal?.reason === 'string'
277-
? abortSignal.reason
278-
: 'cancelled',
279-
{ cause: error },
280-
);
273+
throw new AbortError(abortSignal, error);
281274
}
282275

283276
throw error;

packages/app/src/providers/mock/utils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getChildEntity } from '@h5web/shared/hdf5-utils';
1717
import ndarray from 'ndarray';
1818

1919
import { applyMapping } from '../../vis-packs/core/utils';
20+
import { AbortError } from '../utils';
2021

2122
export const SLOW_TIMEOUT = 3000;
2223

@@ -92,13 +93,7 @@ export async function cancellableDelay(
9293
function handleAbort() {
9394
clearTimeout(timeout);
9495
abortSignal?.removeEventListener('abort', handleAbort);
95-
reject(
96-
new Error(
97-
typeof abortSignal?.reason === 'string'
98-
? abortSignal.reason
99-
: 'cancelled',
100-
),
101-
);
96+
reject(new AbortError(abortSignal));
10297
}
10398

10499
abortSignal?.addEventListener('abort', handleAbort);

packages/app/src/providers/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,13 @@ export function createAxiosProgressHandler(
102102
})
103103
);
104104
}
105+
106+
export class AbortError extends Error {
107+
public constructor(abortSignal?: AbortSignal, cause?: unknown) {
108+
const message =
109+
typeof abortSignal?.reason === 'string' ? abortSignal.reason : undefined;
110+
111+
super(message, { cause });
112+
this.name = 'AbortError';
113+
}
114+
}

0 commit comments

Comments
 (0)