(error, req, res, next)
| 29 | } |
| 30 | |
| 31 | export default async function handleError(error, req, res, next) { |
| 32 | const responseDone = res.headersSent || req.aborted |
| 33 | |
| 34 | if (req.path.startsWith('/assets') || req.path.startsWith('/_next/static')) { |
| 35 | if (!responseDone) { |
| 36 | // By default, Fastly will cache 404 responses unless otherwise |
| 37 | // told not to. |
| 38 | // See https://docs.fastly.com/en/guides/how-caching-and-cdns-work#http-status-codes-cached-by-default |
| 39 | // Let's cache our 404'ing assets conservatively. |
| 40 | // The Cache-Control is short, and let's use the default surrogate |
| 41 | // key just in case it was a mistake. |
| 42 | errorCacheControl(res) |
| 43 | // Makes sure the surrogate key is NOT the manual one if it failed. |
| 44 | // This basically unsets what was assumed in the beginning of |
| 45 | // loading all the middlewares. |
| 46 | setFastlySurrogateKey(res, SURROGATE_ENUMS.DEFAULT) |
| 47 | } |
| 48 | } else if (DEBUG_MIDDLEWARE_TESTS) { |
| 49 | console.warn('An error occurrred in some middleware handler', error) |
| 50 | } |
| 51 | |
| 52 | try { |
| 53 | // If the headers have already been sent or the request was aborted... |
| 54 | if (responseDone) { |
| 55 | // Report to Failbot |
| 56 | await logException(error, req) |
| 57 | |
| 58 | // We MUST delegate to the default Express error handler |
| 59 | return next(error) |
| 60 | } |
| 61 | |
| 62 | if (!req.context) { |
| 63 | req.context = {} |
| 64 | } |
| 65 | // display error on the page in development and staging, but not in production |
| 66 | if (process.env.HEROKU_PRODUCTION_APP !== 'true') { |
| 67 | req.context.error = error |
| 68 | } |
| 69 | |
| 70 | // Special handling for when a middleware calls `next(404)` |
| 71 | if (error === 404) { |
| 72 | // Note that if this fails, it will swallow that error. |
| 73 | return nextApp.render404(req, res) |
| 74 | } |
| 75 | |
| 76 | // If the error contains a status code, just send that back. This is usually |
| 77 | // from a middleware like `express.json()`. |
| 78 | if (error.statusCode || error.status) { |
| 79 | return res.sendStatus(error.statusCode || error.status) |
| 80 | } |
| 81 | |
| 82 | if (process.env.NODE_ENV !== 'test') { |
| 83 | console.error('500 error!', req.path) |
| 84 | console.error(error) |
| 85 | } |
| 86 | |
| 87 | res.statusCode = 500 |
| 88 | nextApp.renderError(error, req, res, req.path) |
nothing calls this directly
no test coverage detected