diff --git a/packages/http-router/package.json b/packages/http-router/package.json index cc529d9d7878f..a1dc08a13487c 100644 --- a/packages/http-router/package.json +++ b/packages/http-router/package.json @@ -31,6 +31,7 @@ ], "dependencies": { "@rocket.chat/core-typings": "workspace:^", + "@rocket.chat/logger": "workspace:^", "@rocket.chat/rest-typings": "workspace:^", "ajv": "^8.17.1", "express": "^4.21.2", diff --git a/packages/http-router/src/Router.ts b/packages/http-router/src/Router.ts index 040de9ed457b8..72f4685831243 100644 --- a/packages/http-router/src/Router.ts +++ b/packages/http-router/src/Router.ts @@ -1,3 +1,4 @@ +import { Logger } from '@rocket.chat/logger'; import type { Method } from '@rocket.chat/rest-typings'; import type { AnySchema } from 'ajv'; import express from 'express'; @@ -9,6 +10,8 @@ import qs from 'qs'; // Using qs specifically to keep express compatibility import type { ResponseSchema, TypedOptions } from './definition'; import { honoAdapterForExpress } from './middlewares/honoAdapterForExpress'; +const logger = new Logger('HttpRouter'); + type MiddlewareHandlerListAndActionHandler Promise>> = [ ...MiddlewareHandler[], TContext, @@ -199,6 +202,12 @@ export class Router< if (options.query) { const validatorFn = options.query; if (typeof options.query === 'function' && !validatorFn(queryParams)) { + logger.warn({ + msg: 'Query parameters validation failed - route spec does not match request payload', + method: req.method, + path: req.url, + error: validatorFn.errors?.map((error: any) => error.message).join('\n '), + }); return c.json( { success: false, @@ -215,6 +224,12 @@ export class Router< if (options.body) { const validatorFn = options.body; if (typeof options.body === 'function' && !validatorFn((req as any).bodyParams || bodyParams)) { + logger.warn({ + msg: 'Request body validation failed - route spec does not match request payload', + method: req.method, + path: req.url, + error: validatorFn.errors?.map((error: any) => error.message).join('\n '), + }); return c.json( { success: false, @@ -240,6 +255,12 @@ export class Router< throw new Error(`Missing response validator for endpoint ${req.method} - ${req.url} with status code ${statusCode}`); } if (responseValidatorFn && !responseValidatorFn(coerceDatesToStrings(body))) { + logger.warn({ + msg: 'Response validation failed - response does not match route spec', + method: req.method, + path: req.url, + error: responseValidatorFn.errors?.map((error: any) => error.message).join('\n '), + }); return c.json( { success: false, diff --git a/yarn.lock b/yarn.lock index 110651e883e89..53d6e319dc48b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7876,6 +7876,7 @@ __metadata: "@rocket.chat/core-typings": "workspace:^" "@rocket.chat/eslint-config": "workspace:~" "@rocket.chat/jest-presets": "workspace:^" + "@rocket.chat/logger": "workspace:^" "@rocket.chat/rest-typings": "workspace:^" "@rocket.chat/tsconfig": "workspace:*" "@types/express": "npm:^4.17.23"