Skip to content

Commit 4524481

Browse files
authored
Merge pull request #4 from emartech/axios
feat(logger): display request/response details for Axios errors
2 parents 1e8e37b + e923083 commit 4524481

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Same as info with fatal log level.
8989
##### JsonLogger.prototype.fromError(action, data)
9090

9191
Displays an error object which formatted to fit into one line.
92-
The displayed line contains the stack trace, the name and the message of the error.
92+
The displayed line contains the stack trace, the name and the message of the error (for Axios errors, also request and response details).
9393
The log level defaults to error.
9494

9595
```javascript
@@ -157,7 +157,7 @@ You need to use the middlewares of `@emartech/cls-adapter` and add its transform
157157
This way it will log the request identifier coming from the header field (`X-Request-Id`) to every log line
158158
where the called function is originating from the route handler.
159159

160-
For automatting
160+
For automating
161161

162162
```javascript
163163
const Koa = require('koa');

src/logger/logger.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,22 @@ class Logger {
6666
}
6767

6868
_getErrorDetails(error) {
69-
return {
69+
const details = {
7070
error_name: error.name,
7171
error_stack: this._shortenStackTrace(error.stack),
7272
error_message: error.message,
7373
error_data: this._shortenData(error.data)
7474
};
75+
76+
if (error.isAxiosError) {
77+
details.request_method = error.config.method;
78+
details.request_url = error.config.url;
79+
details.response_status = error.response.status;
80+
details.response_status_text = error.response.statusText;
81+
details.response_data = this._shortenData(error.response.data);
82+
}
83+
84+
return details;
7585
}
7686
}
7787

src/logger/logger.spec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,36 @@ describe('Logger', function() {
121121
expect(logArguments.error_data.length).to.eql(3004);
122122
});
123123

124+
it('should log request/response details for Axios-like error objects', function() {
125+
const error = new Error('Request failed with status code 500');
126+
error.isAxiosError = true;
127+
error.response = {
128+
status: 500,
129+
statusText: 'Something horrible happened',
130+
data: { useful_detail: 'important info' }
131+
};
132+
error.config = {
133+
url: 'http://amazinghost.com/beautiful-path',
134+
method: 'get'
135+
};
136+
137+
logger.fromError('hi', error, { details: 'here' });
138+
139+
const logArguments = JSON.parse(Logger.config.output.args[0]);
140+
expect(logArguments.name).to.eql('mongo');
141+
expect(logArguments.action).to.eql('hi');
142+
expect(logArguments.level).to.eql(50);
143+
144+
expect(logArguments.error_name).to.eql(error.name);
145+
expect(logArguments.error_stack).to.eql(error.stack);
146+
expect(logArguments.error_message).to.eql(error.message);
147+
expect(logArguments.request_method).to.eql(error.config.method);
148+
expect(logArguments.request_url).to.eql(error.config.url);
149+
expect(logArguments.response_status).to.eql(error.response.status);
150+
expect(logArguments.response_status_text).to.eql(error.response.statusText);
151+
expect(logArguments.response_data).to.eql(JSON.stringify(error.response.data));
152+
});
153+
124154
describe('#customError', function() {
125155
it('should log error as the given severity with action', function() {
126156
const error = new Error('failed');

0 commit comments

Comments
 (0)