Skip to content

Commit 1f8a220

Browse files
committed
* mark all ctor params as optional to match with the requirement of addKnownErrorConstructor(): https://github.com/sindresorhus/serialize-error/blob/4d0f3b27d618739c3a3868fd5b4d619b4ea03ad8/error-constructors.js#L34-L39 @ ApiResponseError
* partial revert 2032607 @ `queryFunction()` @ `api/index.ts` @ fe
1 parent ee30af3 commit 1f8a220

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

fe/src/api/index.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@ import type { PublicRuntimeConfig } from 'nuxt/schema';
22
import type { Enabled, InfiniteData, Query, UseInfiniteQueryOptions, UseQueryOptions } from '@tanstack/vue-query';
33
import { QueryObserver } from '@tanstack/vue-query';
44
import { FetchError } from 'ofetch';
5-
import { serializeError } from 'serialize-error';
5+
import { addKnownErrorConstructor } from 'serialize-error';
66
import _ from 'lodash';
77

88
export class ApiResponseError extends Error {
99
public constructor(
10-
public readonly errorCode: number,
11-
public readonly errorInfo: Record<string, unknown[]> | string,
10+
public readonly errorCode?: number,
11+
public readonly errorInfo?: Record<string, unknown[]> | string,
1212
public readonly fetchError?: FetchError
1313
) {
1414
super(JSON.stringify({ fetchError, errorCode, errorInfo }));
1515
}
1616
}
17+
18+
[FetchError, ApiResponseError].forEach(error => {
19+
type BaseErrorConstructor = Parameters<typeof addKnownErrorConstructor>[0];
20+
try {
21+
addKnownErrorConstructor(error as BaseErrorConstructor);
22+
23+
// https://github.com/sindresorhus/serialize-error/blob/4d0f3b27d618739c3a3868fd5b4d619b4ea03ad8/error-constructors.js#L31C19-L31C70
24+
// eslint-disable-next-line no-empty
25+
} catch {}
26+
});
27+
1728
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
1829
export const isApiError = (response: ApiError | unknown): response is ApiError => _.isObject(response)
1930
&& 'errorCode' in response && _.isNumber(response.errorCode)
@@ -42,13 +53,9 @@ export const queryFunction = async <TResponse>
4253
signal
4354
}
4455
) as TResponse;
45-
} catch (e_: unknown) {
46-
let e = e_;
56+
} catch (e: unknown) {
4757
if (e instanceof FetchError && isApiError(e.data))
48-
e = new ApiResponseError(e.data.errorCode, e.data.errorInfo, e);
49-
if (e instanceof Error)
50-
// eslint-disable-next-line @typescript-eslint/only-throw-error
51-
throw serializeError(e);
58+
throw new ApiResponseError(e.data.errorCode, e.data.errorInfo, e);
5259
throw e;
5360
} finally {
5461
if (import.meta.client) {

0 commit comments

Comments
 (0)