codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
▲

Next.js

59 / 73 topics
59Error Handling in Next.js60Custom Error Pages61Global Error Boundary
Tutorials/Next.js/Error Handling in Next.js
▲Next.js

Error Handling in Next.js

Updated 2026-04-20
3 min read

Introduction

Error handling is a critical aspect of building robust and user-friendly web applications. In this tutorial, we will explore various strategies for error handling in Next.js, a popular React framework that offers server-side rendering (SSR) and static generation capabilities. By the end of this section, you'll have a solid understanding of how to manage errors effectively in your Next.js applications.

Understanding Error Boundaries

React introduced Error Boundaries as a way to handle JavaScript errors anywhere in their child component tree without crashing the entire app. Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.

Creating an Error Boundary

To create an error boundary in Next.js, you can define a class component with static getDerivedStateFromError() and componentDidCatch() methods. Here's how you can implement it:

// components/ErrorBoundary.js
import React from 'react';

class ErrorBoundary extends React.Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    // Update state so the next render will show the fallback UI.
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // You can also log the error to an error reporting service
    console.error("An error occurred:", error);
  }

  render() {
    if (this.state.hasError) {
      // You can render any custom fallback UI
      return <h1>Something went wrong.</h1>;
    }

    return this.props.children;
  }
}

export default ErrorBoundary;

Using the Error Boundary

You can wrap your components with the ErrorBoundary to protect them from errors:

// pages/index.js
import React from 'react';
import ErrorBoundary from '../components/ErrorBoundary';

const HomePage = () => {
  return (
    <div>
      <h1>Welcome to Next.js!</h1>
      {/* Your other components */}
    </div>
  );
};

export default function Home() {
  return (
    <ErrorBoundary>
      <HomePage />
    </ErrorBoundary>
  );
}

Handling Errors in API Routes

Next.js provides a powerful way to handle errors in API routes. When an error occurs in an API route, you can throw an HTTP error using the NextResponse object.

Example of Error Handling in API Route

// pages/api/example.js
import { NextResponse } from 'next/server';

export async function GET(request) {
  try {
    // Simulate a database fetch
    const data = await fetchDataFromDatabase();
    if (!data) {
      throw new Error('No data found');
    }
    return NextResponse.json(data);
  } catch (error) {
    console.error(error);
    return NextResponse.json({ error: 'Something went wrong' }, { status: 500 });
  }
}

async function fetchDataFromDatabase() {
  // Simulate a database fetch
  return null;
}

Global Error Handling

Next.js also provides global error handling mechanisms that can be used to catch unhandled errors in your application.

Using _error.js for Global Errors

You can create a custom _error.js page in the pages directory to handle all client-side and server-side errors. This page will render whenever an error occurs, except for 404s.

// pages/_error.js
import React from 'react';

const ErrorPage = ({ statusCode }) => {
  return (
    <div>
      {statusCode === 404 ? (
        <h1>404 - Page Not Found</h1>
      ) : (
        <h1>An error occurred: {statusCode}</h1>
      )}
    </div>
  );
};

export default ErrorPage;

Server-Side Error Handling

For server-side errors, you can use the Error component in your _app.js file to handle global errors.

// pages/_app.js
import React from 'react';
import App from 'next/app';

class MyApp extends App {
  static async getInitialProps({ Component, ctx }) {
    let pageProps = {};

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    return { pageProps };
  }

  render() {
    const { Component, pageProps } = this.props;

    return (
      <React.Fragment>
        <Component {...pageProps} />
      </React.Fragment>
    );
  }
}

export default MyApp;

Best Practices for Error Handling

  1. Use Try-Catch Blocks: Always use try-catch blocks in your asynchronous functions to handle potential errors gracefully.
  2. Log Errors Properly: Use a logging service like Sentry or LogRocket to capture and analyze errors in production.
  3. Provide User-Friendly Messages: Ensure that error messages are user-friendly and do not expose sensitive information.
  4. Fallback UIs: Implement fallback UIs using Error Boundaries to provide a better user experience when an error occurs.

Conclusion

Error handling is essential for building reliable applications. Next.js provides several mechanisms to handle errors, including Error Boundaries, API route error handling, and global error pages. By understanding these strategies and best practices, you can create more robust and user-friendly applications with Next.js.

Remember, effective error handling not only improves the stability of your application but also enhances the user experience by providing meaningful feedback when something goes wrong.


PreviousUsing Middleware for AuthenticationNext Custom Error Pages

Recommended Gear

Using Middleware for AuthenticationCustom Error Pages