UNPKG

@qualweb/core

Version:
57 lines 2.04 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorManager = void 0; const path_1 = __importDefault(require("path")); const fs_1 = require("fs"); class ErrorManager { logOptions; timestamp = new Date().getTime(); fileName = `qualweb-errors-${this.timestamp}.log`; error = false; constructor(logOptions) { this.logOptions = logOptions; const formattedDate = new Date(this.timestamp).toISOString().replace(/T/, ' ').replace(/\..+/, ''); this.writeErrorToFile('Evaluation errors', `${formattedDate}\n-----------`); } handle(cluster) { cluster?.on('taskerror', (err, data) => { this.error = true; this.writeErrorToFile(data.url, `${err.message}\n-----------`); }); } writeErrorToFile(url, message) { if (this.logOptions?.file) { const _path = path_1.default.resolve(process.cwd(), this.fileName); const data = url + ' : ' + message + '\n'; (0, fs_1.writeFile)(_path, data, { flag: 'a', encoding: 'utf-8' }, (err) => { if (err) console.error(err); }); } if (this.logOptions?.console) { console.error(url + ' : ' + message + '\n'); } } showErrorsIfAny() { if (this.logOptions?.file) { if (this.error) { console.warn('One or more urls failed to evaluate. Check the error.log for more information.'.yellow); } else { this.deleteErrorLogFile(); } } } deleteErrorLogFile() { (0, fs_1.unlink)(path_1.default.resolve(process.cwd(), this.fileName), (err) => { if (err) { throw err; } }); } } exports.ErrorManager = ErrorManager; //# sourceMappingURL=ErrorManager.object.js.map