Skip to content

Commit cfbe1c2

Browse files
authored
Merge pull request #85 from knight42/parse-cause
The `cause` could be a JSON string
2 parents c884aa7 + 0329f94 commit cfbe1c2

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

lib/modem.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
function parseJSON(s) {
2+
try {
3+
return JSON.parse(s);
4+
} catch (e) {
5+
return null;
6+
}
7+
}
8+
19
var querystring = require('querystring'),
210
http = require('./http'),
311
fs = require('fs'),
@@ -217,12 +225,7 @@ Modem.prototype.buildRequest = function(options, context, data, callback) {
217225
res.on('end', function() {
218226
debug('Received: %s', chunks);
219227

220-
var json;
221-
try {
222-
json = JSON.parse(chunks);
223-
} catch (e) {
224-
json = chunks;
225-
}
228+
var json = parseJSON(chunks) || chunks;
226229
self.buildPayload(null, context.isStream, context.statusCodes, false, req, res, json, callback);
227230
});
228231
}
@@ -275,7 +278,7 @@ Modem.prototype.buildPayload = function(err, isStream, statusCodes, openStdin, r
275278
chunks += chunk;
276279
});
277280
res.on('end', function() {
278-
callback(null, chunks);
281+
callback(null, parseJSON(chunks) || chunks);
279282
});
280283
} else {
281284
callback(null, json);

0 commit comments

Comments
 (0)