summaryrefslogtreecommitdiff
path: root/MistyCore/error.js
blob: fd5e2b27361134273defae0b9ad3d74cd7402832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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."))
}