Skip to content

add onError hook support to route level#629

Open
stackoverfloweth wants to merge 2 commits intomainfrom
route-onError-hook
Open

add onError hook support to route level#629
stackoverfloweth wants to merge 2 commits intomainfrom
route-onError-hook

Conversation

@stackoverfloweth
Copy link
Copy Markdown
Contributor

In a recent PR we added a new onError hook to the router. This PR extends support for this hook to individual routes.

const route = createRoute({ name: 'my-route' })

route.onError((error, context) => {
  ...
})

Just like the router hooks, these route level hooks are passed the error as well as the same context

Property Type Description
to RouterResolvedRouteUnion The destination route that was being navigated to when the error occurred
from RouterResolvedRouteUnion | null The source route being navigated from. Will be null if there is no previous route
source 'props' | 'hook' | 'component' The origin of the error - indicates whether the error occurred in route props, a hook, or a component
reject RouterReject Function to reject the navigation with a custom rejection
push RouterPush Function to navigate to a different route using push navigation
replace RouterReplace Function to navigate to a different route using replace navigation
const route = createRoute({ name: 'my-route' })

route.onError((error, { to, from, source, push, replace, reject }) => {
  if (error instanceof MyExpectedError) {
    push(...)
  }

  throw reject('AppError')
})

@netlify
Copy link
Copy Markdown

netlify bot commented Dec 28, 2025

Deploy Preview for kitbag-router ready!

Name Link
🔨 Latest commit 0cb6f0e
🔍 Latest deploy log https://app.netlify.com/projects/kitbag-router/deploys/6951dea37be9c90008f6fb7a
😎 Deploy Preview https://deploy-preview-629--kitbag-router.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@pleek91
Copy link
Copy Markdown
Contributor

pleek91 commented Feb 18, 2026

@stackoverfloweth If I remember the question I had about this PR was seems like we shouldn't need to do extra wrappers around the hooks because there's already a try catch around running the hooks that runs the global onError hooks. So I think we could add route level onError hooks to that existing system.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants