Skip to content

Commit

Permalink
Unwrap throws instances of Errors instead of plain objects to capture…
Browse files Browse the repository at this point in the history
… stacktrace
  • Loading branch information
pradiev committed Feb 15, 2024
1 parent 87f045c commit 5ec2ebb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/toolkit/src/createAsyncThunk.ts
Expand Up @@ -735,7 +735,15 @@ export function unwrapResult<R extends UnwrappableAction>(
throw action.payload
}
if (action.error) {
throw action.error
const errorInstance: any = new Error(action.error.message || action.error.name || 'Thunk Error')

for (const prop in action.error) {
if (!errorInstance[prop]) {
errorInstance[prop] = action.error[prop]
}
}

throw errorInstance
}
return action.payload
}
Expand Down
18 changes: 17 additions & 1 deletion packages/toolkit/src/query/core/buildInitiate.ts
Expand Up @@ -362,7 +362,23 @@ You must add the middleware for RTK-Query to function correctly!`,
const result = await statePromise

if (result.isError) {
throw result.error
let errorMessage = '';
const errorMap = result.error as any;

if (errorMap && errorMap.data && errorMap.status) {
errorMessage = `RTK Query responded with ${errorMap.status} when requesting ${errorMap.data.path}`
} else {
errorMessage = errorMap.message || errorMap.name || 'RTK Query Error'
}

const errorToThrow: any = new Error(errorMessage)
for (const prop in errorMap) {
if (!errorToThrow[prop]) {
errorToThrow[prop] = errorMap[prop]
}
}

throw errorToThrow
}

return result.data
Expand Down

0 comments on commit 5ec2ebb

Please sign in to comment.