twreporter-react
Version:
React-Redux site for The Reporter Foundation in Taiwan
18 lines (16 loc) • 442 B
JavaScript
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;
} finally {
fn(err, body);
}
});
};