From d623408d3e5c42f2479e7b89d07c558e184fce48 Mon Sep 17 00:00:00 2001 From: fqqdk Date: Thu, 25 Apr 2024 15:59:44 +0200 Subject: [PATCH] fix(error-logger): log error code from AxiosError instances in ECS format (i don't care about legacy) AUT-2772 Co-authored-by: Nagy Richard --- src/logger/logger.spec.ts | 11 +++++++++++ src/logger/logger.ts | 18 +++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/logger/logger.spec.ts b/src/logger/logger.spec.ts index 7e058ef..d99da8a 100644 --- a/src/logger/logger.spec.ts +++ b/src/logger/logger.spec.ts @@ -229,6 +229,7 @@ describe('Logger', () => { url: 'http://amazinghost.com/beautiful-path', method: 'get', }; + error.code = 'ECONNREINVENTED'; logger.fromError('hi', error, { details: 'here' }); @@ -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); @@ -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); diff --git a/src/logger/logger.ts b/src/logger/logger.ts index 3b9a3d1..ab77270 100644 --- a/src/logger/logger.ts +++ b/src/logger/logger.ts @@ -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; @@ -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, @@ -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, @@ -219,6 +220,9 @@ export class Logger { }, }, }, + error: { + code: error.code, + }, }; } }