From 780bfe8371dea1ab013e032c3fefab75f5c82cfe Mon Sep 17 00:00:00 2001 From: Andrew Rumble Date: Thu, 11 Sep 2025 11:07:01 +0100 Subject: [PATCH] Handle Zod errors GitOrigin-RevId: 0501602e8d9a014987b3cbc89cffd86186995e06 --- services/notifications/app.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/services/notifications/app.js b/services/notifications/app.js index 5433d7fb0c..ea32c83329 100644 --- a/services/notifications/app.js +++ b/services/notifications/app.js @@ -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 {