Handle Zod errors

GitOrigin-RevId: 0501602e8d9a014987b3cbc89cffd86186995e06
This commit is contained in:
Andrew Rumble
2025-09-11 11:07:01 +01:00
committed by Copybot
parent 773e5dd856
commit 780bfe8371

View File

@@ -8,6 +8,8 @@ import methodOverride from 'method-override'
import { mongoClient } from './app/js/mongodb.js'
import NotificationsController from './app/js/NotificationsController.ts'
import HealthCheckController from './app/js/HealthCheckController.js'
import { isZodErrorLike } from 'zod-validation-error'
import { ParamsError } from '@overleaf/validation-tools'
const app = express()
@@ -42,6 +44,22 @@ app.get('/health_check', HealthCheckController.check)
app.get('*', (req, res) => res.sendStatus(404))
app.use(handleApiError)
function handleApiError(err, req, res, next) {
req.logger.addFields({ err })
if (err instanceof ParamsError) {
req.logger.setLevel('warn')
res.sendStatus(404)
} else if (isZodErrorLike(err)) {
req.logger.setLevel('warn')
res.sendStatus(400)
} else {
req.logger.setLevel('error')
res.sendStatus(500)
}
}
const host = Settings.internal.notifications?.host || '127.0.0.1'
const port = Settings.internal.notifications?.port || 3042
try {