Skip to content

Fixing issue where logging responses cannot handle Error objects #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2015
Merged
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
30 changes: 21 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,30 @@ class DSHttpAdapter {
config.url += suffix;
}

// logs the HTTP response
function logResponse(data) {
let str = `${start.toUTCString()} - ${data.config.method.toUpperCase()} ${data.config.url} - ${data.status} ${(new Date().getTime() - start.getTime())}ms`;
if (data.status >= 200 && data.status < 300) {
if (_this.defaults.log) {
_this.defaults.log(str, data);
// examine the data object
if (data instanceof Error) {
// log the Error object
_this.defaults.error(`'FAILED: ${data.message || 'Unknown Error'}'`, data);
return DSUtils.Promise.reject(data);
} else if (data instanceof Object) {
let str = `${start.toUTCString()} - ${data.config.method.toUpperCase()} ${data.config.url} - ${data.status} ${(new Date().getTime() - start.getTime())}ms`;

if (data.status >= 200 && data.status < 300) {
if (_this.defaults.log) {
_this.defaults.log(str, data);
}
return data;
} else {
if (_this.defaults.error) {
_this.defaults.error(`'FAILED: ${str}'`, data);
}
return DSUtils.Promise.reject(data);
}
return data;
} else {
if (_this.defaults.error) {
_this.defaults.error(`'FAILED: ${str}`, data);
}
// unknown type for 'data' that is not an Object or Error
_this.defaults.error(`'FAILED'`, data);
return DSUtils.Promise.reject(data);
}
}
Expand Down Expand Up @@ -289,4 +302,3 @@ class DSHttpAdapter {
}

module.exports = DSHttpAdapter;