logify-alert
Version:
Node.js client for Logify Alert
111 lines (91 loc) • 4.34 kB
JavaScript
;
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 _nodeCollector = require("./collectors/nodeCollector.js");
var _nodeCollector2 = _interopRequireDefault(_nodeCollector);
var _customDataCollector = require("./collectors/customDataCollector.js");
var _customDataCollector2 = _interopRequireDefault(_customDataCollector);
var _tagsCollector = require("./collectors/tagsCollector.js");
var _tagsCollector2 = _interopRequireDefault(_tagsCollector);
var _nodeReportSender = require("./reportSender/nodeReportSender.js");
var _nodeReportSender2 = _interopRequireDefault(_nodeReportSender);
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"); } }
var logifyAlert = function () {
function logifyAlert(apiKey) {
_classCallCheck(this, logifyAlert);
this._apiKey = apiKey;
this._handleReports = false;
this.applicationName = undefined;
this.applicationVersion = undefined;
this.userId = undefined;
this.customData = undefined;
this.tags = undefined;
this.beforeReportException = undefined;
this.afterReportException = undefined;
}
_createClass(logifyAlert, [{
key: "startHandling",
value: function startHandling() {
var _this = this;
if (this._handleReports) return;
this._handleReports = true;
process.on('uncaughtException', function (error) {
if (_this._handleReports) {
_this.sendException(error);
}
});
process.on('unhandledRejection', function (reason, promise) {
if (_this._handleReports) {
_this.sendRejection(reason);
}
});
}
}, {
key: "stopHandling",
value: function stopHandling() {
this._handleReports = false;
}
}, {
key: "sendException",
value: function sendException(error) {
this.callBeforeReportExceptionCallback();
var collector = this.createCollector();
collector.handleException(error);
this.sendReportCore(collector.reportData);
}
}, {
key: "sendRejection",
value: function sendRejection(reason) {
this.callBeforeReportExceptionCallback();
var collector = this.createCollector();
collector.handleRejection(reason);
this.sendReportCore(collector.reportData);
}
}, {
key: "sendReportCore",
value: function sendReportCore(reportData) {
var sender = new _nodeReportSender2.default();
sender.sendReport(this._apiKey, reportData, this.afterReportException);
}
}, {
key: "createCollector",
value: function createCollector() {
var collector = new _nodeCollector2.default();
collector.applicationName = this.applicationName;
collector.applicationVersion = this.applicationVersion;
collector.userId = this.userId;
collector.collectors.push(new _customDataCollector2.default(this.customData));
collector.collectors.push(new _tagsCollector2.default(this.tags));
return collector;
}
}, {
key: "callBeforeReportExceptionCallback",
value: function callBeforeReportExceptionCallback() {
if (this.beforeReportException != undefined) {
this.customData = this.beforeReportException(this.customData);
}
}
}]);
return logifyAlert;
}();
module.exports = logifyAlert;