Skip to content

Commit 3903260

Browse files
committed
feat(http-router): integrate logger for improved error handling in Router class
1 parent e87c749 commit 3903260

3 files changed

Lines changed: 8 additions & 0 deletions

File tree

packages/http-router/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
],
3232
"dependencies": {
3333
"@rocket.chat/core-typings": "workspace:^",
34+
"@rocket.chat/logger": "workspace:^",
3435
"@rocket.chat/rest-typings": "workspace:^",
3536
"ajv": "^8.17.1",
3637
"express": "^4.21.2",

packages/http-router/src/Router.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Logger } from '@rocket.chat/logger';
12
import type { Method } from '@rocket.chat/rest-typings';
23
import type { AnySchema } from 'ajv';
34
import express from 'express';
@@ -9,6 +10,8 @@ import qs from 'qs'; // Using qs specifically to keep express compatibility
910
import type { ResponseSchema, TypedOptions } from './definition';
1011
import { honoAdapterForExpress } from './middlewares/honoAdapterForExpress';
1112

13+
const logger = new Logger('HttpRouter');
14+
1215
type MiddlewareHandlerListAndActionHandler<TOptions extends TypedOptions, TContext = (c: Context) => Promise<ResponseSchema<TOptions>>> = [
1316
...MiddlewareHandler[],
1417
TContext,
@@ -199,6 +202,7 @@ export class Router<
199202
if (options.query) {
200203
const validatorFn = options.query;
201204
if (typeof options.query === 'function' && !validatorFn(queryParams)) {
205+
logger.warn('Invalid query params', { error: validatorFn.errors?.map((error: any) => error.message).join('\n ') });
202206
return c.json(
203207
{
204208
success: false,
@@ -215,6 +219,7 @@ export class Router<
215219
if (options.body) {
216220
const validatorFn = options.body;
217221
if (typeof options.body === 'function' && !validatorFn((req as any).bodyParams || bodyParams)) {
222+
logger.warn('Invalid body params', { error: validatorFn.errors?.map((error: any) => error.message).join('\n ') });
218223
return c.json(
219224
{
220225
success: false,
@@ -240,6 +245,7 @@ export class Router<
240245
throw new Error(`Missing response validator for endpoint ${req.method} - ${req.url} with status code ${statusCode}`);
241246
}
242247
if (responseValidatorFn && !responseValidatorFn(coerceDatesToStrings(body))) {
248+
logger.warn('Invalid response', { error: responseValidatorFn.errors?.map((error: any) => error.message).join('\n ') });
243249
return c.json(
244250
{
245251
success: false,

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7876,6 +7876,7 @@ __metadata:
78767876
"@rocket.chat/core-typings": "workspace:^"
78777877
"@rocket.chat/eslint-config": "workspace:~"
78787878
"@rocket.chat/jest-presets": "workspace:^"
7879+
"@rocket.chat/logger": "workspace:^"
78797880
"@rocket.chat/rest-typings": "workspace:^"
78807881
"@rocket.chat/tsconfig": "workspace:*"
78817882
"@types/express": "npm:^4.17.23"

0 commit comments

Comments
 (0)