UNPKG

universal-react-logger

Version:
143 lines (122 loc) 5.89 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _reactRouterDom = require('react-router-dom'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var ErrorBoundary = function (_React$Component) { _inherits(ErrorBoundary, _React$Component); function ErrorBoundary(props) { _classCallCheck(this, ErrorBoundary); var _this = _possibleConstructorReturn(this, (ErrorBoundary.__proto__ || Object.getPrototypeOf(ErrorBoundary)).call(this, props)); _this.state = { renderError: null, renderErrorInfo: null }; _this.resetRenderError = _this.resetRenderError.bind(_this); _this.resetEventError = _this.resetEventError.bind(_this); return _this; } _createClass(ErrorBoundary, [{ key: 'componentDidCatch', value: function componentDidCatch(renderError, renderErrorInfo) { this.setState({ renderError: renderError, renderErrorInfo: renderErrorInfo }); } }, { key: 'componentDidUpdate', value: function componentDidUpdate() { if (this.props.critical) { var body = {}; if (this.state.renderError) { body = { error: { message: this.state.renderError.toString(), stack: this.state.renderErrorInfo.componentStack } }; } if (this.props.eventError) { body = { error: { message: this.props.eventError.message, stack: 'in ' + this.props.eventError.fileName + ' - line ' + this.props.eventError.lineNumber + ' in ' + this.props.eventError.stack } }; } // send the errors to the server if (body.error) { fetch('/log-client-errors', { method: 'post', headers: { 'Accept': 'application/json', "Content-Type": "application/json" }, body: JSON.stringify(body) }); } } } }, { key: 'resetRenderError', value: function resetRenderError() { this.setState({ renderError: null, renderErrorInfo: null }); } }, { key: 'resetEventError', value: function resetEventError() { this.props.eventError = null; } }, { key: 'render', value: function render() { if (this.props.critical && this.state.renderError) { return _react2.default.createElement( 'div', null, _react2.default.createElement( _reactRouterDom.Link, { to: '/', onClick: this.resetRenderError }, 'Back to Homepage' ), _react2.default.createElement( 'h2', null, 'Something went wrong.' ) ); } if (this.props.critical && this.props.eventError) { return _react2.default.createElement( 'div', null, _react2.default.createElement( _reactRouterDom.Link, { to: '/', onClick: this.resetEventError }, 'Back to Homepage' ), _react2.default.createElement( 'h2', null, 'Something went wrong.' ) ); } // if the component error is not critical keep displaying the children return this.props.children; } }]); return ErrorBoundary; }(_react2.default.Component); exports.default = ErrorBoundary;