Skip to content

Errors thrown in beforeLoad have properties inserted by the router #4708

Closed

Description

@michaelbull

Which project does this relate to?

Router

Describe the bug

In our application, we throw errors in beforeLoad that are strongly typed objects with rich diagnostic information, such as error codes and messages.

I have observed that the router is mutating our errors before they hit the errorComponent, inserting its own routerCode field.

Consider an example where our rich error object already includes a routerCode field; the router is now overwriting our value that we constructed in our business logic.

It also raises a strong concern about whatever future fields the router decides to start inserting/overwriting on the error object. Given these seem to be internal implementation details, there's no way for consumers to be aware that this is happening unless they observe it, as I did.

Your Example Website or App

https://stackblitz.com/edit/github-l4mi66wo-6kb5naqj?file=src%2Fmain.tsx

Steps to Reproduce the Bug or Issue

Throw an object in a route's beforeLoad, and observe the routerCode property is either added/overwritten on the object you throw. Potentially other fields are overwritten, however routerCode is the only one I've observed.

Expected behavior

Include these error codes in some other fashion - without polluting the original error that was thrown in business logic.

Screenshots or Videos

Image

Platform

"@tanstack/react-router": "1.128.3",

Additional context

No response

Activity

  1. leesb971204 commented on Jul 19, 2025

    @leesb971204
    Contributor

    This is also related to #4707 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions