Created
February 3, 2011 14:19
-
-
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.
This file contains hidden or 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
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