Handle Runtime Errors in Next.js 13 Nested Routes

InstructorDaniel Phiri

Share this video with your friends

Send Tweet

We need to set up error handling so that if something goes wrong our in our app our users aren't left with a broken page and not know what to do. Next.js 13 has error handling that we can access at the route level. To handle errors on our dynamic pages we'll create an error.js file within our [...slug] folder.

Any errors that occur within these routes will display the component that we define here. It should be noted that errors are handled on the client so you have to set the "use client" directive at the top of your error pages.

On our error page we will maintain the style of our application and direct the user back to the home page informing them that something went wrong. You can even use the reset prop to refresh the page and try again if that's the behavior that you want.