reactjsgatsbysentrygatsby-plugin

i have created a project in sentry on react but configuring it on gatsby is it a correct way to do?


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>


Solution

  • There are a few things to comment here: