Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance AppInsightsErrorBoundary to Pass Error Details to onError Component #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

floccini
Copy link

This PR introduces an enhancement to the AppInsightsErrorBoundary component by providing access to the error and errorInfo objects in the onError component, allowing developers to handle errors in a more customized way.

  • Updated setState to include the error and errorInfo in the state when an error is caught.
  • Passed those two values as props to the onError component, allowing developers to easily extract that information.

Usage example:

type ErrorBoundary = {
    error: Error | null;
    errorInfo: React.ErrorInfo | null;
};

const handleError = ({ error, errorInfo }: ErrorBoundary) => {
        if (error?.message === 'Session expired') {
             // Assuming a session expiration error was thrown somewhere in our application
            return (
                <h1>
                    Session expired, redirecting to the authentication page...
                </h1>
            );
        }

        return (
            <h1>
                An unknown error occurred while performing the operation.
            </h1>
        );
    };
    
    
<AppInsightsErrorBoundary appInsights={reactPlugin} onError={handleError}>
     ...
</AppInsightsErrorBoundary>;

This enhancement allows developers to customize the component displayed in response to an error by providing access to the error details. This is something that I needed at work and I also found someone who also needed it in this Stack Overflow question.

@floccini
Copy link
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant