Ver Fonte

Handle Zod errors

GitOrigin-RevId: 0501602e8d9a014987b3cbc89cffd86186995e06
Andrew Rumble há 11 meses atrás
pai
commit
b15bd48376
1 ficheiros alterados com 18 adições e 0 exclusões
  1. 18 0
      services/notifications/app.js

+ 18 - 0
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 {