Skip to content

Commit 53d10b9

Browse files
Fix JSON stringify exceptions in the logger (#1635)
Fixes OPS-3108.
1 parent 6cee49c commit 53d10b9

2 files changed

Lines changed: 53 additions & 9 deletions

File tree

packages/server/shared/src/lib/logger/log-cleaner.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ export const cleanLogEvent = (logEvent: any) => {
4141
} else if (typeof value === 'number') {
4242
eventData[key] = Math.round(value * 100) / 100;
4343
} else if (typeof value === 'object') {
44-
try {
45-
eventData[key] = truncate(JSON.stringify(value));
46-
} catch (error) {
47-
eventData[key] = `Logger error - could not stringify object. ${error}`;
48-
}
44+
eventData[key] = stringify(value);
4945
} else {
5046
eventData[key] = truncate(value);
5147
}
@@ -90,10 +86,16 @@ function extractErrorFields(
9086
eventData[errorKey + 'Name'] = truncate(name);
9187
if (value instanceof ApplicationError) {
9288
eventData[errorKey + 'Code'] = truncate(value.error.code);
93-
eventData[errorKey + 'Params'] = truncate(
94-
JSON.stringify(value.error.params),
95-
);
89+
eventData[errorKey + 'Params'] = stringify(value.error.params);
9690
} else if (context && Object.keys(context).length) {
97-
eventData[errorKey + 'Context'] = truncate(JSON.stringify(context));
91+
eventData[errorKey + 'Context'] = stringify(context);
92+
}
93+
}
94+
95+
function stringify(value: any) {
96+
try {
97+
return truncate(JSON.stringify(value));
98+
} catch (error) {
99+
return `Logger error - could not stringify object. ${error}`;
98100
}
99101
}

packages/server/shared/test/log-cleaner.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,48 @@ describe('log-cleaner', () => {
263263
});
264264
});
265265

266+
it('should handle error context with circular reference', () => {
267+
const error = new Error('test error');
268+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
269+
const circularContext: any = { key: 'value' };
270+
circularContext.circular = circularContext;
271+
Object.assign(error, circularContext);
272+
273+
const logEvent = {
274+
event: error,
275+
};
276+
277+
const result = cleanLogEvent(logEvent);
278+
279+
expect(result.event.errorContext).toMatch(
280+
/^Logger error - could not stringify object\./,
281+
);
282+
});
283+
284+
it('should handle ApplicationError params with circular reference', () => {
285+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
286+
const circularParams: any = {
287+
message: 'test',
288+
data: {},
289+
};
290+
circularParams.data.circular = circularParams;
291+
292+
const appError = new ApplicationError({
293+
code: ErrorCode.ENTITY_NOT_FOUND,
294+
params: circularParams,
295+
});
296+
297+
const logEvent = {
298+
event: appError,
299+
};
300+
301+
const result = cleanLogEvent(logEvent);
302+
303+
expect(result.event.errorParams).toMatch(
304+
/^Logger error - could not stringify object\./,
305+
);
306+
});
307+
266308
it('should flatten error in correct fields by prefix', () => {
267309
const logEvent = {
268310
event: {

0 commit comments

Comments
 (0)