@foal/core
Version:
Full-featured Node.js framework, with no complexity
40 lines (39 loc) • 1.55 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderError = renderError;
// std
const promises_1 = require("node:fs/promises");
const path_1 = require("path");
// FoalTS
const http_1 = require("../http");
const config_1 = require("../config");
const render_1 = require("./render");
/**
* Renders the default HTML page when an error is thrown or rejected in the application.
*
* The page is different depending on if the configuration key `settings.debug` is
* true or false.
*
* @export
* @param {Error} error - The error thrown or rejected.
* @param {Context} ctx - The Context object.
* @returns {Promise<HttpResponseInternalServerError>} The HTTP response.
*/
async function renderError(error, ctx) {
let body = '<html><head><title>INTERNAL SERVER ERROR</title></head><body>'
+ '<h1>500 - INTERNAL SERVER ERROR</h1></body></html>';
if (config_1.Config.get('settings.debug', 'boolean')) {
const template = await (0, promises_1.readFile)((0, path_1.join)(__dirname, '500.debug.html'), 'utf8');
const rex = /at (.*) \((.*):(\d+):(\d+)\)/;
const [, , path, line, column] = Array.from(rex.exec(error.stack || '') || []);
body = (0, render_1.renderToString)(template, {
column,
filename: (0, path_1.basename)(path || ''),
line,
message: error.message,
name: error.name,
stack: error.stack,
});
}
return new http_1.HttpResponseInternalServerError(body, { ctx, error });
}