question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a way to throw error (maybe asynchronous) that can be handled by componentDidCatch

See original GitHub issue

Do you want to request a feature or report a bug? Feature request.

What is the current behavior? componentDidCatch can only handle synchronous error.

What is the expected behavior? The capability of error boundaries to handle asynchronous error.

The error boundaries example in the docs is very similar with how an asynchronous error is handled in React.

class WithAsyncData extends React.Component {
  constructor(props) {
    super(props);
    this.state = { error: null, value: null };
  }

  // using componentDidMount instead of componentDidCatch
  componentDidMount() {
    doAsynchronousThing()
      .then(value => this.setState({ value }))
      .catch(error => this.setState({ error }))
  }

  render() {
    if (this.state.error) {
      return <h1>An error occured.</h1>;
    }
    return this.props.children;
  }
}

Unfortunately, the new componentDidCatch lifecycle only aware of synchronous error. I think, it will be good to provide a way to throw an error asynchronously, like:

class WithAsyncData extends React.Component {
  constructor(props) {
    super(props);
    this.state = { error: null, value: null };
  }

  componentDidMount() {
    doAsynchronousThing()
      .then(value => this.setState({ value }))
      .catch(error => this.throwError(error)) // throwing error asynchronously
  }

  render() {
    if (this.state.error) {
      return <h1>An error occured.</h1>;
    }
    return this.props.children;
  }
}

With that, a network error can be handled in single place with a NetworkErrorBoundary component for example.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

25reactions
gaearoncommented, Aug 31, 2018

I think

this.setState(() => { throw err })

might do what you’re looking for

1reaction
f-riccicommented, Aug 6, 2021

@wise-introvert @LinusU

I have found this solution in another issue on this repository but unfortunately cannot remember the author. I’m sorry for that, all credits belong to the original author.

import { useState, useCallback } from 'react'

export function useCrash() {
  const [, setState] = useState()
  return useCallback(
    (err) =>
      setState(() => {
        throw err
      }),
    [],
  )
}

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catching Asynchronous Errors in React using Error Boundaries
Just use setState . Throw inside the callback. See how errors are caught by your Error Boundaries. Let's extract this snippet to a...
Read more >
Handling JavaScript errors in React with error boundaries
Use error boundaries in React components to handle JavaScript errors ... UI after an error has been thrown, while componentDidCatch() should ...
Read more >
React error boundaries with useEffect and async function ...
Use componentDidCatch() to log error information. ... In your async function catch your exception and throw it inside a react hook callback.
Read more >
Error Boundaries - React
Asynchronous code (e.g. setTimeout or requestAnimationFrame callbacks); Server side rendering; Errors thrown in the error boundary itself (rather than its ...
Read more >
How to Handle Errors in React - AppSignal Blog
const fetchData = async () => { try { return await fetch("https://some-url-that-might-fail.com"); } catch (error) { console.error(error); // You ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found