You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
571 B
22 lines
571 B
'use strict';
|
|
|
|
module.exports = function parseJSON(res, fn){
|
|
res.text = '';
|
|
res.setEncoding('utf8');
|
|
res.on('data', chunk => {
|
|
res.text += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
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);
|
|
}
|
|
});
|
|
};
|
|
|