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