UNPKG

@rockpack/logger

Version:

This module can help you build error tracking & crash reporting system for your React application.

175 lines (174 loc) 7.48 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useLoggerApi = exports.LoggerContext = void 0; var react_1 = __importStar(require("react")); var valid_types_1 = require("valid-types"); var BSOD_1 = __importDefault(require("./BSOD")); var stack_1 = require("./stack"); var utils_1 = require("./utils"); var logger_1 = require("./logger"); exports.LoggerContext = react_1.createContext(null); var isBackend = function () { return typeof window === 'undefined'; }; exports.useLoggerApi = function () { var ctx = react_1.useContext(exports.LoggerContext); return { getStackData: ctx.getStackData, triggerError: ctx.onError }; }; var LoggerContainer = /** @class */ (function (_super) { __extends(LoggerContainer, _super); function LoggerContainer(props) { var _this = _super.call(this, props) || this; _this.__hasCriticalError = false; _this.handlerError = function (errorMsg, url, lineNumber, lineCount, trace) { _this.unbindActions(); if (!_this.__hasCriticalError) { _this.__hasCriticalError = true; var stackData = stack_1.onCriticalError(_this.stack, logger_1.logger.getStackCollection(), { getCurrentDate: _this.props.getCurrentDate, onPrepareStack: _this.props.onPrepareStack, }, trace, lineNumber); _this.onError(stackData); _this.setState({ bsod: true }); } }; _this._onMouseDown = function (e) { _this.stack.mousePressed = e && e.button; }; _this._onKeyDown = function (e) { _this.stack.keyboardPressed = e && e.code; }; _this._onKeyUp = function () { setTimeout(function () { _this.stack.keyboardPressed = null; }); }; _this._onMouseUp = function () { setTimeout(function () { _this.stack.mousePressed = null; }); }; _this.getStackData = function () { return stack_1.getStackData(_this.stack, logger_1.logger.getStackCollection(), { getCurrentDate: _this.props.getCurrentDate, onPrepareStack: _this.props.onPrepareStack, }); }; _this.onError = function (stackData) { if (valid_types_1.isFunction(_this.props.onError)) { _this.props.onError(stackData); } }; _this.closeBsod = function () { _this.setState({ bsod: false }); }; _this.state = { bsod: false }; _this.stack = { keyboardPressed: null, mousePressed: null, session: {}, env: {}, actions: logger_1.logger.getStackCollection().data }; var LIMIT = valid_types_1.isNumber(_this.props.limit) ? _this.props.limit : 25; logger_1.logger.setUp({ active: props.active }); logger_1.logger.getStackCollection().setLimit(LIMIT); if (valid_types_1.isFunction(_this.props.onPrepareStack)) { _this.stack.onPrepareStack = _this.props.onPrepareStack; } if (valid_types_1.isFunction(_this.props.stdout)) { logger_1.logger.setUp({ stdout: _this.props.stdout }); } _this.stack.session.start = valid_types_1.isFunction(_this.props.getCurrentDate) ? _this.props.getCurrentDate() : utils_1.getCurrentDate(); if (valid_types_1.isString(_this.props.sessionID) || valid_types_1.isNumber(_this.props.sessionID)) { _this.stack.sessionId = _this.props.sessionID; } return _this; } LoggerContainer.prototype.componentDidMount = function () { if (this.props.active && !isBackend()) { this.bindActions(); window.onerror = this.handlerError; } }; LoggerContainer.prototype.componentWillUnmount = function () { this.unbindActions(); }; LoggerContainer.prototype.bindActions = function () { document.addEventListener('mousedown', this._onMouseDown); document.addEventListener('mouseup', this._onMouseUp); document.addEventListener('keydown', this._onKeyDown); document.addEventListener('keyup', this._onKeyUp); }; LoggerContainer.prototype.unbindActions = function () { window.onerror = null; document.removeEventListener('keydown', this._onKeyDown); document.removeEventListener('keyup', this._onKeyUp); document.removeEventListener('mousedown', this._onMouseDown); document.removeEventListener('mouseup', this._onMouseUp); }; LoggerContainer.prototype.render = function () { var Bsod = react_1.isValidElement(this.props.bsod) ? this.props.bsod : BSOD_1.default; return (react_1.default.createElement(exports.LoggerContext.Provider, { value: { getStackData: this.getStackData, onError: this.onError } }, this.props.children, this.props.bsodActive && this.state.bsod && (react_1.default.createElement(Bsod, { count: logger_1.logger.getCounter(), onClose: this.closeBsod, stackData: this.getStackData() })))); }; LoggerContainer.defaultProps = { logger: logger_1.logger, active: true, bsodActive: true, sessionID: false, limit: 25, getCurrentDate: utils_1.getCurrentDate }; return LoggerContainer; }(react_1.Component)); exports.default = LoggerContainer;