Skip to content

Commit 2f9d25f

Browse files
feat(logger): add error cause logging
DIME-8714 Co-authored-by: Balint Barki <[email protected]>
1 parent ac84a92 commit 2f9d25f

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/logger/logger.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,38 @@ describe('Logger', () => {
250250
expect(logArguments.http.response.body.content).to.eql(JSON.stringify(error.response?.data));
251251
});
252252

253+
it('should log causes', () => {
254+
const rootCause = new Error('Root cause error');
255+
rootCause.name = 'RootCauseError';
256+
rootCause.stack = 'RootCauseError: Root cause error\n at rootFunction()';
257+
258+
const intermediateError = new Error('Intermediate error');
259+
intermediateError.name = 'IntermediateError';
260+
intermediateError.stack = 'IntermediateError: Intermediate error\n at intermediateFunction()';
261+
intermediateError.cause = rootCause;
262+
263+
const mainError = new Error('Main error occurred');
264+
mainError.name = 'MainError';
265+
mainError.stack = 'MainError: Main error occurred\n at mainFunction()';
266+
mainError.cause = intermediateError;
267+
268+
logger.fromError('test_action', mainError, { details: 'test details' });
269+
270+
const logArguments = JSON.parse(outputStub.args[0][0]);
271+
expect(logArguments.error.type).to.eql('MainError');
272+
expect(logArguments.error.message).to.eql('Main error occurred');
273+
expect(logArguments.error.cause).to.eql({
274+
type: 'IntermediateError',
275+
message: 'Intermediate error',
276+
stack_trace: intermediateError.stack,
277+
cause: {
278+
type: 'RootCauseError',
279+
message: 'Root cause error',
280+
stack_trace: rootCause.stack,
281+
},
282+
});
283+
});
284+
253285
describe('#customError', () => {
254286
it('should log error as the given severity with action', () => {
255287
const error: Error & { data?: any } = new Error('failed');

src/logger/logger.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,24 @@ export class Logger {
181181
}
182182

183183
return {
184-
error: {
185-
type: error.name,
186-
message: error.message,
187-
context: this.shortenData((error as ErrorWithData).data),
188-
stack_trace: this.shortenStackTrace(error.stack || ''),
189-
},
184+
error: this.extractError(error),
190185
event: {
191186
reason: error.message,
192187
},
193188
};
194189
}
195190

191+
private extractError(error: Error): unknown {
192+
const shortenedData = this.shortenData((error as ErrorWithData).data);
193+
return {
194+
type: error.name,
195+
message: error.message,
196+
...(shortenedData && { context: shortenedData }),
197+
stack_trace: this.shortenStackTrace(error.stack || ''),
198+
...(error.cause instanceof Error && { cause: this.extractError(error.cause) }),
199+
};
200+
}
201+
196202
private getAxiosErrorDetails(error: AxiosError) {
197203
if (!error.isAxiosError) {
198204
return {};

0 commit comments

Comments
 (0)