Skip to content

Commit 2367bfd

Browse files
authored
Expose patchRoutesOnNavigation errors directly (#12111)
1 parent 5978f6f commit 2367bfd

File tree

3 files changed

+19
-75
lines changed

3 files changed

+19
-75
lines changed

.changeset/shy-chicken-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@remix-run/router": patch
3+
---
4+
5+
Expose errors thown from `patchRoutesOnNavigation` directly to `useRouteError` instead of wrapping them in a 400 `ErrorResponse` instance

packages/router/__tests__/lazy-discovery-test.ts

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,15 +2064,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
20642064
actionData: null,
20652065
loaderData: {},
20662066
errors: {
2067-
a: new ErrorResponseImpl(
2068-
400,
2069-
"Bad Request",
2070-
new Error(
2071-
'Unable to match URL "/a/b" - the `patchRoutesOnNavigation()` ' +
2072-
"function threw the following error:\nError: broke!"
2073-
),
2074-
true
2075-
),
2067+
a: new Error("broke!"),
20762068
},
20772069
});
20782070
expect(router.state.matches.map((m) => m.route.id)).toEqual(["a"]);
@@ -2139,15 +2131,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
21392131
actionData: null,
21402132
loaderData: {},
21412133
errors: {
2142-
a: new ErrorResponseImpl(
2143-
400,
2144-
"Bad Request",
2145-
new Error(
2146-
'Unable to match URL "/a/b" - the `patchRoutesOnNavigation()` ' +
2147-
"function threw the following error:\nError: broke!"
2148-
),
2149-
true
2150-
),
2134+
a: new Error("broke!"),
21512135
},
21522136
});
21532137
expect(router.state.matches.map((m) => m.route.id)).toEqual(["a"]);
@@ -2213,16 +2197,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
22132197
actionData: null,
22142198
loaderData: {},
22152199
errors: {
2216-
parent: new ErrorResponseImpl(
2217-
400,
2218-
"Bad Request",
2219-
new Error(
2220-
'Unable to match URL "/parent/child/grandchild" - the ' +
2221-
"`patchRoutesOnNavigation()` function threw the following " +
2222-
"error:\nError: broke!"
2223-
),
2224-
true
2225-
),
2200+
parent: new Error("broke!"),
22262201
},
22272202
});
22282203
expect(router.state.matches.map((m) => m.route.id)).toEqual([
@@ -2274,16 +2249,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
22742249
actionData: null,
22752250
loaderData: {},
22762251
errors: {
2277-
parent: new ErrorResponseImpl(
2278-
400,
2279-
"Bad Request",
2280-
new Error(
2281-
'Unable to match URL "/parent/child/grandchild" - the ' +
2282-
"`patchRoutesOnNavigation()` function threw the following " +
2283-
"error:\nError: broke!"
2284-
),
2285-
true
2286-
),
2252+
parent: new Error("broke!"),
22872253
},
22882254
});
22892255
expect(router.state.matches.map((m) => m.route.id)).toEqual([

packages/router/router.ts

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,17 +1690,15 @@ export function createRouter(init: RouterInit): Router {
16901690
if (discoverResult.type === "aborted") {
16911691
return { shortCircuited: true };
16921692
} else if (discoverResult.type === "error") {
1693-
let { boundaryId, error } = handleDiscoverRouteError(
1694-
location.pathname,
1695-
discoverResult
1696-
);
1693+
let boundaryId = findNearestBoundary(discoverResult.partialMatches)
1694+
.route.id;
16971695
return {
16981696
matches: discoverResult.partialMatches,
16991697
pendingActionResult: [
17001698
boundaryId,
17011699
{
17021700
type: ResultType.error,
1703-
error,
1701+
error: discoverResult.error,
17041702
},
17051703
],
17061704
};
@@ -1868,15 +1866,13 @@ export function createRouter(init: RouterInit): Router {
18681866
if (discoverResult.type === "aborted") {
18691867
return { shortCircuited: true };
18701868
} else if (discoverResult.type === "error") {
1871-
let { boundaryId, error } = handleDiscoverRouteError(
1872-
location.pathname,
1873-
discoverResult
1874-
);
1869+
let boundaryId = findNearestBoundary(discoverResult.partialMatches)
1870+
.route.id;
18751871
return {
18761872
matches: discoverResult.partialMatches,
18771873
loaderData: {},
18781874
errors: {
1879-
[boundaryId]: error,
1875+
[boundaryId]: discoverResult.error,
18801876
},
18811877
};
18821878
} else if (!discoverResult.matches) {
@@ -2254,8 +2250,7 @@ export function createRouter(init: RouterInit): Router {
22542250
if (discoverResult.type === "aborted") {
22552251
return;
22562252
} else if (discoverResult.type === "error") {
2257-
let { error } = handleDiscoverRouteError(path, discoverResult);
2258-
setFetcherError(key, routeId, error, { flushSync });
2253+
setFetcherError(key, routeId, discoverResult.error, { flushSync });
22592254
return;
22602255
} else if (!discoverResult.matches) {
22612256
setFetcherError(
@@ -2547,8 +2542,7 @@ export function createRouter(init: RouterInit): Router {
25472542
if (discoverResult.type === "aborted") {
25482543
return;
25492544
} else if (discoverResult.type === "error") {
2550-
let { error } = handleDiscoverRouteError(path, discoverResult);
2551-
setFetcherError(key, routeId, error, { flushSync });
2545+
setFetcherError(key, routeId, discoverResult.error, { flushSync });
25522546
return;
25532547
} else if (!discoverResult.matches) {
25542548
setFetcherError(
@@ -3124,23 +3118,6 @@ export function createRouter(init: RouterInit): Router {
31243118
return { notFoundMatches: matches, route, error };
31253119
}
31263120

3127-
function handleDiscoverRouteError(
3128-
pathname: string,
3129-
discoverResult: DiscoverRoutesErrorResult
3130-
) {
3131-
return {
3132-
boundaryId: findNearestBoundary(discoverResult.partialMatches).route.id,
3133-
error: getInternalRouterError(400, {
3134-
type: "route-discovery",
3135-
pathname,
3136-
message:
3137-
discoverResult.error != null && "message" in discoverResult.error
3138-
? discoverResult.error
3139-
: String(discoverResult.error),
3140-
}),
3141-
};
3142-
}
3143-
31443121
function cancelActiveDeferreds(
31453122
predicate?: (routeId: string) => boolean
31463123
): string[] {
@@ -5485,7 +5462,7 @@ function getInternalRouterError(
54855462
pathname?: string;
54865463
routeId?: string;
54875464
method?: string;
5488-
type?: "defer-action" | "invalid-body" | "route-discovery";
5465+
type?: "defer-action" | "invalid-body";
54895466
message?: string;
54905467
} = {}
54915468
) {
@@ -5494,11 +5471,7 @@ function getInternalRouterError(
54945471

54955472
if (status === 400) {
54965473
statusText = "Bad Request";
5497-
if (type === "route-discovery") {
5498-
errorMessage =
5499-
`Unable to match URL "${pathname}" - the \`patchRoutesOnNavigation()\` ` +
5500-
`function threw the following error:\n${message}`;
5501-
} else if (method && pathname && routeId) {
5474+
if (method && pathname && routeId) {
55025475
errorMessage =
55035476
`You made a ${method} request to "${pathname}" but ` +
55045477
`did not provide a \`loader\` for route "${routeId}", ` +

0 commit comments

Comments
 (0)