TanStack / TanStack/router

`error.routerCode` removed and not a clear replacement for `error.routerCode === 'PARSE_PARAMS'` or `error.routerCode === 'VALIDATE_SEARCH'`

Open
#7,545 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
15.1k
Forks
1.9k
Avg merge
1d 20h
Merged PRs (30d)
143

Description

Which project does this relate to?

Router

Describe the bug

In commit 2f53749 (shipped in @tanstack/react-router@1.170.10), error.routerCode was removed from the error passed to a route's onError handler. Previously, search/param validation failures could be detected like this:

export const Route = createFileRoute('/')({
  component: RouteComponent,
  params: pathParamsSchema, // Using Zod (v4) schema
  validateSearch: searchParamsSchema, // Using Zod (v4) schema
  ....
  onError: (error) => {
    if (error.routerCode === 'PARSE_PARAMS') throw redirect({ to: '/parse-error' })
    if (error.routerCode === 'VALIDATE_SEARCH') throw redirect({ to: '/validation-error' })
    throw error
  }
}

This now silently breaks because routerCode no longer exists. I would say this change is more of a breaking change as the contract has changed, but I'm unsure if it is intentional or just a bug.

Looking at the code base (guessing the change is intentionally) a possible solution to have the minimum changes and exact functionality could be:

onError: (error) => {
  if (error instanceof PathParamError) throw redirect({ to: '/' })
  if (error instanceof SearchParamError) throw redirect({ to: '/' })
  throw error
}

However,@tanstack/react-router only re-exports SearchParamError, not PathParamError it is only exported from @tanstack/router-core, forcing consumers to import from a transitive dependency (maybe is not but most likely yes):

import { SearchParamError } from '@tanstack/react-router'; // ✅ works
import { PathParamError } from '@tanstack/react-router';   // ❌ not exported
import { PathParamError } from '@tanstack/router-core';    // works, but most likely reaching into a transitive dep
Expected behavior

Either have routerCode back or re-export PathParamError from @tanstack/react-router alongside SearchParamError, so consumers don't have to import from @tanstack/router-core (which most likely is going to be a transitive dep).

If that's not correct then docs should explain the proper way to differentiate between a parse error and a validation error is provided.

Platform
  • Router / Start Version: @tanstack/react-router 1.170.11 (@tanstack/router-core 1.171.9, @tanstack/router-plugin 1.168.14)
  • OS: Any
  • Browser: Any
  • Browser Version: Any
  • Bundler: vite
  • Bundler Version: 8.0.16
Additional context

Maybe the usage of error instanceOf PathParamError or error instanceOf SearchParamError is not a good solution so in that case what would be a good solution?

Side note: the documentation still references the removed routerCode in Search Params guide

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the route onError entry point and the Search Params guide, which still references routerCode. Compare the current @tanstack/react-router exports with the error types described in the issue and review the change from commit 2f53749. Done means consumers have a documented, supported way to distinguish parse and validation failures without relying on an unavailable export or removed field.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
documentation, frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.