@@ -2,18 +2,29 @@ import type { PublicRuntimeConfig } from 'nuxt/schema';
2
2
import type { Enabled , InfiniteData , Query , UseInfiniteQueryOptions , UseQueryOptions } from '@tanstack/vue-query' ;
3
3
import { QueryObserver } from '@tanstack/vue-query' ;
4
4
import { FetchError } from 'ofetch' ;
5
- import { serializeError } from 'serialize-error' ;
5
+ import { addKnownErrorConstructor } from 'serialize-error' ;
6
6
import _ from 'lodash' ;
7
7
8
8
export class ApiResponseError extends Error {
9
9
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 ,
12
12
public readonly fetchError ?: FetchError
13
13
) {
14
14
super ( JSON . stringify ( { fetchError, errorCode, errorInfo } ) ) ;
15
15
}
16
16
}
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
+
17
28
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
18
29
export const isApiError = ( response : ApiError | unknown ) : response is ApiError => _ . isObject ( response )
19
30
&& 'errorCode' in response && _ . isNumber ( response . errorCode )
@@ -42,13 +53,9 @@ export const queryFunction = async <TResponse>
42
53
signal
43
54
}
44
55
) as TResponse ;
45
- } catch ( e_ : unknown ) {
46
- let e = e_ ;
56
+ } catch ( e : unknown ) {
47
57
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 ) ;
52
59
throw e ;
53
60
} finally {
54
61
if ( import . meta. client ) {
0 commit comments