import FailBot from '../lib/failbot.js' import { nextApp } from './next.js' import { setFastlySurrogateKey, SURROGATE_ENUMS } from './set-fastly-surrogate-key.js' import { cacheControlFactory } from './cache-control.js' const cacheControl = cacheControlFactory(60) // 1 minute function shouldLogException(error) { const IGNORED_ERRORS = [ // avoid sending CSRF token errors (from bad-actor POST requests) 'EBADCSRFTOKEN', // Client connected aborted 'ECONNRESET', ] if (IGNORED_ERRORS.includes(error.code)) { return false } // We should log this exception return true } async function logException(error, req) { if (process.env.NODE_ENV !== 'test' && shouldLogException(error)) { await FailBot.report(error, { path: req.path, }) } } export default async function handleError(error, req, res, next) { // When you run tests that use things doing get() requests in // our supertest handler, if something goes wrong anywhere in the app // and its middlewares, you get a 500 but the error is never displayed // anywhere. So this is why we log it additionally. // Note, not using console.error() because it's arguably handled. // Some tests might actually expect a 500 error. if (req.path.startsWith('/assets') || req.path.startsWith('/_next/static')) { // By default, Fastly will cache 404 responses unless otherwise // told not to. // See https://docs.fastly.com/en/guides/how-caching-and-cdns-work#http-status-codes-cached-by-default // Most of the time, that's a good thing! Especially, if bombarded // for some static asset that we don't have. // E.g. `ab -n 10000 https://docs.github.com/assets/doesnotexist.png` // But due to potential timing issue related to how the servers start, // what might happen is that a new insteance comes up that // contains `