Skip to content

Commit 5ec2ebb

Browse files
committed
Unwrap throws instances of Errors instead of plain objects to capture stacktrace
1 parent 87f045c commit 5ec2ebb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

packages/toolkit/src/createAsyncThunk.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,15 @@ export function unwrapResult<R extends UnwrappableAction>(
735735
throw action.payload
736736
}
737737
if (action.error) {
738-
throw action.error
738+
const errorInstance: any = new Error(action.error.message || action.error.name || 'Thunk Error')
739+
740+
for (const prop in action.error) {
741+
if (!errorInstance[prop]) {
742+
errorInstance[prop] = action.error[prop]
743+
}
744+
}
745+
746+
throw errorInstance
739747
}
740748
return action.payload
741749
}

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,23 @@ You must add the middleware for RTK-Query to function correctly!`,
362362
const result = await statePromise
363363

364364
if (result.isError) {
365-
throw result.error
365+
let errorMessage = '';
366+
const errorMap = result.error as any;
367+
368+
if (errorMap && errorMap.data && errorMap.status) {
369+
errorMessage = `RTK Query responded with ${errorMap.status} when requesting ${errorMap.data.path}`
370+
} else {
371+
errorMessage = errorMap.message || errorMap.name || 'RTK Query Error'
372+
}
373+
374+
const errorToThrow: any = new Error(errorMessage)
375+
for (const prop in errorMap) {
376+
if (!errorToThrow[prop]) {
377+
errorToThrow[prop] = errorMap[prop]
378+
}
379+
}
380+
381+
throw errorToThrow
366382
}
367383

368384
return result.data

0 commit comments

Comments
 (0)