Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/logger/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,38 @@ describe('Logger', () => {
expect(logArguments.http.response.body.content).to.eql(JSON.stringify(error.response?.data));
});

it('should log causes', () => {
const rootCause = new Error('Root cause error');
rootCause.name = 'RootCauseError';
rootCause.stack = 'RootCauseError: Root cause error\n at rootFunction()';

const intermediateError = new Error('Intermediate error');
intermediateError.name = 'IntermediateError';
intermediateError.stack = 'IntermediateError: Intermediate error\n at intermediateFunction()';
intermediateError.cause = rootCause;

const mainError = new Error('Main error occurred');
mainError.name = 'MainError';
mainError.stack = 'MainError: Main error occurred\n at mainFunction()';
mainError.cause = intermediateError;

logger.fromError('test_action', mainError, { details: 'test details' });

const logArguments = JSON.parse(outputStub.args[0][0]);
expect(logArguments.error.type).to.eql('MainError');
expect(logArguments.error.message).to.eql('Main error occurred');
expect(logArguments.error.cause).to.eql({
type: 'IntermediateError',
message: 'Intermediate error',
stack_trace: intermediateError.stack,
cause: {
type: 'RootCauseError',
message: 'Root cause error',
stack_trace: rootCause.stack,
},
});
});

describe('#customError', () => {
it('should log error as the given severity with action', () => {
const error: Error & { data?: any } = new Error('failed');
Expand Down
18 changes: 12 additions & 6 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,24 @@ export class Logger {
}

return {
error: {
type: error.name,
message: error.message,
context: this.shortenData((error as ErrorWithData).data),
stack_trace: this.shortenStackTrace(error.stack || ''),
},
error: this.extractError(error),
event: {
reason: error.message,
},
};
}

private extractError(error: Error): unknown {
const shortenedData = this.shortenData((error as ErrorWithData).data);
return {
type: error.name,
message: error.message,
...(shortenedData && { context: shortenedData }),
stack_trace: this.shortenStackTrace(error.stack || ''),
...(error.cause instanceof Error && { cause: this.extractError(error.cause) }),
};
}

private getAxiosErrorDetails(error: AxiosError) {
if (!error.isAxiosError) {
return {};
Expand Down
Loading