-2

I am using sentry to track crashing report and I have setup my project on top of react but I am configuring it by Gatsby and I have added the plugin in correct way in gatsby.js file but on creating issue in any one of the component it is not reflecting on sentry issue and I am not able to find the reason of this problem. Please help me out

I am putting error-boundary.js file inside my component folder and here is the code of that


    import React from 'react';
    import { ErrorBoundary } from '@sentry/react';
    import { Link } from 'gatsby';
    
    const FallBack = ({ error, resetError }) => (
        <div role="alert">
            <p>Something went wrong:</p>
            <pre>{error.message}</pre>
            <Link to="/" onClick={resetError}>
                <a>Try again</a>
            </Link>
        </div>
    );
    
    const ErrorBoundaryContainer = ({ children }) => {
        return <ErrorBoundary fallback={FallBack}>{children}</ErrorBoundary>;
    };
    
    export default ErrorBoundaryContainer;

and then I am importing it in gatsby-browser.js file


        import React from 'react'
        import ErrorBoundaryContainer from './src/components/error-boundary'
    
        export const wrapPageElement = ({ element }) => (
          <ErrorBoundaryContainer>
        {element}
          </ErrorBoundaryContainer>
    ) 

and then I am creating a case in one of my component to throw error for testing,

 

    const [toggle, toggleError] = React.useState(false);
      if (toggle) {
            console.error('console error');
            throw new Error('I crashed!');
        }
    <button type="button" onClick={() => toggleError(true)}>
                        Break the world
                    </button>

  • Can you provide at least how are you trying to set the error boundary? – Ferran Buireu Sep 21 '21 at 04:49
  • i am following this https://chrisotto.dev/gatsby-error-monitoring-with-sentry/ to setup error boundary but i am not able to understand that how to put it up in layout – Ashutosh rai Sep 21 '21 at 07:18
  • Can you provide **your** implementation rather than the guide you've followed? What have you tried? Where's the code that is not working? You've **only** shared a screenshot of your Sentry dashboard, how do you think we can help you with the information provided? – Ferran Buireu Sep 21 '21 at 07:20
  • @FerranBuireu i have updated questions with the code that i have written in my project – Ashutosh rai Sep 21 '21 at 08:28

1 Answers1

1

There are a few things to comment here:

  • wrapPageElement is a shared API between gatsby-browser.js and gatsby-ssr.js so if you use it, you must place the same snippet in both files

  • I'm assuming you used @sentry/gatsby as a dependency, as the docs suggests

  • The component that is throwing the error should contain a useEffect, otherwise, it will never throw the error. It's a React mistake, not Gatsby.

         const [toggle, toggleError] = React.useState(false);
    
         React.useEffect(()=>{
           if (toggle) {
              console.error('console error');
              throw new Error('I crashed!');
           }
         }, [toggle])
    
      <button type="button" onClick={() => toggleError(!toggle)}>Break the world </button>
    

    Note: if it's a toggle, it should change to the opposite value, isn't it? Tweak it as you wish of course

    The useEffect hook with a toggle dependencies ([toggle]) will be fired each change the toggle value changes. In your snippet, because initially is set to false (React.useState(false)) your error was never thrown. It's like saying "Ok, when my deps value (toggle in this case) changes, fire what I have inside".

Ferran Buireu
  • 28,630
  • 6
  • 39
  • 67
  • I have made these changes but still issue is not getting listed in sentry and I am not able to find out what else is missing – Ashutosh rai Sep 21 '21 at 12:43
  • If you are not able to find out imagine the rest of us xD. Can you debug a little bit? Can you ensure that the `useEffect` is being fired and the `div role="alert"` is surrounding the HTML? – Ferran Buireu Sep 21 '21 at 13:26
  • 1
    finally it is working now, thankyou for your guidance and support cheers! – Ashutosh rai Sep 22 '21 at 07:32