UNPKG

ajsfw

Version:
112 lines (111 loc) 4.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ui = require("ajsfw/ui"); var exceptions_1 = require("ajsfw/exceptions"); var mapFile_1 = require("./mapFile"); function errorHandler(exceptionOrErrorEvent) { "use strict"; function printException(exception, stackString) { var exc = ""; exc += "<h>" + exception.name + "</h4>"; if (exception.message) { exc += "<h5>Message:</h5>"; exc += "<p>" + exception.message + "</p>"; } if (stackString) { exc += "<h5>Stack trace:</h5>"; exc += "<pre>" + stackString + "</pre>"; } return exc; } function getStackString(stack) { if (stack === undefined || stack === null) { return Promise.resolve(""); } var ss = ""; var si; var promises = []; si = stack; while (si !== null) { promises.push(mapFile_1.mapFile(si.file, si.line, si.character)); si = si.child; } return new Promise(function (resolve, reject) { Promise.all(promises).then(function (mappedPositions) { si = stack; var c = 0; while (si !== null) { ss += si.caller + " at "; ss += "<a href=\"showfile.html?file=" + si.file + "&line=" + si.line + "\" target=\"blank\">"; ss += si.file + ":" + si.line + ":" + si.character; ss += "</a>"; if (mappedPositions[c].source !== null) { ss += " mapped to <a href=\"/showfile.html?file=" + mappedPositions[c].source + "&line=" + mappedPositions[c].line + "\" target=\"blank\">"; ss += mappedPositions[c].source + ":" + mappedPositions[c].line + ":" + mappedPositions[c].column; ss += "</a>"; } ss += "\n"; si = si.child; c++; } resolve(ss); }); }); } var exception; if (exceptionOrErrorEvent instanceof exceptions_1.Exception) { exception = exception; } if (exceptionOrErrorEvent instanceof Event) { exception = new exceptions_1.Exception(exceptionOrErrorEvent.error); } while (exception.parentException !== null) { exception = exception.parentException; } var name = exception.name; if (name.substr(0, 4) === "new ") { name = name.substr(4); } getStackString(exception.stack) .then(function (stackString) { var epc = { label: "Ajs Framework unhandled exception", errorCode: "", errorLabel: name, errorMessage: exception.message ? exception.message : "", errorTrace: stackString, userAction: "", }; if (!ui.ErrorScreen.show(epc)) { var err = ""; err += "<div style=\"font-family: Arial\">"; err += "<h1>Ajs Framework</h1>"; err += "<h2>Unhandled exception occured</h2>"; err += "<p>"; err += "<span "; err += "style =\"display: table-cell; border: solid 1px black; padding: 0.5em; background-color: #FFFBE6\">"; err += "<small>This message is not suppoesed to be shown on the production environments. "; err += "To hide sensitive data use the showErrors configuration option of the Ajs Framework "; err += "or configure a HTML error page to be shown in case of unhandled error!</small>"; err += "</span></p>"; err += printException(exception, stackString); err += "</div>"; var errdiv = document.createElement("div"); errdiv.style.position = "absolute"; errdiv.style.left = "0px"; errdiv.style.top = "0px"; errdiv.style.bottom = "0px"; errdiv.style.width = "100%"; errdiv.style.boxSizing = "border-box"; errdiv.style.zIndex = "9000"; errdiv.style.backgroundColor = "white"; errdiv.style.opacity = "0.9"; errdiv.style.padding = "0.5em"; errdiv.style.overflow = "auto"; errdiv.innerHTML = err; document.body.appendChild(errdiv); } }); } exports.errorHandler = errorHandler;