diff --git a/backend/package-lock.json b/backend/package-lock.json index ff2b50915..18516a908 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -29,6 +29,7 @@ "crypto-js": "^4.1.1", "dotenv": "^16.0.1", "express": "^4.18.1", + "express-async-errors": "^3.1.1", "express-rate-limit": "^6.7.0", "express-validator": "^6.14.2", "handlebars": "^4.7.7", @@ -6210,6 +6211,14 @@ "node": ">= 0.10.0" } }, + "node_modules/express-async-errors": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/express-async-errors/-/express-async-errors-3.1.1.tgz", + "integrity": "sha512-h6aK1da4tpqWSbyCa3FxB/V6Ehd4EEB15zyQq9qe75OZBp0krinNKuH4rAY+S/U/2I36vdLAUFSjQJ+TFmODng==", + "peerDependencies": { + "express": "^4.16.2" + } + }, "node_modules/express-rate-limit": { "version": "6.7.0", "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-6.7.0.tgz", @@ -18038,6 +18047,12 @@ } } }, + "express-async-errors": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/express-async-errors/-/express-async-errors-3.1.1.tgz", + "integrity": "sha512-h6aK1da4tpqWSbyCa3FxB/V6Ehd4EEB15zyQq9qe75OZBp0krinNKuH4rAY+S/U/2I36vdLAUFSjQJ+TFmODng==", + "requires": {} + }, "express-rate-limit": { "version": "6.7.0", "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-6.7.0.tgz", diff --git a/backend/package.json b/backend/package.json index 137c9d9d1..1232a4e7e 100644 --- a/backend/package.json +++ b/backend/package.json @@ -20,6 +20,7 @@ "crypto-js": "^4.1.1", "dotenv": "^16.0.1", "express": "^4.18.1", + "express-async-errors": "^3.1.1", "express-rate-limit": "^6.7.0", "express-validator": "^6.14.2", "handlebars": "^4.7.7", diff --git a/backend/src/helpers/auth.ts b/backend/src/helpers/auth.ts index 977a82586..2623d3153 100644 --- a/backend/src/helpers/auth.ts +++ b/backend/src/helpers/auth.ts @@ -43,7 +43,7 @@ const validateAuthMode = ({ }) => { const apiKey = headers['x-api-key']; const authHeader = headers['authorization']; - + let authMode, authTokenValue; if (apiKey === undefined && authHeader === undefined) { // case: no auth or X-API-KEY header present diff --git a/backend/src/index.ts b/backend/src/index.ts index 178baaacb..2e230a66c 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -1,6 +1,8 @@ import dotenv from 'dotenv'; dotenv.config(); import express from 'express'; +// eslint-disable-next-line @typescript-eslint/no-var-requires +require('express-async-errors'); import helmet from 'helmet'; import cors from 'cors'; import { DatabaseService } from './services'; diff --git a/backend/src/middleware/requestErrorHandler.ts b/backend/src/middleware/requestErrorHandler.ts index 4833d991f..047541e2e 100644 --- a/backend/src/middleware/requestErrorHandler.ts +++ b/backend/src/middleware/requestErrorHandler.ts @@ -7,11 +7,6 @@ import { getNodeEnv } from '../config'; export const requestErrorHandler: ErrorRequestHandler = async (error: RequestError | Error, req, res, next) => { if (res.headersSent) return next(); - if ((await getNodeEnv()) !== "production") { - /* eslint-disable no-console */ - console.log(error) - /* eslint-enable no-console */ - } //TODO: Find better way to type check for error. In current setting you need to cast type to get the functions and variables from RequestError if (!(error instanceof RequestError)) { diff --git a/backend/src/utils/patchAsyncRoutes.js b/backend/src/utils/patchAsyncRoutes.js deleted file mode 100644 index 24fe007f9..000000000 --- a/backend/src/utils/patchAsyncRoutes.js +++ /dev/null @@ -1,69 +0,0 @@ -/* -Original work Copyright (c) 2016, Nikolay Nemshilov -Modified work Copyright (c) 2016, David Banham - -Permission to use, copy, modify, and/or distribute this software for any purpose -with or without fee is hereby granted, provided that the above copyright notice -and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS -OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER -TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. - -*/ - -/* eslint-disable @typescript-eslint/no-var-requires */ -/* eslint-env node */ -const Layer = require('express/lib/router/layer'); -const Router = require('express/lib/router'); - -const last = (arr = []) => arr[arr.length - 1]; -const noop = Function.prototype; - -function copyFnProps(oldFn, newFn) { - Object.keys(oldFn).forEach((key) => { - newFn[key] = oldFn[key]; - }); - return newFn; -} - -function wrap(fn) { - const newFn = function newFn(...args) { - const ret = fn.apply(this, args); - const next = (args.length === 5 ? args[2] : last(args)) || noop; - if (ret && ret.catch) ret.catch(err => next(err)); - return ret; - }; - Object.defineProperty(newFn, 'length', { - value: fn.length, - writable: false, - }); - return copyFnProps(fn, newFn); -} - -function patchRouterParam() { - const originalParam = Router.prototype.constructor.param; - Router.prototype.constructor.param = function param(name, fn) { - fn = wrap(fn); - return originalParam.call(this, name, fn); - }; -} - -Object.defineProperty(Layer.prototype, 'handle', { - enumerable: true, - get() { - return this.__handle; - }, - set(fn) { - fn = wrap(fn); - this.__handle = fn; - }, -}); - -module.exports = { - patchRouterParam -}; diff --git a/backend/src/utils/setup/index.ts b/backend/src/utils/setup/index.ts index c59a974d0..c90a94d4f 100644 --- a/backend/src/utils/setup/index.ts +++ b/backend/src/utils/setup/index.ts @@ -4,8 +4,6 @@ import { setTransporter } from '../../helpers/nodemailer'; import { EELicenseService } from '../../ee/services'; import { initSmtp } from '../../services/smtp'; import { createTestUserForDevelopment } from '../addDevelopmentUser' -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { patchRouterParam } = require('../patchAsyncRoutes'); import { validateEncryptionKeysConfig } from './validateConfig'; @@ -36,7 +34,6 @@ import { * - Re-encrypting data */ export const setup = async () => { - patchRouterParam(); await validateEncryptionKeysConfig(); await TelemetryService.logTelemetryMessage();