UNPKG

dino-express

Version:

DinO enabled REST framework based on express

92 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorHandler = void 0; const tslib_1 = require("tslib"); /* eslint-disable @typescript-eslint/strict-boolean-expressions */ /* eslint-disable @typescript-eslint/indent */ const http_exception_1 = require("./exception/http.exception"); // Copyright 2018 Quirino Brizi [quirino.brizi@gmail.com] // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. const dino_core_1 = require("dino-core"); const express_error_handler_1 = tslib_1.__importDefault(require("express-error-handler")); const express_openapi_validate_1 = require("express-openapi-validate"); /** * The error handler used to define the express error handling behaviour * @public */ // eslint-disable-next-line @typescript-eslint/no-extraneous-class class ErrorHandler { /** * Allows to get an instance of the error handler */ static instance(environment) { const cloudProvider = environment.getOrDefault('dino:cloud:provider', 'google'); if (cloudProvider === 'aws') { // AWS doesn't have a format method on response return (err, _req, res, next) => { // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions const statusCode = dino_core_1.ObjectHelper.instanceOf(err, http_exception_1.HttpException) ? err.getStatusCode() : dino_core_1.ObjectHelper.instanceOf(err, express_openapi_validate_1.ValidationError) ? err.statusCode : 500; const body = { status: statusCode, message: err.message }; if (statusCode >= 400 && statusCode <= 499) { ; ['code', 'name', 'type', 'details'].forEach(function (prop) { // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (err[prop]) body[prop] = err[prop]; }); } res.body = body; res.statusCode = statusCode; next(); }; } else { return (err, req, res, next) => { // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions const statusCode = dino_core_1.ObjectHelper.instanceOf(err, http_exception_1.HttpException) ? err.getStatusCode() : 500; const handler = (0, express_error_handler_1.default)({ shutdown: () => { }, serializer: function (err) { const body = { status: statusCode, message: err.message }; // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (express_error_handler_1.default.isClientError(err.status)) { ; ['code', 'name', 'type', 'details'].forEach(function (prop) { // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions if (err[prop]) body[prop] = err[prop]; }); } return body; } }); // eslint-disable-next-line @typescript-eslint/dot-notation err['status'] = statusCode; return handler(err, req, res, next); }; } } } exports.ErrorHandler = ErrorHandler; //# sourceMappingURL=error.handler.js.map