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

@os-design/error-boundary

v1.0.20

Published

The Error Boundary component. Fully supports Relay (useQueryLoader/loadQuery, useLazyLoadQuery).

Downloads

75

Readme

@os-design/error-boundary NPM version BundlePhobia

The Error Boundary component. Fully supports Relay (useQueryLoader/loadQuery, useLazyLoadQuery).

Error Boundary allows you to catch JS errors that occur in the UI. This package is a wrapper that allows you to wrap your components to catch UI errors that may occur inside them.

Installation

Install the package using the following command:

yarn add @os-design/error-boundary

Usage

Let's assume that we have a component that sometimes throw the error.

import React, { useState } from 'react';

const BadComponent: React.FC = () => {
  const [hasError, setHasError] = useState(false);

  if (hasError) {
    throw new Error('Something happened');
  }

  return (
    <>
      <p>I am working!</p>
      <button onClick={() => setHasError(true)}>Throw the error</button>
    </>
  );
};

export default BadComponent;

To catch errors that may occur inside the BadComponent, wrap it with the ErrorBoundary as follows:

import React from 'react';
import ErrorBoundary from '@os-design/error-boundary';

const App: React.FC = () => (
  <ErrorBoundary fallback={<h1>An error occured</h1>}>
    <BadComponent />
  </ErrorBoundary>
);

export default App;

Getting an error

Most likely, you want to print an error message or render the fallback component, depending on the error type (for example, render some component, if there is no network connection).

In this case, you can pass a function to the fallback and get an error like this:

import React from 'react';
import ErrorBoundary from '@os-design/error-boundary';

const App: React.FC = () => (
  <ErrorBoundary fallback={({ error }) => <h1>{error.message}</h1>}>
    <BadComponent />
  </ErrorBoundary>
);

export default App;

Retrying after an error

Let's assume that you have a component that throws the «No error connection» error. You successfully render another component with the error message, but what if the user has already restored the Internet connection?

In this case, you also need to render the retry button. When you click on it, the component will try to render again. You can do it in the following way:

import React from 'react';
import ErrorBoundary from '@os-design/error-boundary';

const App: React.FC = () => (
  <ErrorBoundary
    fallback={({ error, retry }) => (
      <div>
        <h1>{error.message}</h1>
        <button onClick={retry}>Retry</button>
      </div>
    )}
  >
    <BadComponent />
  </ErrorBoundary>
);

export default App;

Using with useQueryLoader / loadQuery

As Relay documentation says:

When using useQueryLoader/loadQuery to fetch a query, in order to retry after an error has occurred, you can call loadQuery again and pass the new query reference to usePreloadedQuery.

You can do it as follows:

import React from 'react';
import ErrorBoundary from '@os-design/error-boundary';

const App: React.FC = ({ initialQueryRef }) => {
  const [queryRef, loadQuery] = useQueryLoader(query, initialQueryRef);

  return (
    <ErrorBoundary
      fallback={({ error, retry }) => (
        <div>
          <h1>{error.message}</h1>
          <button
            onClick={() => {
              loadQuery(/* ... */);
              retry();
            }}
          >
            Retry
          </button>
        </div>
      )}
    >
      <MainContent queryRef={queryRef} />
    </ErrorBoundary>
  );
};

export default App;

Using with useLazyLoadQuery

As Relay documentation says:

When using useLazyLoadQuery to fetch a query, in order to retry after an error has occurred, you can attempt to re-mount and re-evaluate the query component by passing it a new fetchKey.

You can do it as follows:

import React from 'react';
import ErrorBoundary from '@os-design/error-boundary';

const App: React.FC = ({ initialQueryRef }) => {
  const [queryRef, loadQuery] = useQueryLoader(query, initialQueryRef);

  return (
    <ErrorBoundary
      fallback={({ error, retry }) => (
        <div>
          <h1>{error.message}</h1>
          <button onClick={retry}>Retry</button>
        </div>
      )}
    >
      {(fetchKey) => <MainContent fetchKey={fetchKey} />}
    </ErrorBoundary>
  );
};

export default App;