redux-toolkit: Can UnwrapResult prefer payload error?

In unwrapResult, we throw returned.error, which for custom handlers is always “Rejected” Can we throw payload.error if it exists before throwing returned.error? (Is there a way to overwrite returned.error that I didn’t see in rejectWithValue?)

export function unwrapResult<R extends ActionTypesWithOptionalErrorAction>(
  returned: R
): PayloadForActionTypesExcludingErrorActions<R> {
  if ('error' in returned) {
    throw returned.error
  }
  return (returned as any).payload
}

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 23 (9 by maintainers)

Commits related to this issue

Most upvoted comments

I’d opt for adding action.meta.rejectedWithValue (like we already have action.meta.aborted and action.meta.condition) and doing something like

export function unwrapResult(action) {
  if (!action) { return; } //someone actually called it with `undefined` today, we should probably catch that?
  if (action.meta && action.meta.rejectedWithValue) { throw action.payload }
  if (action.error) { throw action.error }
  return action.payload
}

PS: on the other hand, that undefined handling will mock up the return value, so maybe just let JS error out on that