{"size": 1489, "ext": "js", "max_stars_repo_path": "assets/src/edit-story/components/errorBoundary/index.js", "max_stars_repo_name": "sawravchy/web-stories-wp", "max_stars_repo_stars_event_min_datetime": "2020-06-28T18:11:05.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-05T10:17:30.000Z", "max_issues_repo_path": "assets/src/edit-story/components/errorBoundary/index.js", "max_issues_repo_name": "sawravchy/web-stories-wp", "max_issues_repo_issues_event_min_datetime": "2021-08-31T23:19:24.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-02T11:15:25.000Z", "max_forks_repo_path": "assets/src/edit-story/components/errorBoundary/index.js", "max_forks_repo_name": "sawravchy/web-stories-wp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 26.1228070175, "max_line_length": 89, "alphanum_fraction": 0.7018132975}
/*
 * Copyright 2020 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * External dependencies
 */
import { Component } from 'react';
import PropTypes from 'prop-types';

/**
 * Internal dependencies
 */
import ErrorActions from './errorActions';

class ErrorBoundary extends Component {
  static propTypes = {
    children: PropTypes.node.isRequired,
  };

  state = {
    error: null,
    errorInfo: null,
  };

  componentDidCatch(error, errorInfo) {
    this.setState({ error, errorInfo });
    // In the future we might take some action here (report to 3P service with tree info)
  }

  render() {
    const { error, errorInfo } = this.state;
    if (error) {
      return <ErrorActions error={error} errorInfo={errorInfo} />;
    }
    return this.props.children;
  }
}

const shouldDisableErrorBoundaries =
  process.env.DISABLE_ERROR_BOUNDARIES === 'true';
export default shouldDisableErrorBoundaries
  ? ({ children }) => children
  : ErrorBoundary;
