Skip to content

Instantly share code, notes, and snippets.

@oaleynik
Created August 27, 2013 15:21
Show Gist options
  • Save oaleynik/6355007 to your computer and use it in GitHub Desktop.
Save oaleynik/6355007 to your computer and use it in GitHub Desktop.
var r = request.send(uri, options, function(e, res, body) {
if (e) {
// error in request
args.callback(e);
}
// What if 3xx status code? Correct: you will get response data wrapped into Error :))
else if (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 202) {
// provide a default message when none is provided
body = body || 'server returned status code ' + res.statusCode;
// eror in response
args.callback(new Error(body));
}
else {
try {
body = JSON.parse(body);
}
catch(err) {
// continue but data untouched (likely image data)
}
if (typeof body.error === 'string') {
// api response includes an error
args.callback(new Error(body.error));
}
else {
// api response is successful
args.callback(null, body);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment