UNPKG

@gabliam/web-core

Version:
119 lines (118 loc) 4.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SendErrorInterceptor = void 0; const tslib_1 = require("tslib"); const core_1 = require("@gabliam/core"); const escape_html_1 = tslib_1.__importDefault(require("escape-html")); const statuses_1 = tslib_1.__importDefault(require("statuses")); const exceptions_1 = require("../exceptions"); const execution_context_1 = require("../execution-context"); const metadatas_1 = require("../metadatas"); const response_entity_1 = require("../response-entity"); const DOUBLE_SPACE_REGEXP = /\x20{2}/g; const NEWLINE_REGEXP = /\n/g; function getResponseStatusCode(res) { let status = res.status; // default status code to 500 if outside valid range if (typeof status !== 'number' || status < 400 || status > 599) { status = 500; } return status; } function getErrorStatusCode(err) { // check err.status if (typeof err.status === 'number' && err.status >= 400 && err.status < 600) { return err.status; } // check err.statusCode if (typeof err.statusCode === 'number' && err.statusCode >= 400 && err.statusCode < 600) { return err.statusCode; } if (err !== undefined) { return 500; } return undefined; } function createResponse(body, status) { const res = new response_entity_1.ResponseEntity(body, status); res.addHeader('Content-Security-Policy', `default-src 'none'`); res.addHeader('X-Content-Type-Options', 'nosniff'); return res; } function getErrorMessage(err, status, env, json) { if (status === 204) { return ''; } let msg; if (env !== 'production') { // use err.stack, which typically includes err.message msg = err.stack; if (!msg) { if (json) { msg = err; } else if (typeof err.toString === 'function') { msg = err.toString(); } } } return msg || statuses_1.default.message[status] || ''; } function createHtmlDocument(message) { const body = (0, escape_html_1.default)(message) .replace(NEWLINE_REGEXP, '<br>') .replace(DOUBLE_SPACE_REGEXP, ' &nbsp;'); return (`${'<!DOCTYPE html>\n' + '<html lang="en">\n' + '<head>\n' + '<meta charset="utf-8">\n' + '<title>Error</title>\n' + '</head>\n' + '<body>\n' + '<pre>'}${body}</pre>\n` + `</body>\n` + `</html>\n`); } let SendErrorInterceptor = class SendErrorInterceptor { async intercept(next, exec, res) { const env = process.env.NODE_ENV || 'development'; try { await next(); return undefined; } catch (err) { if (exec.getMethodInfo().json) { if (err instanceof exceptions_1.HttpException) { return new response_entity_1.ResponseEntity(err.getJson(), err.getStatus()); } // respect status code from error const status = getErrorStatusCode(err) || getResponseStatusCode(res); // get error message const msg = getErrorMessage(err, status, env, true); return createResponse({ status, error: msg }, status); } if (err instanceof exceptions_1.HttpException) { const status = err.getStatus(); const msg = getErrorMessage(err, status, env, false); const body = createHtmlDocument(msg); return createResponse(body, status); } // let default error handler of express js throw err; } } }; tslib_1.__decorate([ tslib_1.__param(0, (0, metadatas_1.Next)()), tslib_1.__param(1, (0, metadatas_1.ExecContext)()), tslib_1.__param(2, (0, metadatas_1.Response)()), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [Function, execution_context_1.ExecutionContext, Object]), tslib_1.__metadata("design:returntype", Promise) ], SendErrorInterceptor.prototype, "intercept", null); SendErrorInterceptor = tslib_1.__decorate([ (0, core_1.Service)() ], SendErrorInterceptor); exports.SendErrorInterceptor = SendErrorInterceptor;