All Downloads are FREE. Search and download functionalities are using the official Maven repository.

web.lib.core.components.sentry-boundary.tsx Maven / Gradle / Ivy

There is a newer version: 37
Show newest version
import * as React from 'react';
import * as Sentry from '@sentry/browser';
import { Button } from 'react-bootstrap';

interface SentryBoundaryProps {
  sentryDSN?: string;
  environment: string;
  children?: React.ReactNode;
}

export class SentryBoundary extends React.Component {
  constructor(props: SentryBoundaryProps) {
    super(props);
    this.state = { error: undefined };

    if (props.sentryDSN) {
      console.info('Sentry is enabled');
      Sentry.init({
        dsn: props.sentryDSN,
        environment: props.environment,
      });
    } else {
      console.info('Sentry is disabled');
    }
  }

  componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
    if (this.props.sentryDSN) {
      this.setState({ error });
      Sentry.withScope(scope => {
        Object.keys(errorInfo).forEach(key => {
          // @ts-ignore
          scope.setExtra(key, errorInfo[key]);
        });
        Sentry.captureException(error);
      });
    }
  }

  render() {
    if (this.state.error) {
      return (
        
      );
    } else {
      return <>{this.props.children};
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy