fix(connect): log mapStateToProps errors to console.error in dev (#1942)#2306
fix(connect): log mapStateToProps errors to console.error in dev (#1942)#2306webdevelopersrinu wants to merge 1 commit into
mapStateToProps errors to console.error in dev (#1942)#2306Conversation
…uxjs#1942) Errors thrown inside mapStateToProps (or selectors) are caught in the subscription callback at connect.tsx and stored in lastThrownError, but since v8 the component no longer unmounts on selector errors so the captured error is silently swallowed during development. This adds a console.error log gated by process.env.NODE_ENV !== 'production' so the error surfaces in dev tools, aiding debugging without changing any production behavior. Diagnosed by @alexanderchr on the issue thread; approach approved by @markerikson in Dec 2024. Fixes reduxjs#1942
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
mapStateToProps errors to console.error in dev (#1942)
|
The current CI failure is related to arethetypeswrong/arethetypeswrong.github.io#258 which is caused by 101arrowz/fflate#258. As a temporary workaround, we can pin "resolutions": {
"fflate": "0.8.2"
},This should unblock CI until the upstream issue is resolved. |
|
Thanks @aryaemami59 — totally missed your earlier comment, sorry about |
Thanks. I'd prefer to wait and see whether this gets resolved upstream first. That also gives the rest of the team more time to review the PR. Update: This has since been fixed upstream in |
Summary
Logs errors thrown in
mapStateToProps(or selectors) toconsole.errorin non-production environments, so they surface during development
instead of being silently swallowed.
Fixes #1942
Background
Since v8.0.0, errors thrown inside
mapStateToPropsare caught in thesubscription callback (connect.tsx#L133-L136) and stored in
lastThrownError. In v7, those errors surfaced because the componentunmounted on selector errors (the unmount path re-throws
lastThrownError). In v8+, the component no longer unmounts — it justre-renders in its pre-error state — so the error is silently swallowed.
This was diagnosed by @alexanderchr in
#1942 (Dec 12, 2024), and @markerikson approved the dev-only
logging approach in the same thread:
Changes
src/components/connect.tsx— log the caught error toconsole.errorwhen
process.env.NODE_ENV !== 'production', with a brief message anda link back to this issue
test/components/connect.spec.tsx— add a unit test that dispatchesan action which causes
mapStateToPropsto throw, then asserts theerror was logged via
console.errorBehavior
original error object, making it discoverable in DevTools
Test plan
yarn test— allconnecttests pass (65/65), new test passes,no regressions in code I touched
yarn lintruns as part ofpretestand passesvitest --typecheckreports no errors)