T
TanStack4w ago
metropolitan-bronze

Returning not found for invalid path params

I'd like to return a 404 error when a path parameter is invalid. I've tried doing this by throwing notFound from the params.parse function, but this just gives a generic 500 error. Is there a way to do this? Example code (I want this to return 404 for /test/abc):
import { createFileRoute, notFound } from "@tanstack/react-router";

export const Route = createFileRoute("/test/$id")({
params: {
parse: ({ id }) => {
const parsedId = Number.parseInt(id, 10);

if (Number.isNaN(parsedId)) {
throw notFound();
}

return { id: parsedId };
},
},
component: () => {
const { id } = Route.useParams();

return <div>Test: {id}</div>;
},
});
import { createFileRoute, notFound } from "@tanstack/react-router";

export const Route = createFileRoute("/test/$id")({
params: {
parse: ({ id }) => {
const parsedId = Number.parseInt(id, 10);

if (Number.isNaN(parsedId)) {
throw notFound();
}

return { id: parsedId };
},
},
component: () => {
const { id } = Route.useParams();

return <div>Test: {id}</div>;
},
});
5 Replies
flat-fuchsia
flat-fuchsia4w ago
are you using router or start?
metropolitan-bronze
metropolitan-bronzeOP4w ago
Start
flat-fuchsia
flat-fuchsia4w ago
might be a bug. can you please create a github issue including a complete reproducer project?
metropolitan-bronze
metropolitan-bronzeOP4w ago
GitHub
notFound in parameter parser returns 500, not 404 · Issue #5733 ...
Which project does this relate to? Router Describe the bug I have a parse function for path parameters, which throws a notFound error when the parameters aren&#39;t in the required format. For exam...
metropolitan-bronze
metropolitan-bronzeOP4w ago
GitHub
GitHub - matt-thomson/tanstack-validate-not-found: Example showing ...
Example showing not found issue in Tanstack Start route validator - matt-thomson/tanstack-validate-not-found

Did you find this page helpful?