1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/middleware/catch-bad-accept-language.js
Kevin Heis 8a56437c93 Pretty format (#20352)
* Update prettier flow to include JS

* Run prettier

* ...run prettier
2021-07-14 14:35:01 -07:00

16 lines
647 B
JavaScript

import accept from '@hapi/accept'
// Next.JS uses the @hapi/accept package to parse and detect languages. If the accept-language header is malformed
// it throws an error from within Next.JS, which results in a 500 response. This ends up being noisy because we
// track 500s. To counteract this, we'll try to catch the error first and make sure it doesn't happen
export default function catchBadAcceptLanguage(req, res, next) {
try {
accept.language(req.headers['accept-language'])
} catch (e) {
// if there's a problem with parsing 'accept-language', just clear it out.
req.headers['accept-language'] = ''
}
return next()
}