You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.1 KiB
JavaScript

'use strict';
const fs = require("fs");
const os = require("os");
const path = require("path");
const unhandledError = require("unhandled-error");
const jsonStringifySafe = require("json-stringify-safe");
const envName = require("env-name");
const envInfo = require("env-info");
const serializeError = require("./serialize-error");
module.exports = function createErrorReporter(errorPath, options = {}) {
let unhandledErrorHandler = unhandledError((error, context) => {
console.log(context);
let stringifiedErrorData = jsonStringifySafe({
error: serializeError(error),
context: context,
environmentName: envName(),
environmentInfo: envInfo(),
hostname: os.hostname()
}, undefined, "\t", () => {});
let errorFilename = `${Date.now()}_${Math.floor(Math.random() * 100000)}.json`;
fs.writeFileSync(path.join(errorPath, errorFilename), stringifiedErrorData);
if (options.handler != null) {
options.handler(error, context);
}
}, {doNotCrash: options.doNotCrash});
return {
report: unhandledErrorHandler.report.bind(unhandledErrorHandler)
}
}