Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw instances of Error when unwrap, instead of plain objects #4215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/toolkit/src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading