Skip to content

Instantly share code, notes, and snippets.

@kylerush
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save kylerush/9361443 to your computer and use it in GitHub Desktop.

Select an option

Save kylerush/9361443 to your computer and use it in GitHub Desktop.
jQuery.get('/account/info').always(function(data, textStatus, jqXHR){
if(jqXHR.status === 200){
try{
if(typeof data.userName === 'string'){
if(data.userName !== ''){
jQuery('#user-name').text(data.userName);
} else {
//send error to google analytics
_gaq.push(['_trackEvent', 'api error', 'api_name', 'data.userName is empty string')]);
}
} else {
//send error to google analytics
_gaq.push(['_trackEvent', 'api error', 'api_name', 'typeof data.userName: ' + typeof(data.user.name)]);
}
} catch(error){
//handle the error, update the DOM to inform user the api is down
//send error to google analytics
_gaq.push(['_trackEvent', 'api error', 'api_name', 'response contains invalid JSON')]);
}
} else {
//send data to google analytics
_gaq.push(['_trackEvent', 'api error', 'api_name', 'status code: ' + jqXHR.status]);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment