Skip to content

Commit

Permalink
Fix error boundary tracking for multiple errors bubbling to the same …
Browse files Browse the repository at this point in the history
…boundary (#9702)

* Fix error boundary tracking for multiple errors bubbling to the same boundary

* Add changeset
  • Loading branch information
brophdawg11 authored Dec 13, 2022
1 parent 6c8a56f commit c194f4a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-rules-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix error boundary tracking for multiple errors bubbling to the same boundary
55 changes: 55 additions & 0 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10774,6 +10774,61 @@ describe("a router", () => {
});
});

it("should handle multiple errors at separate boundaries", async () => {
let routes = [
{
id: "root",
path: "/",
loader: () => Promise.reject("ROOT"),
hasErrorBoundary: true,
children: [
{
id: "child",
path: "child",
loader: () => Promise.reject("CHILD"),
hasErrorBoundary: true,
},
],
},
];

let { query } = createStaticHandler(routes);
let context;

context = await query(createRequest("/child"));
expect(context.errors).toEqual({
root: "ROOT",
child: "CHILD",
});
});

it("should handle multiple errors at the same boundary", async () => {
let routes = [
{
id: "root",
path: "/",
loader: () => Promise.reject("ROOT"),
hasErrorBoundary: true,
children: [
{
id: "child",
path: "child",
loader: () => Promise.reject("CHILD"),
},
],
},
];

let { query } = createStaticHandler(routes);
let context;

context = await query(createRequest("/child"));
expect(context.errors).toEqual({
// higher error value wins
root: "ROOT",
});
});

it("should handle aborted load requests", async () => {
let dfd = createDeferred();
let controller = new AbortController();
Expand Down
11 changes: 8 additions & 3 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2848,9 +2848,14 @@ function processRouteLoaderData(
error = Object.values(pendingError)[0];
pendingError = undefined;
}
errors = Object.assign(errors || {}, {
[boundaryMatch.route.id]: error,
});

errors = errors || {};

// Prefer higher error values if lower errors bubble to the same boundary
if (errors[boundaryMatch.route.id] == null) {
errors[boundaryMatch.route.id] = error;
}

// Once we find our first (highest) error, we set the status code and
// prevent deeper status codes from overriding
if (!foundError) {
Expand Down

0 comments on commit c194f4a

Please sign in to comment.