Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * @file this is for react error handling to help encapsulate errors
 *
 * @spec
 * https://github.com/v8/v8/wiki/Stack-Trace-API
 *
 * @discussions
 * https://github.com/facebook/react/issues/2461#issuecomment-158787924
 * https://github.com/facebook/react/pull/5615
 * https://github.com/skiano/react-safe-render/blob/feature/safe-methods/index.js
 * https://docs.google.com/document/d/1rda3yKGHimKIhg5YeoAmCOtyURgsbTH_qaYR79FELlk/edit#heading=h.sdpzjcyw06a5
 *
 * @libs
 * https://www.stacktracejs.com/
 * https://github.com/felixge/node-stack-trace
 */
const react_1 = __importDefault(require("react"));
const chain_able_boost_1 = require("chain-able-boost");
const exotic_1 = require("exotic");
const view_container_1 = require("view-container");
// override of exotic
const isNotEmpty = x => exotic_1.isString(x) && !exotic_1.isEmpty(x) && x.trim() !== '.';
// @todo clean this out
let errorLines = 0;
const toFormattedErrorLines = error => {
    const listItems = error
        .split(/((\n+)|(\.\s+))/)
        .filter(isNotEmpty)
        .map(line => {
        return (react_1.default.createElement("li", { key: (errorLines += 1 + '@error') },
            react_1.default.createElement("small", null, line)));
    });
    // is ordered but numbers don't work well
    return react_1.default.createElement("ul", null, listItems);
};
const styles = view_container_1.fromStringToStyle(`
  section {
    padding: 1rem;
  }

  ul {
    color: rgba(0, 0, 0, .3);
  }

  h2 {
    padding: 1rem;
    color: black;
    background-color: red;
  }

  pre {
    background-color: #fefefe;
    border: 2px solid #aeaeae;
    font: monospace;
  }
`);
/**
 * @tutorial https://reactjs.org/blog/2017/07/26/error-handling-in-react-16.html
 * @see https://github.com/commissure/redbox-react
 * @see https://github.com/bvaughn/react-error-boundary
 */
class ErrorBoundary extends react_1.default.Component {
    constructor() {
        super(...arguments);
        this.state = {
            hasError: false,
            error: undefined,
            stack: undefined,
        };
    }
    componentDidCatch(error, info = exotic_1.EMPTY_OBJ) {
        const debugStack = exotic_1.isFunction(window.debuggable)
            ? window.debuggable(error)
            : error;
        // const debugStack = window.debuggable.stackAt(7)
        const stack = chain_able_boost_1.stringify(debugStack, null, 2);
        console.warn(debugStack);
        console.log(error, info);
        this.setState({ hasError: true, error, stack, info });
    }
    render() {
        const { name } = this.props;
        const { error, hasError, stack, info } = this.state;
        if (hasError === true) {
            // JSON.stringify(error, null, 2)
            const errorView = error.message ? error.message : error.stack;
            return (react_1.default.createElement("div", null,
                styles,
                react_1.default.createElement("section", null,
                    react_1.default.createElement("h1", null,
                        name,
                        " Component Error"),
                    react_1.default.createElement("h2", null, "PLEASE CHECK YOUR DEV CONSOLE"),
                    react_1.default.createElement("div", null, toFormattedErrorLines(errorView)),
                    react_1.default.createElement("pre", null,
                        react_1.default.createElement("code", null, info.componentStack)),
                    react_1.default.createElement("pre", null, stack))));
        }
        return this.props.children;
    }
}
exports.ErrorBoundary = ErrorBoundary;
function withErrorBoundary() {
    return function withComponent(Component) {
        return function renderWithErrorBoundaryWithComponent(props) {
            return (react_1.default.createElement(ErrorBoundary, null,
                react_1.default.createElement(Component, Object.assign({}, props))));
        };
    };
}
exports.withErrorBoundary = withErrorBoundary;
exports.default = ErrorBoundary;
//# sourceMappingURL=ErrorBoundary.js.map