Skip to content

Commit 4513bbf

Browse files
committed
make sure onSchemaFailure still gets called when caught
1 parent 79d1dec commit 4513bbf

File tree

2 files changed

+211
-236
lines changed

2 files changed

+211
-236
lines changed

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -763,95 +763,83 @@ export function buildThunks<
763763
}),
764764
)
765765
} catch (error) {
766-
try {
767-
let caughtError = error
768-
if (caughtError instanceof HandledError) {
769-
let transformErrorResponse = getTransformCallbackForEndpoint(
770-
endpointDefinition,
771-
'transformErrorResponse',
772-
)
773-
const { rawErrorResponseSchema, errorResponseSchema } =
774-
endpointDefinition
775-
776-
let { value, meta } = caughtError
777-
778-
try {
779-
if (rawErrorResponseSchema && !skipSchemaValidation) {
780-
value = await parseWithSchema(
781-
rawErrorResponseSchema,
782-
value,
783-
'rawErrorResponseSchema',
784-
meta,
785-
)
786-
}
766+
let caughtError = error
767+
if (caughtError instanceof HandledError) {
768+
let transformErrorResponse = getTransformCallbackForEndpoint(
769+
endpointDefinition,
770+
'transformErrorResponse',
771+
)
772+
const { rawErrorResponseSchema, errorResponseSchema } =
773+
endpointDefinition
787774

788-
if (metaSchema && !skipSchemaValidation) {
789-
meta = await parseWithSchema(metaSchema, meta, 'metaSchema', meta)
790-
}
791-
let transformedErrorResponse = await transformErrorResponse(
775+
let { value, meta } = caughtError
776+
777+
try {
778+
if (rawErrorResponseSchema && !skipSchemaValidation) {
779+
value = await parseWithSchema(
780+
rawErrorResponseSchema,
792781
value,
782+
'rawErrorResponseSchema',
793783
meta,
794-
arg.originalArgs,
795784
)
796-
if (errorResponseSchema && !skipSchemaValidation) {
797-
transformedErrorResponse = await parseWithSchema(
798-
errorResponseSchema,
799-
transformedErrorResponse,
800-
'errorResponseSchema',
801-
meta,
802-
)
803-
}
785+
}
804786

805-
return rejectWithValue(
787+
if (metaSchema && !skipSchemaValidation) {
788+
meta = await parseWithSchema(metaSchema, meta, 'metaSchema', meta)
789+
}
790+
let transformedErrorResponse = await transformErrorResponse(
791+
value,
792+
meta,
793+
arg.originalArgs,
794+
)
795+
if (errorResponseSchema && !skipSchemaValidation) {
796+
transformedErrorResponse = await parseWithSchema(
797+
errorResponseSchema,
806798
transformedErrorResponse,
807-
addShouldAutoBatch({ baseQueryMeta: meta }),
799+
'errorResponseSchema',
800+
meta,
808801
)
809-
} catch (e) {
810-
caughtError = e
811802
}
803+
804+
return rejectWithValue(
805+
transformedErrorResponse,
806+
addShouldAutoBatch({ baseQueryMeta: meta }),
807+
)
808+
} catch (e) {
809+
caughtError = e
812810
}
813-
if (caughtError instanceof NamedSchemaError) {
814-
const info: SchemaFailureInfo = {
815-
endpoint: arg.endpointName,
816-
arg: arg.originalArgs,
817-
type: arg.type,
818-
queryCacheKey: arg.type === 'query' ? arg.queryCacheKey : undefined,
819-
}
820-
const { catchSchemaFailure = globalCatchSchemaFailure } =
821-
endpointDefinition
822-
if (catchSchemaFailure) {
823-
return rejectWithValue(
824-
catchSchemaFailure(caughtError, info),
825-
addShouldAutoBatch({ baseQueryMeta: caughtError._bqMeta }),
826-
)
827-
}
811+
}
812+
if (caughtError 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,
828818
}
829-
if (
830-
typeof process !== 'undefined' &&
831-
process.env.NODE_ENV !== 'production'
832-
) {
833-
console.error(
834-
`An unhandled error occurred processing a request for the endpoint "${arg.endpointName}".
835-
In the case of an unhandled error, no tags will be "provided" or "invalidated".`,
836-
caughtError,
819+
endpointDefinition.onSchemaFailure?.(caughtError, info)
820+
onSchemaFailure?.(caughtError, info)
821+
const { catchSchemaFailure = globalCatchSchemaFailure } =
822+
endpointDefinition
823+
if (catchSchemaFailure) {
824+
return rejectWithValue(
825+
catchSchemaFailure(caughtError, info),
826+
addShouldAutoBatch({ baseQueryMeta: caughtError._bqMeta }),
837827
)
838-
} else {
839-
console.error(caughtError)
840828
}
841-
throw caughtError
842-
} catch (error) {
843-
if (error instanceof NamedSchemaError) {
844-
const info: SchemaFailureInfo = {
845-
endpoint: arg.endpointName,
846-
arg: arg.originalArgs,
847-
type: arg.type,
848-
queryCacheKey: arg.type === 'query' ? arg.queryCacheKey : undefined,
849-
}
850-
endpointDefinition.onSchemaFailure?.(error, info)
851-
onSchemaFailure?.(error, info)
852-
}
853-
throw error
854829
}
830+
if (
831+
typeof process !== 'undefined' &&
832+
process.env.NODE_ENV !== 'production'
833+
) {
834+
console.error(
835+
`An unhandled error occurred processing a request for the endpoint "${arg.endpointName}".
836+
In the case of an unhandled error, no tags will be "provided" or "invalidated".`,
837+
caughtError,
838+
)
839+
} else {
840+
console.error(caughtError)
841+
}
842+
throw caughtError
855843
}
856844
}
857845

0 commit comments

Comments
 (0)