Skip to content

Instantly share code, notes, and snippets.

@coreyti
Created November 30, 2012 22:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyti/4179190 to your computer and use it in GitHub Desktop.
Save coreyti/4179190 to your computer and use it in GitHub Desktop.
blacklist select javascript errors from being reported to Airbrake/Hoptoad
<%#
NOTES:
* Works with current version (0.1.0) notifier.js hosted at:
http://airbrake.io/javascripts/notifier.js (also https://...)
* Does not need to be HTML or ERB. You just need some way to get your
Airbrake API key and other server-side variables into the javascript.
* This example uses jQuery to load the notifier.js script. Feel free to
adjust as desired for framework-independence.
%>
<%- if Airbrake.configuration.public? -%>
<script type="text/javascript">
(function($){
var airbrake = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'airbrake.io/javascripts/notifier.js';
$.getScript(airbrake, function() {
var actual, blacklist;
// Standard Airbrake setup:
// ----------------------------------------------------------------------
window.Airbrake = (typeof(Airbrake) == 'undefined' && typeof(Hoptoad) != 'undefined') ? Hoptoad : Airbrake;
// Configure as needed:
Airbrake.setKey('<%= Airbrake.configuration.api_key %>');
Airbrake.setHost('<%= Airbrake.configuration.host.dup %>');
Airbrake.setEnvironment('<%= Airbrake.configuration.environment_name %>');
Airbrake.setErrorDefaults({
url : "<%= escape_javascript(request.url) %>",
component : "<%= controller.controller_name %>",
action : "<%= controller.action_name %>"
});
// Blacklist patch and config:
// ----------------------------------------------------------------------
// Capture actual/original `.notify`:
actual = Airbrake.notify;
// Add error message substrings to ignore here. Other possibilities to
// consider at: https://gist.github.com/1878283
blacklist = [
'atomicFindClose', // mysterious mobile Safari error
'Script error.', // http://stackoverflow.com/questions/5913978/cryptic-script-error-reported-in-javascript-in-chrome-and-firefox
];
Airbrake.notify = function(error) {
if( ! Airbrake.blacklisted(error)) {
actual.call(this, error);
}
};
Airbrake.blacklisted = function(error) {
var message = error.message;
if(message) {
for(i = 0, n = blacklist.length; i < n; i++) {
if(message.indexOf(blacklist[i]) > -1) {
return true;
}
}
}
return false;
};
});
})(jQuery);
</script>
<%- end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment