aboutsummaryrefslogtreecommitdiff
path: root/node_modules/superagent/lib/node/parsers/json.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/superagent/lib/node/parsers/json.js')
-rw-r--r--node_modules/superagent/lib/node/parsers/json.js19
1 files changed, 19 insertions, 0 deletions
diff --git a/node_modules/superagent/lib/node/parsers/json.js b/node_modules/superagent/lib/node/parsers/json.js
new file mode 100644
index 0000000..b71662d
--- /dev/null
+++ b/node_modules/superagent/lib/node/parsers/json.js
@@ -0,0 +1,19 @@
+
+module.exports = function parseJSON(res, fn){
+ res.text = '';
+ res.setEncoding('utf8');
+ res.on('data', function(chunk){ res.text += chunk;});
+ res.on('end', function(){
+ try {
+ var body = res.text && JSON.parse(res.text);
+ } catch (e) {
+ var err = e;
+ // issue #675: return the raw response if the response parsing fails
+ err.rawResponse = res.text || null;
+ // issue #876: return the http status code if the response parsing fails
+ err.statusCode = res.statusCode;
+ } finally {
+ fn(err, body);
+ }
+ });
+};