Created
August 27, 2013 15:21
-
-
Save oaleynik/6355007 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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