Skip to content

Commit d623408

Browse files
fqqdkakapa
fqqdk
andcommitted
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 <[email protected]>
1 parent 7663720 commit d623408

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/logger/logger.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ describe('Logger', () => {
229229
url: 'http://amazinghost.com/beautiful-path',
230230
method: 'get',
231231
};
232+
error.code = 'ECONNREINVENTED';
232233

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

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

241+
expect(logArguments.error.code).to.eql(error.code);
240242
expect(logArguments.error.type).to.eql(error.name);
241243
expect(logArguments.error.stack_trace).to.eql(error.stack);
242244
expect(logArguments.error.message).to.eql(error.message);
@@ -274,6 +276,15 @@ describe('Logger', () => {
274276
expect(logArguments.error).to.not.have.any.keys('context');
275277
});
276278

279+
it('should not log error code when it is undefined in the AxiosError object', () => {
280+
const error = new AxiosError('failed');
281+
282+
logger.customError('warn', 'hi', error, { details: 'here' });
283+
284+
const logArguments = JSON.parse(outputStub.args[0][0]);
285+
expect(logArguments.error).to.not.have.any.keys('code');
286+
});
287+
277288
it('should log only 3000 character of data', () => {
278289
const error: Error & { data?: any } = new Error('failed');
279290
error.data = 'exactlyTen'.repeat(400);

src/logger/logger.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ interface ErrorWithData extends Error {
1313

1414
interface AxiosError extends Error {
1515
isAxiosError: boolean;
16-
config: {
17-
method: string;
18-
url: string;
16+
code?: string;
17+
config?: {
18+
method?: string;
19+
url?: string;
1920
};
2021
response?: {
2122
status: number;
@@ -196,8 +197,8 @@ export class Logger {
196197

197198
if (Logger.config.outputFormat === 'legacy') {
198199
return {
199-
request_method: error.config.method,
200-
request_url: error.config.url,
200+
request_method: error.config?.method,
201+
request_url: error.config?.url,
201202
response_status: error.response ? error.response.status : undefined,
202203
response_status_text: error.response ? error.response.statusText : undefined,
203204
response_data: error.response ? this.shortenData(error.response.data) : undefined,
@@ -206,11 +207,11 @@ export class Logger {
206207

207208
return {
208209
url: {
209-
full: error.config.url,
210+
full: error.config?.url,
210211
},
211212
http: {
212213
request: {
213-
method: error.config.method,
214+
method: error.config?.method,
214215
},
215216
response: {
216217
status_code: error.response ? error.response.status : undefined,
@@ -219,6 +220,9 @@ export class Logger {
219220
},
220221
},
221222
},
223+
error: {
224+
code: error.code,
225+
},
222226
};
223227
}
224228
}

0 commit comments

Comments
 (0)