Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch errors in async handlers #506

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ExpressApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const express = require('express');
require('express-async-errors');

module.exports = express
2 changes: 1 addition & 1 deletion express.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const fafApp = require('./fafApp')
const express = require('express')
const express = require('./ExpressApp')
const app = express()

fafApp.setup(app)
Expand Down
2 changes: 1 addition & 1 deletion fafApp.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion routes/views/accountRouter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const express = require('express')
const express = require('../../ExpressApp')
const router = express.Router();
const middlewares = require('../middleware')

Expand Down
2 changes: 1 addition & 1 deletion routes/views/auth.js
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
2 changes: 1 addition & 1 deletion routes/views/clanRouter.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion routes/views/dataRouter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const express = require('express');
const express = require('../../ExpressApp')
const router = express.Router();
const {AcquireTimeoutError} = require('../../lib/MutexService');

Expand Down
2 changes: 1 addition & 1 deletion routes/views/defaultRouter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const express = require('express');
const express = require('../../ExpressApp')
const router = express.Router();

router.get('/', (req, res) => res.render('index'))
Expand Down
2 changes: 1 addition & 1 deletion routes/views/leaderboardRouter.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 1 addition & 1 deletion routes/views/news.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const express = require('express');
const express = require('../../ExpressApp')
const router = express.Router();

function getNewsArticleBySlug(articles, slug) {
Expand Down
2 changes: 1 addition & 1 deletion routes/views/staticMarkdownRouter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const express = require('express')
const express = require('../../ExpressApp')
const showdown = require('showdown')
const fs = require('fs')
const router = express.Router()
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/IsAuthenticatedMiddleware.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/NewsRouter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Express = require('express')
const Express = require('../../ExpressApp')
const supertestSession = require('supertest-session')
const fafApp = require('../../fafApp')

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/accountRouter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Express = require('express')
const Express = require('../../ExpressApp')
const supertestSession = require('supertest-session')
const fafApp = require('../../fafApp')

Expand Down
28 changes: 28 additions & 0 deletions tests/integration/asyncErrorHandler.test.js
Original file line number Diff line number Diff line change
@@ -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')
next(err)
})

const res = await testSession.get('/')

expect(res.status).toBe(500)
expect(res.text).toBe('not crashed')
})
})
2 changes: 1 addition & 1 deletion tests/integration/clanRouter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Express = require('express')
const Express = require('../../ExpressApp')
const supertestSession = require('supertest-session')
const fafApp = require('../../fafApp')

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/defaultRouter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Express = require('express')
const Express = require('../../ExpressApp')
const supertestSession = require('supertest-session')
const fafApp = require('../../fafApp')

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/leaderboardRouter.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/markdownRouter.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Express = require('express')
const Express = require('../../ExpressApp')
const supertestSession = require('supertest-session')
const fafApp = require('../../fafApp')

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/servicesMiddleware.test.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down