UNPKG

hfs

Version:
33 lines (32 loc) 1.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getErrorSections = getErrorSections; exports.sendErrorPage = sendErrorPage; const lang_1 = require("./lang"); const customHtml_1 = require("./customHtml"); const cross_1 = require("./cross"); const declaredErrorPages = [cross_1.HTTP_NOT_FOUND, cross_1.HTTP_FORBIDDEN, cross_1.HTTP_TOO_MANY_REQUESTS].map(String); function getErrorSections() { return declaredErrorPages; } // to be used with errors whose recipient is possibly human async function sendErrorPage(ctx, code = ctx.status) { var _a, _b; ctx.type = 'text'; ctx.set('content-disposition', ''); // reset ctx.attachment (or forceDownload) ctx.status = code; const msg = cross_1.HTTP_MESSAGES[ctx.status]; if (!msg) return; const lang = await (0, lang_1.getLangData)(ctx); if (!lang) return; const trans = (_a = Object.values(lang)[0]) === null || _a === void 0 ? void 0 : _a.translate; ctx.body = (_b = trans === null || trans === void 0 ? void 0 : trans[msg]) !== null && _b !== void 0 ? _b : msg; const errorPage = (0, customHtml_1.getSection)(ctx.status === cross_1.HTTP_UNAUTHORIZED ? 'unauthorized' : String(ctx.status)); if (!errorPage) return; if (errorPage.includes('<')) ctx.type = 'html'; ctx.body = errorPage.replace('$MESSAGE', String(ctx.body)); }