Skip to content

Commit

Permalink
fix(error-logger): log error code from AxiosError instances in ECS fo…
Browse files Browse the repository at this point in the history
…rmat (i don't care about legacy)

AUT-2772

Co-authored-by: Nagy Richard <[email protected]>
  • Loading branch information
fqqdk and akapa committed Apr 25, 2024
1 parent 7663720 commit d623408
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/logger/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ describe('Logger', () => {
url: 'http://amazinghost.com/beautiful-path',
method: 'get',
};
error.code = 'ECONNREINVENTED';

logger.fromError('hi', error, { details: 'here' });

Expand All @@ -237,6 +238,7 @@ describe('Logger', () => {
expect(logArguments.event.action).to.eql('hi');
expect(logArguments.log.level).to.eql(50);

expect(logArguments.error.code).to.eql(error.code);
expect(logArguments.error.type).to.eql(error.name);
expect(logArguments.error.stack_trace).to.eql(error.stack);
expect(logArguments.error.message).to.eql(error.message);
Expand Down Expand Up @@ -274,6 +276,15 @@ describe('Logger', () => {
expect(logArguments.error).to.not.have.any.keys('context');
});

it('should not log error code when it is undefined in the AxiosError object', () => {
const error = new AxiosError('failed');

logger.customError('warn', 'hi', error, { details: 'here' });

const logArguments = JSON.parse(outputStub.args[0][0]);
expect(logArguments.error).to.not.have.any.keys('code');
});

it('should log only 3000 character of data', () => {
const error: Error & { data?: any } = new Error('failed');
error.data = 'exactlyTen'.repeat(400);
Expand Down
18 changes: 11 additions & 7 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ interface ErrorWithData extends Error {

interface AxiosError extends Error {
isAxiosError: boolean;
config: {
method: string;
url: string;
code?: string;
config?: {
method?: string;
url?: string;
};
response?: {
status: number;
Expand Down Expand Up @@ -196,8 +197,8 @@ export class Logger {

if (Logger.config.outputFormat === 'legacy') {
return {
request_method: error.config.method,
request_url: error.config.url,
request_method: error.config?.method,
request_url: error.config?.url,
response_status: error.response ? error.response.status : undefined,
response_status_text: error.response ? error.response.statusText : undefined,
response_data: error.response ? this.shortenData(error.response.data) : undefined,
Expand All @@ -206,11 +207,11 @@ export class Logger {

return {
url: {
full: error.config.url,
full: error.config?.url,
},
http: {
request: {
method: error.config.method,
method: error.config?.method,
},
response: {
status_code: error.response ? error.response.status : undefined,
Expand All @@ -219,6 +220,9 @@ export class Logger {
},
},
},
error: {
code: error.code,
},
};
}
}

0 comments on commit d623408

Please sign in to comment.