Compare commits

...

3 Commits

Author SHA1 Message Date
Richard Hansen 66a7cf73d8 Let Express log HTTP handler errors
Express already comes with a built-in error handler that is attached
as the last middleware. Any handler or middleware can pass an Error
object (with an optional `.status` or `.statusCode` property set to a
4xx or 5xx code) to `next()` and Express will both log a detailed
error message and display a useful error message to the user. For more
details see "The default error handler" at:
https://expressjs.com/en/guide/error-handling.html
2021-08-20 02:51:43 -04:00
Richard Hansen 30e5bffaf2 stats: Add metrics for all HTTP status codes 2021-08-20 02:41:46 -04:00
Richard Hansen 1434129a7b stats: Move `http500` metric middleware to the beginning 2021-08-20 02:40:50 -04:00
3 changed files with 8 additions and 25 deletions

View File

@ -74,12 +74,6 @@
"expressCreateServer": "ep_etherpad-lite/node/hooks/express/importexport"
}
},
{
"name": "errorhandling",
"hooks": {
"expressCreateServer": "ep_etherpad-lite/node/hooks/express/errorhandling"
}
},
{
"name": "socketio",
"hooks": {

View File

@ -1,6 +1,7 @@
'use strict';
const _ = require('underscore');
const assert = require('assert').strict;
const cookieParser = require('cookie-parser');
const events = require('events');
const express = require('express');
@ -113,6 +114,13 @@ exports.restartServer = async () => {
}
app.use((req, res, next) => {
// This stats logic should be in the first middleware to ensure that no other middleware can
// prevent the stats from being updated.
res.on('finish', () => {
assert(res.statusCode);
stats.meter(`http${res.statusCode}`).mark();
});
// res.header("X-Frame-Options", "deny"); // breaks embedded pads
if (settings.ssl) {
// we use SSL

View File

@ -1,19 +0,0 @@
'use strict';
const stats = require('../../stats');
exports.expressCreateServer = (hook_name, args, cb) => {
exports.app = args.app;
// Handle errors
args.app.use((err, req, res, next) => {
// if an error occurs Connect will pass it down
// through these "error-handling" middleware
// 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();
};