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

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

Global Error Boundary

Updated 2026-04-20
3 min read

Global Error Boundary

In modern web development, error handling is a critical aspect of building robust applications. Errors can occur due to various reasons such as network issues, incorrect data fetching, or unexpected user inputs. In Next.js, managing errors effectively ensures that your application remains responsive and provides a good user experience even when something goes wrong.

Introduction to Error Boundaries

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. They help prevent the entire app from crashing due to an error in one part of the application.

Next.js provides built-in support for error handling through its Error Boundary feature. This feature allows you to define global error boundaries that can catch errors across your entire application or specific parts of it.

Setting Up a Global Error Boundary

To set up a global error boundary in Next.js, you need to create a custom _error.js page in the pages directory. This page will act as the fallback UI for any unhandled errors within your application.

Step 1: Create the _error.js Page

First, create a new file named _error.js inside the pages directory of your Next.js project.

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

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

Error.getInitialProps = ({ res, err }) => {
  const statusCode = res ? res.statusCode : err ? err.statusCode : 404;
  return { statusCode };
};

export default Error;

Explanation

  • statusCode: This property is used to determine the type of error that occurred. Next.js automatically passes this information to the _error.js page.
  • getInitialProps: This static method allows you to fetch additional data or modify the props before rendering the component. In this case, it retrieves the status code from the response or error object.

Step 2: Test the Global Error Boundary

To test the global error boundary, you can intentionally trigger an error in one of your components. For example, create a new page that throws an error:

// pages/error-example.js
import React from 'react';

const ErrorExample = () => {
  throw new Error('This is a test error');
};

export default ErrorExample;

When you navigate to this page, the global error boundary defined in _error.js should catch the error and display the appropriate fallback UI.

Local Error Boundaries

While global error boundaries are useful for catching errors across your entire application, sometimes it's beneficial to handle errors at a more granular level. Next.js allows you to define local error boundaries within specific components or pages.

Step 1: Create a Local Error Boundary Component

Create a new component that acts as an error boundary:

// components/LocalErrorBoundary.js
import React, { Component } from 'react';

class LocalErrorBoundary extends 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 log the error to an error reporting service here
    console.error('Caught an error:', error, errorInfo);
  }

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

    return this.props.children;
  }
}

export default LocalErrorBoundary;

Step 2: Use the Local Error Boundary

Wrap your components with the LocalErrorBoundary component to handle errors locally:

// pages/local-error-example.js
import React from 'react';
import LocalErrorBoundary from '../components/LocalErrorBoundary';

const LocalErrorExample = () => {
  throw new Error('This is a local test error');
};

export default function Page() {
  return (
    <div>
      <h1>Local Error Example</h1>
      <LocalErrorBoundary>
        <LocalErrorExample />
      </LocalErrorBoundary>
    </div>
  );
}

In this example, the LocalErrorBoundary will catch errors thrown by the LocalErrorExample component and display a fallback UI.

Best Practices for Error Handling in Next.js

  1. Use Global Error Boundaries for Critical Errors: Ensure that your global error boundary is set up to handle critical errors gracefully and provide meaningful feedback to users.
  2. Implement Local Error Boundaries for Specific Scenarios: Use local error boundaries when you need more granular control over error handling, such as in specific components or pages.
  3. Log Errors for Debugging: Implement logging within your error boundaries to capture detailed information about errors for debugging purposes.
  4. Provide User-Friendly Fallback UIs: Design fallback UIs that are user-friendly and provide helpful messages or actions users can take.
  5. Test Error Handling: Regularly test your error handling mechanisms to ensure they work as expected in different scenarios.

Conclusion

Error boundaries are a powerful feature in Next.js that help manage errors gracefully, ensuring your application remains stable and provides a good user experience even when something goes wrong. By setting up global and local error boundaries, you can effectively handle errors across your entire application or specific parts of it. Following best practices for error handling will further enhance the robustness and reliability of your Next.js applications.

By following this comprehensive guide, you should now have a solid understanding of how to implement and utilize global error boundaries in your Next.js projects.


PreviousCustom Error PagesNext Logging and Monitoring

Recommended Gear

Custom Error PagesLogging and Monitoring