@@ -28,6 +28,8 @@ import type {
28
28
QueryDefinition ,
29
29
ResultDescription ,
30
30
ResultTypeFrom ,
31
+ SchemaFailureHandler ,
32
+ SchemaFailureInfo ,
31
33
} from '../endpointDefinitions'
32
34
import {
33
35
calculateProvidedBy ,
@@ -65,7 +67,7 @@ import {
65
67
isRejectedWithValue ,
66
68
SHOULD_AUTOBATCH ,
67
69
} from './rtkImports'
68
- import { parseWithSchema } from '../standardSchema'
70
+ import { parseWithSchema , NamedSchemaError } from '../standardSchema'
69
71
70
72
export type BuildThunksApiEndpointQuery <
71
73
Definition extends QueryDefinition < any , any , any , any , any > ,
@@ -330,6 +332,7 @@ export function buildThunks<
330
332
api,
331
333
assertTagType,
332
334
selectors,
335
+ onSchemaFailure,
333
336
} : {
334
337
baseQuery : BaseQuery
335
338
reducerPath : ReducerPath
@@ -338,6 +341,7 @@ export function buildThunks<
338
341
api : Api < BaseQuery , Definitions , ReducerPath , any >
339
342
assertTagType : AssertTagTypes
340
343
selectors : AllSelectors
344
+ onSchemaFailure : SchemaFailureHandler | undefined
341
345
} ) {
342
346
type State = RootState < any , string , ReducerPath >
343
347
@@ -559,7 +563,11 @@ export function buildThunks<
559
563
endpointDefinition
560
564
561
565
if ( argSchema ) {
562
- finalQueryArg = await parseWithSchema ( argSchema , finalQueryArg )
566
+ finalQueryArg = await parseWithSchema (
567
+ argSchema ,
568
+ finalQueryArg ,
569
+ 'argSchema' ,
570
+ )
563
571
}
564
572
565
573
if ( forceQueryFn ) {
@@ -618,7 +626,11 @@ export function buildThunks<
618
626
let { data } = result
619
627
620
628
if ( rawResponseSchema ) {
621
- data = await parseWithSchema ( rawResponseSchema , result . data )
629
+ data = await parseWithSchema (
630
+ rawResponseSchema ,
631
+ result . data ,
632
+ 'rawResponseSchema' ,
633
+ )
622
634
}
623
635
624
636
let transformedResponse = await transformResponse (
@@ -631,6 +643,7 @@ export function buildThunks<
631
643
transformedResponse = await parseWithSchema (
632
644
responseSchema ,
633
645
transformedResponse ,
646
+ 'responseSchema' ,
634
647
)
635
648
}
636
649
@@ -727,6 +740,7 @@ export function buildThunks<
727
740
finalQueryReturnValue . meta = await parseWithSchema (
728
741
metaSchema ,
729
742
finalQueryReturnValue . meta ,
743
+ 'metaSchema' ,
730
744
)
731
745
}
732
746
@@ -739,59 +753,78 @@ export function buildThunks<
739
753
} ) ,
740
754
)
741
755
} catch ( error ) {
742
- let caughtError = error
743
- if ( caughtError instanceof HandledError ) {
744
- let transformErrorResponse = getTransformCallbackForEndpoint (
745
- endpointDefinition ,
746
- 'transformErrorResponse' ,
747
- )
748
- const { rawErrorResponseSchema, errorResponseSchema } =
749
- endpointDefinition
756
+ try {
757
+ let caughtError = error
758
+ if ( caughtError instanceof HandledError ) {
759
+ let transformErrorResponse = getTransformCallbackForEndpoint (
760
+ endpointDefinition ,
761
+ 'transformErrorResponse' ,
762
+ )
763
+ const { rawErrorResponseSchema, errorResponseSchema } =
764
+ endpointDefinition
750
765
751
- let { value, meta } = caughtError
766
+ let { value, meta } = caughtError
752
767
753
- if ( rawErrorResponseSchema ) {
754
- value = await parseWithSchema ( rawErrorResponseSchema , value )
755
- }
768
+ if ( rawErrorResponseSchema ) {
769
+ value = await parseWithSchema (
770
+ rawErrorResponseSchema ,
771
+ value ,
772
+ 'rawErrorResponseSchema' ,
773
+ )
774
+ }
756
775
757
- if ( metaSchema ) {
758
- meta = await parseWithSchema ( metaSchema , meta )
759
- }
776
+ if ( metaSchema ) {
777
+ meta = await parseWithSchema ( metaSchema , meta , 'metaSchema' )
778
+ }
760
779
761
- try {
762
- let transformedErrorResponse = await transformErrorResponse (
763
- value ,
764
- meta ,
765
- arg . originalArgs ,
766
- )
767
- if ( errorResponseSchema ) {
768
- transformedErrorResponse = await parseWithSchema (
769
- errorResponseSchema ,
780
+ try {
781
+ let transformedErrorResponse = await transformErrorResponse (
782
+ value ,
783
+ meta ,
784
+ arg . originalArgs ,
785
+ )
786
+ if ( errorResponseSchema ) {
787
+ transformedErrorResponse = await parseWithSchema (
788
+ errorResponseSchema ,
789
+ transformedErrorResponse ,
790
+ 'errorResponseSchema' ,
791
+ )
792
+ }
793
+
794
+ return rejectWithValue (
770
795
transformedErrorResponse ,
796
+ addShouldAutoBatch ( { baseQueryMeta : meta } ) ,
771
797
)
798
+ } catch ( e ) {
799
+ caughtError = e
772
800
}
773
-
774
- return rejectWithValue (
775
- transformedErrorResponse ,
776
- addShouldAutoBatch ( { baseQueryMeta : meta } ) ,
777
- )
778
- } catch ( e ) {
779
- caughtError = e
780
801
}
781
- }
782
- if (
783
- typeof process !== 'undefined' &&
784
- process . env . NODE_ENV !== 'production'
785
- ) {
786
- console . error (
787
- `An unhandled error occurred processing a request for the endpoint "${ arg . endpointName } ".
802
+ if (
803
+ typeof process !== 'undefined' &&
804
+ process . env . NODE_ENV !== 'production'
805
+ ) {
806
+ console . error (
807
+ `An unhandled error occurred processing a request for the endpoint "${ arg . endpointName } ".
788
808
In the case of an unhandled error, no tags will be "provided" or "invalidated".` ,
789
- caughtError ,
790
- )
791
- } else {
792
- console . error ( caughtError )
809
+ caughtError ,
810
+ )
811
+ } else {
812
+ console . error ( caughtError )
813
+ }
814
+ throw caughtError
815
+ } catch ( error ) {
816
+ if ( error instanceof NamedSchemaError ) {
817
+ const info : SchemaFailureInfo = {
818
+ endpoint : arg . endpointName ,
819
+ arg : arg . originalArgs ,
820
+ type : arg . type ,
821
+ queryCacheKey : arg . type === 'query' ? arg . queryCacheKey : undefined ,
822
+ }
823
+ endpointDefinition . onSchemaFailure ?.( error , info )
824
+ onSchemaFailure ?.( error , info )
825
+ }
826
+ throw error
793
827
}
794
- throw caughtError
795
828
}
796
829
}
797
830
0 commit comments