[web] improve logging for failed requests on MockApis (#35958)

- Prepare for zod usage in mocks
- Fix logging of the mock name

GitOrigin-RevId: 3c4d2981010992c293a9ece45935ce13cadd8dde
This commit is contained in:
Jakob Ackermann
2026-09-17 11:40:09 +00:00
committed by Copybot
parent e8070def26
commit e947f3c4aa
@@ -1,5 +1,7 @@
import logger from '@overleaf/logger'
import OError from '@overleaf/o-error'
import express from 'express'
import { handleValidationError } from '@overleaf/validation-tools'
/**
* Abstract class for running a mock API via Express. Handles setting up of
@@ -113,15 +115,23 @@ class AbstractMockApi {
* Set this to 'true' from the constructor of your derived class
*/
static initialize(port, { debug } = {}) {
/* eslint-disable mocha/no-mocha-arrows */
// `this` refers to the derived class
this._fromInit = true
this._obj = new this(port, { debug })
const name = this._obj.constructor.name
this._obj.applyDebugRoutes()
this._obj.applyRoutes()
// Every mock is a real Express app running in-process during
// acceptance tests, so it needs the same validation error handling
// as production routes for parseReq() schemas added in applyRoutes().
this._obj.app.use((err, req, res, next) => {
logger.fatal({ mockName: name, err, req }, 'request failed')
next(err)
})
this._obj.app.use(handleValidationError)
/* eslint-disable mocha/no-mocha-arrows */
const name = this.constructor.name
before(`starting mock ${name}`, () => this._obj.start())
after(`stopping mock ${name}`, () => this._obj.stop())
beforeEach(`resetting mock ${name}`, () => this._obj.reset())