Skip to content

Commit

Permalink
Permet de gérer les exceptions dans les routes async d'Express
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed May 11, 2022
1 parent 455acaa commit 23a5007
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
63 changes: 61 additions & 2 deletions export/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion export/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "stylo-export",
"private": true,
"version": "dev",
"version": "1.0.0-dev",
"description": "Export logic for Stylo to HTML/zip + others",
"main": "app.js",
"engines": {
Expand All @@ -11,6 +11,7 @@
"scripts": {
"test": "jest",
"start": "node src/app.js",
"dev": "npx dotenv -e ../stylo.env npm run start",
"prod": "NODE_ENV=production node --heapsnapshot-signal=SIGUSR2 src/app.js"
},
"repository": {
Expand Down Expand Up @@ -44,6 +45,8 @@
"remove-markdown": "^0.3.0"
},
"devDependencies": {
"dotenv": "^16.0.1",
"dotenv-cli": "^5.1.0",
"jest": "^26.6.1"
},
"husky": {
Expand Down
34 changes: 23 additions & 11 deletions export/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ const pino = require('pino-http')({

const { exportArticleHtml, exportArticleZip, exportBookHtml, exportBookZip, exportVersionHtml, exportVersionZip, exportBatchTagZip } = require('./export.js')

// inspired by: https://github.com/Abazhenov/express-async-handler/blob/master/index.js
const asyncHandler = fn =>
function asyncUtilWrap (...args) {
const fnReturn = fn(...args)
const next = args[args.length - 1]
return Promise.resolve(fnReturn).catch(next)
}

const app = express()
app.use(pino)
app.use(cors({
Expand All @@ -15,24 +23,28 @@ app.use(cors({

const listenPort = process.env.PORT || 3060

const asyncExportVersionHtml = asyncHandler(exportVersionHtml)
const asyncExportArticleHtml = asyncHandler(exportArticleHtml)
const asyncExportBookHtml = asyncHandler(exportBookHtml)

const exportRouter = express.Router()
exportRouter.get('/version/:id/html', exportVersionHtml)
exportRouter.get('/version/:id/zip', exportVersionZip)
exportRouter.get('/article/:id/html', exportArticleHtml)
exportRouter.get('/article/:id/zip', exportArticleZip)
exportRouter.get('/book/:id/html', exportBookHtml)
exportRouter.get('/book/:id/zip', exportBookZip)
exportRouter.get('/tag/:ids/zip', exportBatchTagZip)
exportRouter.get('/version/:id/html', asyncExportVersionHtml)
exportRouter.get('/version/:id/zip', asyncHandler(exportVersionZip))
exportRouter.get('/article/:id/html', asyncExportArticleHtml)
exportRouter.get('/article/:id/zip', asyncHandler(exportArticleZip))
exportRouter.get('/book/:id/html', asyncExportBookHtml)
exportRouter.get('/book/:id/zip', asyncHandler(exportBookZip))
exportRouter.get('/tag/:ids/zip', asyncHandler(exportBatchTagZip))
app.use('/export', exportRouter)

// WARNING: the following routes are used by article/book preview routes to annotate the page.
// the URL is used as an unique identifier and should not be modified otherwise the annotations will be "lost"!
// as a result, we are forced to keep these alias until we use an URL-independent identifier on Hypothesis.
// see: https://web.hypothes.is/help/how-hypothesis-interacts-with-document-metadata/
app.get('/api/v1/htmlVersion/:id', exportVersionHtml)
app.get('/api/v1/htmlArticle/:id', exportArticleHtml)
app.get('/htmlBook/:id', exportBookHtml)
app.get('/api/v1/htmlBook/:id', exportBookHtml)
app.get('/api/v1/htmlVersion/:id', asyncExportVersionHtml)
app.get('/api/v1/htmlArticle/:id', asyncExportArticleHtml)
app.get('/htmlBook/:id', asyncExportBookHtml)
app.get('/api/v1/htmlBook/:id', asyncExportBookHtml)

app.use(function errorHandler (error, req, res, next) {
if (!error) {
Expand Down

0 comments on commit 23a5007

Please sign in to comment.