rittenhop-ghost/versions/5.94.2/node_modules/superagent/lib/node/parsers/json.js

25 lines
592 B
JavaScript
Raw Normal View History

"use strict";
module.exports = function (res, fn) {
res.text = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
res.text += chunk;
});
res.on('end', function () {
var body;
var err;
try {
body = res.text && JSON.parse(res.text);
} catch (err2) {
err = err2; // 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);
}
});
};