Website: Add error handler middleware function to http config. (#19703)

Closes: #19679

Changes:
- Added a custom error handler to the HTTP middleware that returns a
416: Range Not Satisfiable if the serve-static middleware throws a
'Range Not Satisfiable' error.
This commit is contained in:
Eric 2024-06-14 17:35:21 -05:00 committed by GitHub
parent 48ea95385e
commit 52ff031e98
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -29,16 +29,17 @@ module.exports.http = {
* *
***************************************************************************/
// order: [
// 'cookieParser',
// 'session',
// 'bodyParser',
// 'compress',
// 'poweredBy',
// 'router',
// 'www',
// 'favicon',
// ],
order: [
'cookieParser',
'session',
'bodyParser',
'compress',
'poweredBy',
'router',
'www',
'favicon',
'middlewareErrorHandler'
],
/***************************************************************************
@ -67,6 +68,16 @@ module.exports.http = {
return middlewareFn;
})(),
// Note: this middleware function will run for every HTTP request, but will only handle errors thrown by the serve-static middleware if a user requests an invalid byte range of a static asset.
middlewareErrorHandler: function(err, req, res, next) {
// If this is a 'RangeNotSatisfiableError' error, respond with a 416 status code.
if (err.message === 'Range Not Satisfiable') {
return res.status(416).send();
} else {
return next(err);
}
},
},
};