From f66d428210c4c86b0d2fd477b5c6b4f630afde21 Mon Sep 17 00:00:00 2001 From: fcaps Date: Thu, 30 Nov 2023 14:59:26 +0100 Subject: [PATCH] catch errors in async handlers --- ExpressApp.js | 4 +++ express.js | 2 +- fafApp.js | 2 +- package.json | 1 + routes/views/accountRouter.js | 2 +- routes/views/auth.js | 2 +- routes/views/clanRouter.js | 2 +- routes/views/dataRouter.js | 2 +- routes/views/defaultRouter.js | 2 +- routes/views/leaderboardRouter.js | 2 +- routes/views/news.js | 2 +- routes/views/staticMarkdownRouter.js | 2 +- .../IsAuthenticatedMiddleware.test.js | 2 +- tests/integration/NewsRouter.test.js | 2 +- tests/integration/accountRouter.test.js | 2 +- tests/integration/asyncErrorHandler.test.js | 28 +++++++++++++++++++ tests/integration/clanRouter.test.js | 2 +- tests/integration/defaultRouter.test.js | 2 +- tests/integration/leaderboardRouter.test.js | 2 +- tests/integration/markdownRouter.test.js | 2 +- tests/integration/servicesMiddleware.test.js | 2 +- yarn.lock | 5 ++++ 22 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 ExpressApp.js create mode 100644 tests/integration/asyncErrorHandler.test.js diff --git a/ExpressApp.js b/ExpressApp.js new file mode 100644 index 00000000..a05ccb72 --- /dev/null +++ b/ExpressApp.js @@ -0,0 +1,4 @@ +const express = require('express'); +require('express-async-errors'); + +module.exports = express diff --git a/express.js b/express.js index 4da08f03..cc469501 100644 --- a/express.js +++ b/express.js @@ -1,5 +1,5 @@ const fafApp = require('./fafApp') -const express = require('express') +const express = require('./ExpressApp') const app = express() fafApp.setup(app) diff --git a/fafApp.js b/fafApp.js index d00143b4..07e1d512 100644 --- a/fafApp.js +++ b/fafApp.js @@ -1,5 +1,5 @@ const appConfig = require('./config/app') -const express = require('express') +const express = require('./ExpressApp') const bodyParser = require('body-parser') const session = require('express-session') const FileStore = require('session-file-store')(session) diff --git a/package.json b/package.json index 46185d0c..cffe9c70 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "connect-flash": "^0.1.1", "dotenv": "16.3.1", "express": "^4.18.1", + "express-async-errors": "^3.1.1", "express-session": "^1.17.3", "express-validator": "7.0.1", "moment": "^2.29.4", diff --git a/routes/views/accountRouter.js b/routes/views/accountRouter.js index d27b3dba..d4c29e04 100644 --- a/routes/views/accountRouter.js +++ b/routes/views/accountRouter.js @@ -1,4 +1,4 @@ -const express = require('express') +const express = require('../../ExpressApp') const router = express.Router(); const middlewares = require('../middleware') diff --git a/routes/views/auth.js b/routes/views/auth.js index 7c5aff04..c8393e4a 100644 --- a/routes/views/auth.js +++ b/routes/views/auth.js @@ -1,6 +1,6 @@ const appConfig = require('../../config/app') const passport = require('passport') -const express = require("express") +const express = require('../../ExpressApp') const router = express.Router() router.get('/login', passport.authenticate(appConfig.oauth.strategy)); diff --git a/routes/views/clanRouter.js b/routes/views/clanRouter.js index 5000277f..c42c8f21 100644 --- a/routes/views/clanRouter.js +++ b/routes/views/clanRouter.js @@ -1,4 +1,4 @@ -const express = require('express'); +const express = require('../../ExpressApp') const router = express.Router(); // This will be replaced soon, therefor I did not spend time on it diff --git a/routes/views/dataRouter.js b/routes/views/dataRouter.js index 8b0faeec..b6b1095e 100644 --- a/routes/views/dataRouter.js +++ b/routes/views/dataRouter.js @@ -1,4 +1,4 @@ -const express = require('express'); +const express = require('../../ExpressApp') const router = express.Router(); const {AcquireTimeoutError} = require('../../lib/MutexService'); diff --git a/routes/views/defaultRouter.js b/routes/views/defaultRouter.js index 2ec30e28..4789b9eb 100644 --- a/routes/views/defaultRouter.js +++ b/routes/views/defaultRouter.js @@ -1,4 +1,4 @@ -const express = require('express'); +const express = require('../../ExpressApp') const router = express.Router(); router.get('/', (req, res) => res.render('index')) diff --git a/routes/views/leaderboardRouter.js b/routes/views/leaderboardRouter.js index 0a667ff0..2f7aa1b7 100644 --- a/routes/views/leaderboardRouter.js +++ b/routes/views/leaderboardRouter.js @@ -1,4 +1,4 @@ -const express = require('express'); +const express = require('../../ExpressApp') const router = express.Router(); const {AcquireTimeoutError} = require('../../lib/MutexService'); const middlewares = require('../middleware') diff --git a/routes/views/news.js b/routes/views/news.js index 3188b1d7..3531807d 100644 --- a/routes/views/news.js +++ b/routes/views/news.js @@ -1,4 +1,4 @@ -const express = require('express'); +const express = require('../../ExpressApp') const router = express.Router(); function getNewsArticleBySlug(articles, slug) { diff --git a/routes/views/staticMarkdownRouter.js b/routes/views/staticMarkdownRouter.js index 6b8c9e5c..98267e1c 100644 --- a/routes/views/staticMarkdownRouter.js +++ b/routes/views/staticMarkdownRouter.js @@ -1,4 +1,4 @@ -const express = require('express') +const express = require('../../ExpressApp') const showdown = require('showdown') const fs = require('fs') const router = express.Router() diff --git a/tests/integration/IsAuthenticatedMiddleware.test.js b/tests/integration/IsAuthenticatedMiddleware.test.js index 765b4a6e..60f45842 100644 --- a/tests/integration/IsAuthenticatedMiddleware.test.js +++ b/tests/integration/IsAuthenticatedMiddleware.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const middlewares = require('../../routes/middleware') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') diff --git a/tests/integration/NewsRouter.test.js b/tests/integration/NewsRouter.test.js index e85244d6..a8f6c3f5 100644 --- a/tests/integration/NewsRouter.test.js +++ b/tests/integration/NewsRouter.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') diff --git a/tests/integration/accountRouter.test.js b/tests/integration/accountRouter.test.js index 611652ed..f65ac552 100644 --- a/tests/integration/accountRouter.test.js +++ b/tests/integration/accountRouter.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') diff --git a/tests/integration/asyncErrorHandler.test.js b/tests/integration/asyncErrorHandler.test.js new file mode 100644 index 00000000..301f5010 --- /dev/null +++ b/tests/integration/asyncErrorHandler.test.js @@ -0,0 +1,28 @@ +const Express = require('../../ExpressApp') +const supertestSession = require('supertest-session') + +let testApp = null +let testSession = null + +beforeEach(() => { + const app = new Express() + testSession = supertestSession(app) + testApp = app +}) + +describe('Async Error Handler', function () { + test('route is protected and redirects to "/login"', async () => { + testApp.get('/', async () => { + throw new Error('test failed and node crashed') + }) + + testApp.use((err, req, res, next) => { + res.status(500).send('not crashed') + }) + + const res = await testSession.get('/') + + expect(res.status).toBe(500) + expect(res.text).toBe('not crashed') + }) +}) diff --git a/tests/integration/clanRouter.test.js b/tests/integration/clanRouter.test.js index dbd4cab2..2fe81c56 100644 --- a/tests/integration/clanRouter.test.js +++ b/tests/integration/clanRouter.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') diff --git a/tests/integration/defaultRouter.test.js b/tests/integration/defaultRouter.test.js index ca2b5629..09607455 100644 --- a/tests/integration/defaultRouter.test.js +++ b/tests/integration/defaultRouter.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') diff --git a/tests/integration/leaderboardRouter.test.js b/tests/integration/leaderboardRouter.test.js index 7815414d..192a44a5 100644 --- a/tests/integration/leaderboardRouter.test.js +++ b/tests/integration/leaderboardRouter.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') const passportMock = require('../helpers/PassportMock') diff --git a/tests/integration/markdownRouter.test.js b/tests/integration/markdownRouter.test.js index 6458a661..d61d5e6c 100644 --- a/tests/integration/markdownRouter.test.js +++ b/tests/integration/markdownRouter.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const supertestSession = require('supertest-session') const fafApp = require('../../fafApp') diff --git a/tests/integration/servicesMiddleware.test.js b/tests/integration/servicesMiddleware.test.js index 88f70d49..3e333121 100644 --- a/tests/integration/servicesMiddleware.test.js +++ b/tests/integration/servicesMiddleware.test.js @@ -1,4 +1,4 @@ -const Express = require('express') +const Express = require('../../ExpressApp') const WordpressService = require('../../lib/WordpressService') const UserService = require('../../lib/LoggedInUserService') const LeaderboardService = require('../../lib/LeaderboardService') diff --git a/yarn.lock b/yarn.lock index ccb783a2..cb6161f5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3220,6 +3220,11 @@ expect@^29.7.0: jest-message-util "^29.7.0" jest-util "^29.7.0" +express-async-errors@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/express-async-errors/-/express-async-errors-3.1.1.tgz#6053236d61d21ddef4892d6bd1d736889fc9da41" + integrity sha512-h6aK1da4tpqWSbyCa3FxB/V6Ehd4EEB15zyQq9qe75OZBp0krinNKuH4rAY+S/U/2I36vdLAUFSjQJ+TFmODng== + express-session@^1.17.3: version "1.17.3" resolved "https://registry.yarnpkg.com/express-session/-/express-session-1.17.3.tgz#14b997a15ed43e5949cb1d073725675dd2777f36"