diff options
Diffstat (limited to 'MistyCore/error.js')
-rw-r--r-- | MistyCore/error.js | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/MistyCore/error.js b/MistyCore/error.js new file mode 100644 index 0000000..fd5e2b2 --- /dev/null +++ b/MistyCore/error.js @@ -0,0 +1,155 @@ +const path = require('path'); +const fs = require('fs'); +const crypto = require('crypto'); +const chalk = require("chalk"); + +let fixLength = (string, length) => { + let end = " ".repeat(length); + if (string.length > length) return string.substring(0, length); + + return string + end.substring(0, length - string.length); +} + +let hexEight = (dec) => { + let hex = Math.round(dec).toString(16); + let zero = "00000000"; + return zero.substring(0, 8 - hex.length) + hex; +} + +let errorName = (name) => { + switch (name) { + case "InternalError": + case "Error": + return "TRAP_CAUSE_UNKNOWN"; + + case "RangeError": + return "OUT_OF_RANGE_EXCEPTION"; + + case "ReferenceError": + return "MEMORY_MANAGEMENT"; + + case "SyntaxError": + return "INSTRUCTION_COHERENCY_EXCEPTION"; + + case "TypeError": + return "NO_USER_MODE_CONTEXT"; + + case "URIError": + return "INTERRUPT_UNWIND_ATTEMPTED"; + + default: + return null; + } +} + +module.exports = (error, currentStage, version) => { + let lines = error.stack.split("\n").map(i => i.trim()); + let dump = lines.map(i => i.substring(3)); dump.shift(); + let name = lines[0].split(":")[0]; + let baseCodes = [...Buffer.from(name).toString("hex").match(/.{1,8}/g).map(i => parseInt(i, 16)).map(i => hexEight(i).toUpperCase()), "00000000", "00000000", "00000000", "00000000"]; + + let addressesRaw = dump.filter(i => i.includes("(")).map(i => i.split("(")[1].split(")")[0].trim()).map((i) => { + if (i.startsWith("node:internal/")) { + if (i.startsWith("node:internal/modules/")) { + return "__" + i.substring(21); + } else { + return "_" + i.substring(13); + } + } else { + return i; + } + }); + let addressesFull = [...new Set(dump.filter(i => i.includes("(")).map(i => i.split("(")[1].split(")")[0].trim()).map(i => i.split(":")[0]))]; + + let bases = addressesFull.map((i) => { + if (fs.existsSync(i)) { + try { + fs.accessSync(i, fs.constants.R_OK); + return crypto.createHash("sha256").update(fs.readFileSync(i)).digest("hex").substring(0, 8); + } catch (e) { + return "ffffffff"; + } + } else { + return "ffffffff"; + } + }); + let dateStamps = addressesFull.map((i) => { + if (fs.existsSync(i)) { + try { + fs.accessSync(i, fs.constants.R_OK); + return hexEight(Math.round((new Date().getTime() / 1000) - (fs.lstatSync(i).mtimeMs / 1000))); + } catch (e) { + return "ffffffff"; + } + } else { + return "ffffffff"; + } + }); + + let fileNamesFull = addressesFull.map(i => path.basename(i.split(":")[0])); + + let fileNames = addressesRaw.map(i => path.basename(i.split(":")[0])); + let dumpsRaws = dump.filter(i => i.includes("(")).map(i => i.split("(")[0].trim()); + + let addresses = addressesRaw.map((i) => { + let hex = Buffer.from(i).toString("hex"); + + return hex.substring(0, 4) + hex.substring(hex.length - 4, hex.length); + }); + + let dumps = dumpsRaws.map((i) => { + let arr = ["00000000", "00000000", "00000000", "00000000", "00000000", "00000000"]; + let hex = Buffer.from(i).toString("hex"); + + arr.unshift(hex.substring(0, 8), hex.substring(8, 16), hex.substring(16, 24), hex.substring(24, 32), hex.substring(32, 40), hex.substring(40, 48)); + arr = arr.filter(i => i.trim() !== "").map(i => hexEight(parseInt(i, 16))); + + return arr; + }); + + console.log(chalk.bgYellow.white("*** STOP: 0x" + hexEight(process.uptime()).toUpperCase() + " (0x" + baseCodes[0] + ",0x" + baseCodes[1] + ",0x" + baseCodes[2] + ",0x" + baseCodes[3] + ")")); + + let pName = errorName(name); + if (pName) { + console.log(chalk.bgYellow.white(pName)); + } + + console.log(chalk.bgYellow.white("")); + console.log(chalk.bgYellow.white("CPUID:" + (require('os').cpus()[0] ?? { model: "???" }).model + " SYSVER 0x" + hexEight(parseInt(Buffer.from(version.replace(/(\.| \(.*\))/gm, "")).toString("hex"), 16)))); + + console.log(chalk.bgYellow.white("")); + console.log(chalk.bgYellow.white("Sys Base DateStmp - Name Sys Base DateStmp - Name")); + + for (let indexes of [ + [0, 1], + [2, 3], + [4, 5], + [6, 7], + [8, 9], + [10, 11] + ]) { + if (bases[indexes[0]] && bases[indexes[1]]) { + console.log(chalk.bgYellow.white(bases[indexes[0]], dateStamps[indexes[0]], "-", fixLength(fileNamesFull[indexes[0]], 18), bases[indexes[1]], dateStamps[indexes[1]], "-", fixLength(fileNamesFull[indexes[1]], 18))); + } else if (bases[indexes[0]]) { + console.log(chalk.bgYellow.white(bases[indexes[0]], dateStamps[indexes[0]], "-", fixLength(fileNamesFull[indexes[0]], 18))); + } else { + console.log(chalk.bgYellow.white("")); + } + } + + console.log(chalk.bgYellow.white("")); + console.log(chalk.bgYellow.white("Address dword dump - Name")); + + for (let index in addresses) { + let address = addresses[index]; + let dump = dumps[index]; + let fileName = fileNames[index]; + + console.log(chalk.bgYellow.white(address, dump[0], dump[1], dump[2], dump[3], dump[4], dump[5], "-", fileName)); + } + + console.log(chalk.bgYellow.white("")); + console.log(chalk.bgYellow.white("Restart and press Shift on startup to access recovery options")); + console.log(chalk.bgYellow.white("or read the system journal information. If this message reappears,")) + console.log(chalk.bgYellow.white("contact your system administrator or technical support group.")) +}
\ No newline at end of file |