UNPKG

web-dev-server

Version:

Node.js simple http server for common development or training purposes.

143 lines 5.74 kB
Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorsHandler = void 0; var Server_1 = require("../Server"); var StringHelper_1 = require("../Tools/Helpers/StringHelper"); var ErrorsHandler = /** @class */ (function () { function ErrorsHandler(server, cache) { // development purposes: this.request = null; this.response = null; this.server = server; this.register = cache; this.initErrorsHandlers(); } ErrorsHandler.prototype.SetHandledRequestProperties = function (req, res) { this.request = req; this.response = res; return this; }; /** * @summary Print error in command line a little more nicely or log error by custom error log handler: */ ErrorsHandler.prototype.LogError = function (e, code, req, res) { var development = this.server.IsDevelopment(), customErrorHandler = this.server.GetErrorHandler(), noErrorHandler = customErrorHandler === null, errorText = (development || noErrorHandler) ? this.renderErrorText(e) : ''; if (noErrorHandler) { if (development) console.info("\n"); console.error(errorText); if (development) console.info("\n"); } else { try { customErrorHandler(e, code, req, res); } catch (e1) { if (development) console.info("\n"); console.error(e1.message); if (development) console.info("\n"); } } return this; }; /** * @summary Print exception in command line a little more nicely in response: */ ErrorsHandler.prototype.PrintError = function (e, code, req, res) { if (code === void 0) { code = 500; } if (req === void 0) { req = null; } if (res === void 0) { res = null; } var development = this.server.IsDevelopment(), errorText = development ? this.renderErrorText(e) : ''; if (!res || (res && res.IsSent())) return this; if (!res.IsSentHeaders()) { res.SetHeader('Content-Type', development ? 'text/plain; charset=utf-8' : 'text/html; charset=utf-8'); res.SendHeaders(code); } if (development) { res.SetBody("/*\n" + errorText + "\n*/"); } else { if (code == 404) { var headerCode = Server_1.Server.DEFAULTS.RESPONSES.CODES.HEADER_NOT_FOUND .replace('%path%', StringHelper_1.StringHelper.HtmlEntitiesEncode(req.GetBaseUrl() + req.GetBasePath())); var outputStr = Server_1.Server.DEFAULTS.RESPONSES.CODES.HTML .replace('%head%', Server_1.Server.DEFAULTS.RESPONSES.CODES.HEAD_NOT_FOUND) .replace('%body%', headerCode); } else { var outputStr = Server_1.Server.DEFAULTS.RESPONSES.CODES.HTML .replace('%head%', Server_1.Server.DEFAULTS.RESPONSES.CODES.HEAD_ERROR) .replace('%body%', Server_1.Server.DEFAULTS.RESPONSES.CODES.HEADER_ERROR); } res.SetBody(outputStr); } res.Send(); return this; }; /** * @summary Initialize uncatch error and uncatch warning handlers */ ErrorsHandler.prototype.initErrorsHandlers = function () { var _this = this; /** @var process NodeJS.Process */ process.on('uncaughtException', this.handleUncatchError.bind(this, true)); process.on('warning', this.handleUncatchError.bind(this, false)); process.on('unhandledRejection', function (reason, promise) { if (reason instanceof Error) { _this.handleUncatchError(true, reason); } else { var reasonMsg; try { reasonMsg = JSON.stringify(reason); } catch (e1) { reasonMsg = reason.toString(); } try { throw new Error(reasonMsg); } catch (e2) { _this.handleUncatchError(true, e2); } } }); }; /** * @summary Clear all modules on any uncatched error */ ErrorsHandler.prototype.handleUncatchError = function (clearRequireCache, error) { this .LogError(error, 500, this.request, this.response) .PrintError(error, 500, this.request, this.response); var development = this.server.IsDevelopment(); if (development && clearRequireCache) { this.register.StopAll(); } }; /** * @summary Render error as text for development purposes: */ ErrorsHandler.prototype.renderErrorText = function (e) { if (e === void 0) { e = null; } if (!e || !e.stack) return ''; var documentRoot = this.server.GetDocumentRoot(), stackLines = e.stack.replace(/\r/g, '').split("\n"), stackLine; for (var i = 1, l = stackLines.length; i < l; i++) { stackLine = stackLines[i].replace(/\\/g, '/'); if (stackLine.indexOf(documentRoot) > -1) stackLines[i] = stackLine.replace(documentRoot, ''); } return stackLines.join("\n"); }; return ErrorsHandler; }()); exports.ErrorsHandler = ErrorsHandler; //# sourceMappingURL=Error.js.map