2024-03-29 20:08:34 -04:00
|
|
|
const AppError = require("./AppError");
|
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
2025-01-26 21:50:57 -05:00
|
|
|
const errorHandler = (error, req, res, _next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
if (error instanceof AppError) {
|
|
|
|
|
logError(error);
|
|
|
|
|
return res.status(error.statusCode).json({
|
|
|
|
|
error: error.message
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logError(error.stack);
|
|
|
|
|
return res.status(505).send("Oups! We screwed up big time. ┻━┻ ︵ヽ(`Д´)ノ︵ ┻━┻");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const logError = (error) => {
|
|
|
|
|
const time = new Date();
|
|
|
|
|
var log_file = fs.createWriteStream(__dirname + '/../debug.log', {flags : 'a'});
|
|
|
|
|
log_file.write(time + '\n' + error + '\n\n');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = errorHandler;
|