npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

async-error-boundary

v1.0.0

Published

A React error boundary component that captures and handles both synchronous and asynchronous errors, including unhandled promise rejections

Downloads

29

Readme

async-error-boundary

A React error boundary component library for handling asynchronous errors. It allows easy management of HTTP errors and displays custom error fallbacks.

Installation

You can install the package using npm:

npm install async-error-boundary

Or if you're using yarn:

yarn add async-error-boundary

Usage

ErrorBoundary Component

Use the ErrorBoundary component to catch and handle errors occurring in its child components.

import { ErrorBoundary } from 'async-error-boundary';
import ErrorFallback from './ErrorFallback';

function App() {
  return (
    <ErrorBoundary
      fallback={ErrorFallback}
      onReset={() => { navigate('/') }}
    >
      <ApiComponent />
    </ErrorBoundary>
  );
}

ErrorBoundary Properties

| Name | Type | Required | Description | | ------------------ | --------------------------------- | -------- | ------------------------------------------ | | fallbackComponent | React.ComponentType<ErrorProps> | Yes | Fallback component to display when an error occurs | | onReset | () => void | Yes | Function to reset the error state | | children | React.ReactNode | Yes | Child components to be wrapped |

HTTPError Class

Use the HTTPError class to create and handle HTTP errors.

import { HTTPError } from 'async-error-boundary';

try {
  const response = await fetch("api/endpoint");
  if (!response.ok) {
    const errorData = await response.json();
    throw new HTTPError(response.status, errorData.message);
  }
} catch (error) {
  if (error instanceof Error) {
    throw error;
  } else {
    throw new HTTPError(400);
  }
}

HTTPError Constructor

constructor(statusCode: number, message?: string)

| Name | Type | Required | Description | | ------------ | -------- | -------- | ---------------------------------------------------------- | | statusCode | number | Yes | HTTP status code | | message | string | No | Error message (uses default error message if not provided) |

Custom Error Fallback Component

Create a custom error fallback component using the ErrorProps type. It's important to set a default value for statusCode as it may be undefined if the error is not an instance of HTTPError.

import { ErrorProps } from "async-error-boundary";

const HTTP_ERROR_MESSAGE = {
  404: {
    HEADING: "404",
    BODY: "The page you requested could not be found.",
    BUTTON: "Go to Home",
  },
  500: {
    HEADING: "Server Error",
    BODY: "An error occurred on the server. Please try again later.",
    BUTTON: "Refresh",
  },
  400: {
    HEADING: "Bad Request",
    BODY: "The request was invalid. Please check and try again.",
    BUTTON: "Go to Home",
  },
};

const ErrorFallback = ({ statusCode = 404, resetError, message }: ErrorProps) => {
  // Set a default value of 404 for statusCode.
  // This handles cases where the error is not an HTTPError and statusCode is undefined.
  const currentStatusCode = statusCode as keyof typeof HTTP_ERROR_MESSAGE;
  const { HEADING, BODY, BUTTON } = HTTP_ERROR_MESSAGE[currentStatusCode];
  
  return (
    <div>
      <h1>{HEADING}</h1>
      <p>{BODY}</p>
      <button onClick={resetError}>{BUTTON}</button>
      {message && <div>{message}</div>}
    </div>
  );
};

export default ErrorFallback;

API

<ErrorBoundary>

A React component that catches and handles errors.

Props

| Name | Type | Required | Description | | ------------------ | --------------------------------- | -------- | --------------------------------------------------------- | | fallbackComponent | React.ComponentType<ErrorProps> | Yes | Fallback component to display when an error occurs | | onReset | () => void | Yes | Function to reset the error state | | children | React.ReactNode | Yes | Child components to be wrapped |

HTTPError

A class representing HTTP errors.

Properties

| Name | Type | Description | | ------------ | -------- | --------------------------------------------- | | name | string | Always set to "HTTPError" | | statusCode | number | HTTP status code | | message | string | Error message |

Methods

| Name | Return Type | Description | | ------------- | ----------- | ------------------------------------------------------------ | | constructor | HTTPError | Creates an instance of HTTPError with a status code and an optional error message. |

ErrorProps

The type of properties passed to the error fallback component.

interface ErrorProps {
  statusCode?: number;
  resetError?: () => void;
  message?: string;
}

| Name | Type | Required | Description | | ------------- | -------------- | -------- | ------------------------------------------------------------------ | | statusCode | number | No | HTTP status code. Undefined if not provided. | | resetError | () => void | No | Function to reset the error state. Provided by ErrorBoundary. | | message | string | No | Error message. Undefined if not provided. |

Example

Using HTTPError in an API Component

import { useEffect } from "react";
import { HTTPError } from "async-error-boundary";

const DEFAULT_STATUS_CODE = 400;

const ApiComponent = () => {
  useEffect(() => {
    const fetchData = async () => {
      try {
        const response = await fetch("api/endpoint");
        if (!response.ok) {
          const errorData = await response.json();
          throw new HTTPError(response.status, errorData.message);
        }
      } catch (error) {
        if (error instanceof Error) {
          throw error;
        } else {
          throw new HTTPError(DEFAULT_STATUS_CODE);
        }
      }
    };

    fetchData();
  }, []);

  return <div>ApiComponent</div>;
};

export default ApiComponent;

In this example, errors occurring during the API call are handled using HTTPError. The ErrorBoundary will catch this error and display the appropriate error fallback.

Note

  • ErrorBoundary catches all JavaScript errors in its child components. However, it doesn't automatically catch asynchronous errors (e.g., errors in fetch requests). In such cases, you need to explicitly throw an HTTPError.
  • If the error is not an instance of HTTPError, statusCode will be undefined. Therefore, it's crucial to set a default value for statusCode in your fallback component (e.g., 404). This ensures appropriate fallback display for all types of errors.
  • While message is optional, it can be useful for providing more detailed error information to the user.
  • The resetError function is provided by ErrorBoundary and is used to reset the error state. Calling this function will execute the function passed to the onReset prop of ErrorBoundary.

Best Practices

  1. Always set a default value for statusCode in your fallback component:
const ErrorFallback = ({ statusCode = 404, resetError, message }: ErrorProps) => {
  // ...
}
  1. Whenever possible, use HTTPError to throw errors. This provides more specific error information:
throw new HTTPError(response.status, errorData.message);
  1. For unknown errors, consider throwing an HTTPError with a default status code:
throw new HTTPError(DEFAULT_STATUS_CODE);

These practices ensure consistent error handling for all types of errors.