Skip to content

Commit

Permalink
Merge pull request #9 from Receiptful/trim
Browse files Browse the repository at this point in the history
Trim the body string for JSON parse
  • Loading branch information
RickCraig committed Apr 5, 2016
2 parents ddc6c4d + 1c5f639 commit 92de150
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,17 @@ class Request {

var json = {};
if (typeof body === 'string' && body.trim() !== '') {
json = JSON.parse(body);
json = JSON.parse(body.trim());
} else if (typeof body === 'object') {
json = body;
}

if (json.hasOwnProperty('error') ||
json.hasOwnProperty('errors')) {
const errorMessage = json.error || JSON.stringify(json.errors);
const serverError = this.error(errorMessage, response, body);
cb(serverError, body, response);
reject(serverError);
return;
if (json.hasOwnProperty('error') || json.hasOwnProperty('errors')) {
const errorMessage = json.error || JSON.stringify(json.errors);
const serverError = this.error(errorMessage, response, body);
cb(serverError, body, response);
reject(serverError);
return;
}

logger.info('Request complete, returning data and response.');
Expand Down

0 comments on commit 92de150

Please sign in to comment.