From 1434129a7b3934f1aaba397e45ce1f3af44c4ba5 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Fri, 20 Aug 2021 02:35:56 -0400 Subject: [PATCH] stats: Move `http500` metric middleware to the beginning --- src/node/hooks/express.js | 7 +++++++ src/node/hooks/express/errorhandling.js | 3 --- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/node/hooks/express.js b/src/node/hooks/express.js index 351ab5bf2..c16374b0c 100644 --- a/src/node/hooks/express.js +++ b/src/node/hooks/express.js @@ -112,6 +112,13 @@ exports.restartServer = async () => { exports.server = http.createServer(app); } + // This error-handling middleware is installed first to ensure that no other middleware can + // prevent the stats from being updated. + app.use((err, req, res, next) => { + stats.meter('http500').mark(); + next(err); + }); + app.use((req, res, next) => { // res.header("X-Frame-Options", "deny"); // breaks embedded pads if (settings.ssl) { diff --git a/src/node/hooks/express/errorhandling.js b/src/node/hooks/express/errorhandling.js index 884ca9be0..b395f4606 100644 --- a/src/node/hooks/express/errorhandling.js +++ b/src/node/hooks/express/errorhandling.js @@ -1,7 +1,5 @@ 'use strict'; -const stats = require('../../stats'); - exports.expressCreateServer = (hook_name, args, cb) => { exports.app = args.app; @@ -12,7 +10,6 @@ exports.expressCreateServer = (hook_name, args, cb) => { // allowing you to respond however you like res.status(500).send({error: 'Sorry, something bad happened!'}); console.error(err.stack ? err.stack : err.toString()); - stats.meter('http500').mark(); }); return cb();