Skip to content

Commit ec587ae

Browse files
GabbeVryansolid
authored andcommitted
fix(signals): propagate async setter errors
1 parent e346e61 commit ec587ae

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Route errors thrown while applying asynchronous computed setters through the
6+
node's error state. This prevents user callbacks invoked during asynchronous
7+
projection reconciliation, such as key selectors, from escaping as unhandled
8+
promise rejections.

‎packages/signals/src/core/async.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,12 @@ export function handleAsync<T>(
449449
// change (and only then classify it as an async landing).
450450
if (__DEV__ && attrHooks !== null) attrHooks.asyncStart(el);
451451
if (setter) {
452-
setter(value);
452+
try {
453+
setter(value);
454+
} catch (error) {
455+
handleError(error);
456+
return;
457+
}
453458
if (wasUninitialized) clearStatus(el, true);
454459
} else if (el._x?._overrideValue !== undefined) {
455460
// Optimistic node — resting OR covered by an active override — holds

‎packages/signals/tests/store/createProjection.async.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,29 @@ describe("errored derive follows memo rules", () => {
10311031
flush();
10321032
};
10331033

1034+
it("routes async reconciliation errors through the projection error state", async () => {
1035+
const boom = new Error("invalid projection key");
1036+
let store!: { id: number };
1037+
const dispose = createRoot(d => {
1038+
store = createProjection(
1039+
async () => ({ id: 2 }),
1040+
{ id: 1 },
1041+
{
1042+
key: item => {
1043+
if (item.id === 2) throw boom;
1044+
return item.id;
1045+
}
1046+
}
1047+
);
1048+
return d;
1049+
});
1050+
1051+
flush();
1052+
await settle();
1053+
expect(() => untrack(() => store.id)).toThrow("invalid projection key");
1054+
dispose();
1055+
});
1056+
10341057
it("untracked reads of a rejected uninitialized derive throw its error", async () => {
10351058
const boom = new Error("boom");
10361059
let store!: any;

0 commit comments

Comments
 (0)