Port handle-errors.js to TypeScript (#50596)
This commit is contained in:
@@ -15,7 +15,7 @@ import {
|
|||||||
setDefaultFastlySurrogateKey,
|
setDefaultFastlySurrogateKey,
|
||||||
setLanguageFastlySurrogateKey,
|
setLanguageFastlySurrogateKey,
|
||||||
} from './set-fastly-surrogate-key.js'
|
} from './set-fastly-surrogate-key.js'
|
||||||
import handleErrors from '@/observability/middleware/handle-errors.js'
|
import handleErrors from '@/observability/middleware/handle-errors'
|
||||||
import handleNextDataPath from './handle-next-data-path.js'
|
import handleNextDataPath from './handle-next-data-path.js'
|
||||||
import detectLanguage from '@/languages/middleware/detect-language.js'
|
import detectLanguage from '@/languages/middleware/detect-language.js'
|
||||||
import reloadTree from './reload-tree.js'
|
import reloadTree from './reload-tree.js'
|
||||||
|
|||||||
@@ -1,15 +1,24 @@
|
|||||||
|
import type { NextFunction, Response } from 'express'
|
||||||
|
|
||||||
import FailBot from '../lib/failbot.js'
|
import FailBot from '../lib/failbot.js'
|
||||||
import { nextApp } from '#src/frame/middleware/next.js'
|
import { nextApp } from '@/frame/middleware/next.js'
|
||||||
import {
|
import {
|
||||||
setFastlySurrogateKey,
|
setFastlySurrogateKey,
|
||||||
SURROGATE_ENUMS,
|
SURROGATE_ENUMS,
|
||||||
} from '#src/frame/middleware/set-fastly-surrogate-key.js'
|
} from '@/frame/middleware/set-fastly-surrogate-key.js'
|
||||||
import { errorCacheControl } from '#src/frame/middleware/cache-control.js'
|
import { errorCacheControl } from '@/frame/middleware/cache-control.js'
|
||||||
import statsd from '#src/observability/lib/statsd.js'
|
import statsd from '@/observability/lib/statsd.js'
|
||||||
|
import { ExtendedRequest } from '@/types.js'
|
||||||
|
|
||||||
const DEBUG_MIDDLEWARE_TESTS = Boolean(JSON.parse(process.env.DEBUG_MIDDLEWARE_TESTS || 'false'))
|
const DEBUG_MIDDLEWARE_TESTS = Boolean(JSON.parse(process.env.DEBUG_MIDDLEWARE_TESTS || 'false'))
|
||||||
|
|
||||||
function shouldLogException(error) {
|
type ErrorWithCode = Error & {
|
||||||
|
code: string
|
||||||
|
statusCode?: number
|
||||||
|
status?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
function shouldLogException(error: ErrorWithCode) {
|
||||||
const IGNORED_ERRORS = [
|
const IGNORED_ERRORS = [
|
||||||
// Client connected aborted
|
// Client connected aborted
|
||||||
'ECONNRESET',
|
'ECONNRESET',
|
||||||
@@ -23,7 +32,7 @@ function shouldLogException(error) {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
async function logException(error, req) {
|
async function logException(error: ErrorWithCode, req: ExtendedRequest) {
|
||||||
if (process.env.NODE_ENV !== 'test' && shouldLogException(error)) {
|
if (process.env.NODE_ENV !== 'test' && shouldLogException(error)) {
|
||||||
await FailBot.report(error, {
|
await FailBot.report(error, {
|
||||||
path: req.path,
|
path: req.path,
|
||||||
@@ -32,7 +41,7 @@ async function logException(error, req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function timedOut(req) {
|
function timedOut(req: ExtendedRequest) {
|
||||||
// The `req.pagePath` can come later so it's not guaranteed to always
|
// The `req.pagePath` can come later so it's not guaranteed to always
|
||||||
// be present. It's added by the `handle-next-data-path.js` middleware
|
// be present. It's added by the `handle-next-data-path.js` middleware
|
||||||
// we translates those "cryptic" `/_next/data/...` URLs from
|
// we translates those "cryptic" `/_next/data/...` URLs from
|
||||||
@@ -44,10 +53,15 @@ function timedOut(req) {
|
|||||||
statsd.increment('middleware.timeout', 1, incrementTags)
|
statsd.increment('middleware.timeout', 1, incrementTags)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function handleError(error, req, res, next) {
|
export default async function handleError(
|
||||||
|
error: ErrorWithCode | number,
|
||||||
|
req: ExtendedRequest,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction,
|
||||||
|
) {
|
||||||
// Potentially set by the `connect-timeout` middleware.
|
// Potentially set by the `connect-timeout` middleware.
|
||||||
if (req.timedout) {
|
if (req.timedout) {
|
||||||
timedOut(req, res)
|
timedOut(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
const responseDone = res.headersSent || req.aborted
|
const responseDone = res.headersSent || req.aborted
|
||||||
@@ -74,7 +88,9 @@ export default async function handleError(error, req, res, next) {
|
|||||||
// If the headers have already been sent or the request was aborted...
|
// If the headers have already been sent or the request was aborted...
|
||||||
if (responseDone) {
|
if (responseDone) {
|
||||||
// Report to Failbot
|
// Report to Failbot
|
||||||
await logException(error, req)
|
if (typeof error !== 'number') {
|
||||||
|
await logException(error, req)
|
||||||
|
}
|
||||||
|
|
||||||
// We MUST delegate to the default Express error handler
|
// We MUST delegate to the default Express error handler
|
||||||
return next(error)
|
return next(error)
|
||||||
@@ -83,21 +99,25 @@ export default async function handleError(error, req, res, next) {
|
|||||||
if (!req.context) {
|
if (!req.context) {
|
||||||
req.context = {}
|
req.context = {}
|
||||||
}
|
}
|
||||||
// display error on the page in development and staging, but not in production
|
|
||||||
if (process.env.HEROKU_PRODUCTION_APP !== 'true') {
|
|
||||||
req.context.error = error
|
|
||||||
}
|
|
||||||
|
|
||||||
// Special handling for when a middleware calls `next(404)`
|
// Special handling for when a middleware calls `next(404)`
|
||||||
if (error === 404) {
|
if (error === 404) {
|
||||||
// Note that if this fails, it will swallow that error.
|
// Note that if this fails, it will swallow that error.
|
||||||
return nextApp.render404(req, res)
|
return nextApp.render404(req, res)
|
||||||
}
|
}
|
||||||
|
if (typeof error === 'number') {
|
||||||
|
throw new Error("Don't use next(xxx) where xxx is any other number than 404")
|
||||||
|
}
|
||||||
|
|
||||||
|
// display error on the page in development and staging, but not in production
|
||||||
|
if (process.env.HEROKU_PRODUCTION_APP !== 'true') {
|
||||||
|
req.context.error = error
|
||||||
|
}
|
||||||
|
|
||||||
// If the error contains a status code, just send that back. This is usually
|
// If the error contains a status code, just send that back. This is usually
|
||||||
// from a middleware like `express.json()`.
|
// from a middleware like `express.json()`.
|
||||||
if (error.statusCode || error.status) {
|
if (error.statusCode) {
|
||||||
return res.sendStatus(error.statusCode || error.status)
|
return res.sendStatus(error.statusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
res.statusCode = 500
|
res.statusCode = 500
|
||||||
@@ -8,6 +8,7 @@ export type ExtendedRequest = Request & {
|
|||||||
pagePath?: string
|
pagePath?: string
|
||||||
context?: {
|
context?: {
|
||||||
currentCategory?: string
|
currentCategory?: string
|
||||||
|
error?: Error
|
||||||
}
|
}
|
||||||
// Add more properties here as needed
|
// Add more properties here as needed
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user