Skip to content

Commit

Permalink
Expose patchRoutesOnNavigation errors directly (#12111)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Oct 10, 2024
1 parent 5978f6f commit 2367bfd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-chicken-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Expose errors thown from `patchRoutesOnNavigation` directly to `useRouteError` instead of wrapping them in a 400 `ErrorResponse` instance
42 changes: 4 additions & 38 deletions packages/router/__tests__/lazy-discovery-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2064,15 +2064,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
actionData: null,
loaderData: {},
errors: {
a: new ErrorResponseImpl(
400,
"Bad Request",
new Error(
'Unable to match URL "/a/b" - the `patchRoutesOnNavigation()` ' +
"function threw the following error:\nError: broke!"
),
true
),
a: new Error("broke!"),
},
});
expect(router.state.matches.map((m) => m.route.id)).toEqual(["a"]);
Expand Down Expand Up @@ -2139,15 +2131,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
actionData: null,
loaderData: {},
errors: {
a: new ErrorResponseImpl(
400,
"Bad Request",
new Error(
'Unable to match URL "/a/b" - the `patchRoutesOnNavigation()` ' +
"function threw the following error:\nError: broke!"
),
true
),
a: new Error("broke!"),
},
});
expect(router.state.matches.map((m) => m.route.id)).toEqual(["a"]);
Expand Down Expand Up @@ -2213,16 +2197,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
actionData: null,
loaderData: {},
errors: {
parent: new ErrorResponseImpl(
400,
"Bad Request",
new Error(
'Unable to match URL "/parent/child/grandchild" - the ' +
"`patchRoutesOnNavigation()` function threw the following " +
"error:\nError: broke!"
),
true
),
parent: new Error("broke!"),
},
});
expect(router.state.matches.map((m) => m.route.id)).toEqual([
Expand Down Expand Up @@ -2274,16 +2249,7 @@ describe("Lazy Route Discovery (Fog of War)", () => {
actionData: null,
loaderData: {},
errors: {
parent: new ErrorResponseImpl(
400,
"Bad Request",
new Error(
'Unable to match URL "/parent/child/grandchild" - the ' +
"`patchRoutesOnNavigation()` function threw the following " +
"error:\nError: broke!"
),
true
),
parent: new Error("broke!"),
},
});
expect(router.state.matches.map((m) => m.route.id)).toEqual([
Expand Down
47 changes: 10 additions & 37 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,17 +1690,15 @@ export function createRouter(init: RouterInit): Router {
if (discoverResult.type === "aborted") {
return { shortCircuited: true };
} else if (discoverResult.type === "error") {
let { boundaryId, error } = handleDiscoverRouteError(
location.pathname,
discoverResult
);
let boundaryId = findNearestBoundary(discoverResult.partialMatches)
.route.id;
return {
matches: discoverResult.partialMatches,
pendingActionResult: [
boundaryId,
{
type: ResultType.error,
error,
error: discoverResult.error,
},
],
};
Expand Down Expand Up @@ -1868,15 +1866,13 @@ export function createRouter(init: RouterInit): Router {
if (discoverResult.type === "aborted") {
return { shortCircuited: true };
} else if (discoverResult.type === "error") {
let { boundaryId, error } = handleDiscoverRouteError(
location.pathname,
discoverResult
);
let boundaryId = findNearestBoundary(discoverResult.partialMatches)
.route.id;
return {
matches: discoverResult.partialMatches,
loaderData: {},
errors: {
[boundaryId]: error,
[boundaryId]: discoverResult.error,
},
};
} else if (!discoverResult.matches) {
Expand Down Expand Up @@ -2254,8 +2250,7 @@ export function createRouter(init: RouterInit): Router {
if (discoverResult.type === "aborted") {
return;
} else if (discoverResult.type === "error") {
let { error } = handleDiscoverRouteError(path, discoverResult);
setFetcherError(key, routeId, error, { flushSync });
setFetcherError(key, routeId, discoverResult.error, { flushSync });
return;
} else if (!discoverResult.matches) {
setFetcherError(
Expand Down Expand Up @@ -2547,8 +2542,7 @@ export function createRouter(init: RouterInit): Router {
if (discoverResult.type === "aborted") {
return;
} else if (discoverResult.type === "error") {
let { error } = handleDiscoverRouteError(path, discoverResult);
setFetcherError(key, routeId, error, { flushSync });
setFetcherError(key, routeId, discoverResult.error, { flushSync });
return;
} else if (!discoverResult.matches) {
setFetcherError(
Expand Down Expand Up @@ -3124,23 +3118,6 @@ export function createRouter(init: RouterInit): Router {
return { notFoundMatches: matches, route, error };
}

function handleDiscoverRouteError(
pathname: string,
discoverResult: DiscoverRoutesErrorResult
) {
return {
boundaryId: findNearestBoundary(discoverResult.partialMatches).route.id,
error: getInternalRouterError(400, {
type: "route-discovery",
pathname,
message:
discoverResult.error != null && "message" in discoverResult.error
? discoverResult.error
: String(discoverResult.error),
}),
};
}

function cancelActiveDeferreds(
predicate?: (routeId: string) => boolean
): string[] {
Expand Down Expand Up @@ -5485,7 +5462,7 @@ function getInternalRouterError(
pathname?: string;
routeId?: string;
method?: string;
type?: "defer-action" | "invalid-body" | "route-discovery";
type?: "defer-action" | "invalid-body";
message?: string;
} = {}
) {
Expand All @@ -5494,11 +5471,7 @@ function getInternalRouterError(

if (status === 400) {
statusText = "Bad Request";
if (type === "route-discovery") {
errorMessage =
`Unable to match URL "${pathname}" - the \`patchRoutesOnNavigation()\` ` +
`function threw the following error:\n${message}`;
} else if (method && pathname && routeId) {
if (method && pathname && routeId) {
errorMessage =
`You made a ${method} request to "${pathname}" but ` +
`did not provide a \`loader\` for route "${routeId}", ` +
Expand Down

0 comments on commit 2367bfd

Please sign in to comment.