Skip to content

Instantly share code, notes, and snippets.

@dominiek
Created February 3, 2011 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dominiek/809519 to your computer and use it in GitHub Desktop.
Save dominiek/809519 to your computer and use it in GitHub Desktop.
Most fucked-up code I've written in 2010. Converts error data from Twitter to my own internal error objects.
exports.convertTwitterError = function(error_str) {
if(typeof error_str == 'object') {
var code = 500;
if(error_str.code) {
code = error_str.code;
}
if(error_str.status) {
code = error_str.status;
}
if(error_str.statusCode) {
code = error_str.statusCode;
}
var error = error_str;
if(error_str.data) {
error = error_str.data;
try {
error = JSON.parse(error);
error = error.error;
} catch(pe) {
}
}
return {'status': 'error', 'code': code, 'error': error};
}
m = error_str.match(/^(\d+)\s+:\s+(.+)/);
if(!m) {
if(error_str.match(/\<html\>/)) {
error_str = "Twitter is over capacity.";
}
return {'status': 'error', 'code': m[1], 'error': error_str};
}
sys.puts(sys.inspect(m));
var error = null;
try {
error = JSON.parse(m[2]);
} catch(pe) {
return {'status': 'error', 'code': 500, 'error': error_str};
}
return {'status': 'error', 'code': parseInt(m[1]), 'error': 'Twitter says: '+error.error};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment