Skip to content

Commit e86621d

Browse files
authored
Log request validation errors at debug level (#1721)
Fixes OPS-2954. ## Additional Notes
1 parent 63804fb commit e86621d

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

packages/server/api/src/app/helper/error-handler.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from '@openops/server-shared';
22
import { ApplicationError, ErrorCode } from '@openops/shared';
3+
import type { ErrorObject } from 'ajv';
34
import { FastifyError, FastifyReply, FastifyRequest } from 'fastify';
45
import { StatusCodes } from 'http-status-codes';
56

@@ -19,19 +20,28 @@ export const errorHandler = async (
1920
params: error.error.params,
2021
});
2122
} else {
22-
const requestSummary = (({ method, url, body, params, query }) => ({
23+
const { method, url, body, params, query } = _request;
24+
const requestSummary = {
2325
method,
2426
url,
2527
body,
2628
params,
2729
query,
28-
}))(_request);
30+
};
2931

30-
logger.error('Error handler caught an exception.', {
31-
message: error.message,
32-
stack: error.stack,
33-
request: requestSummary,
34-
});
32+
if (isValidationError(error)) {
33+
logger.debug('Request validation failed.', {
34+
message: error.message,
35+
validation: error.validation,
36+
request: requestSummary,
37+
});
38+
} else {
39+
logger.error('Error handler caught an exception.', {
40+
message: error.message,
41+
stack: error.stack,
42+
request: requestSummary,
43+
});
44+
}
3545

3646
await reply
3747
.status(error.statusCode ?? StatusCodes.INTERNAL_SERVER_ERROR)
@@ -53,6 +63,17 @@ const isApplicationError = (error: unknown): error is ApplicationError => {
5363
}
5464
};
5565

66+
type FastifyValidationError = FastifyError & { validation?: ErrorObject[] };
67+
68+
const isValidationError = (error: unknown): error is FastifyValidationError => {
69+
return (
70+
typeof error === 'object' &&
71+
error !== null &&
72+
'validation' in error &&
73+
Array.isArray((error as FastifyValidationError).validation)
74+
);
75+
};
76+
5677
const statusCodeMap: Partial<Record<ErrorCode, StatusCodes>> = {
5778
[ErrorCode.INVALID_API_KEY]: StatusCodes.UNAUTHORIZED,
5879
[ErrorCode.INVALID_BEARER_TOKEN]: StatusCodes.UNAUTHORIZED,

0 commit comments

Comments
 (0)