Error handling with error.tsx in Next.js
📖 Scenario: You are building a Next.js app that shows a list of products. Sometimes, the data fetching might fail or the page might encounter an error. You want to create a simple error page to show a friendly message when something goes wrong.
🎯 Goal: Create an error.tsx file in the app directory that displays a user-friendly error message. This error page should catch errors and show a heading and a paragraph explaining that something went wrong.
📋 What You'll Learn
Create an
error.tsx file exporting a React component named Error.The
Error component should accept a prop called error of type Error.Display a heading with the text
Something went wrong!.Display the error message inside a paragraph.
Export the
Error component as default.💡 Why This Matters
🌍 Real World
Custom error pages improve user experience by showing friendly messages when something goes wrong in your web app.
💼 Career
Knowing how to handle errors gracefully is important for frontend and full-stack developers working with Next.js or React frameworks.
Progress0 / 4 steps