Base implementation of PM back-end started.

This commit is contained in:
rainbow napkin 2025-09-19 03:47:19 -04:00
parent 7da07c8717
commit 67edef9035
5 changed files with 182 additions and 12 deletions

View file

@ -173,17 +173,40 @@ module.exports.errorMiddleware = function(err, req, res, next){
* @param {Error} err - error to dump to file
* @param {Date} date - Date of error, defaults to now
*/
module.exports.dumpError = function(err, date = new Date()){
module.exports.dumpError = async function(err, date = new Date()){
try{
const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`;
const path = `log/crash/${date.getTime()}.log`;
//Crash directory
const dir = "./log/crash/"
//Double check crash folder exists
try{
await fs.stat(dir);
//If we caught an error (most likely it's missing)
}catch(err){
//Shout about it
module.exports.consoleWarn("Log folder missing, mking dir!")
//Make it if doesn't
await fs.mkdir(dir, {recursive: true});
}
//Assemble log file path
const path = `${dir}${date.getTime()}.log`;
//Generate error file content
const content = `Error Date: ${date.toLocaleString()} (UTC-${date.getTimezoneOffset()/60})\nError Type: ${err.name}\nError Msg:${err.message}\nStack Trace:\n\n${err.stack}`;
//Write content to file
fs.writeFile(path, content);
//Whine about the error
module.exports.consoleWarn(`Warning: Unexpected Server Crash gracefully dumped to '${path}'... SOMETHING MAY BE VERY BROKEN!!!!`);
//If somethine went really really wrong
}catch(doubleErr){
//Use humor to cope with the pain
module.exports.consoleWarn("Yo Dawg, I herd you like errors, so I put an error in your error dump, so you can dump while you dump:");
//Dump the original error to console
module.exports.consoleWarn(err);
//Dump the error we had saving that error to file to console
module.exports.consoleWarn(doubleErr);
}
}