Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A real-world example of TraceKit based on my current contract project

API

  • TraceKit.report.subscribe(function(stackInfo) { ... })
  • TraceKit.report.unsubscribe(function(stackInfo) { ... })
  • TraceKit.report(exception) (e.g. try { ...code... } catch(ex) { TraceKit.report(ex); } )

TraceKit will attempt to fetch an analyze source files, but you can turn this off using:

TraceKit.remoteFetching = false;

You can also tell TraceKit to ignore global window errors with:

TraceKit.collectWindowErrors = false;

Examples

Collect all window errors, and any callback in

TraceKit.report.subscribe(function rogersJs(jsonErrorReport) {
  if (dev) { // If you're developing right now, just log to console
    console.error('TraceKit caught:', jsonErrorReport
  } else {
    $.post('/jserrorReport', jsonErrorReport)
    .fail(function dearUser() {
      alert('How ridiculous! We failed to report an error! Please report to support@domain.com');
    })
  }
});

For some third party code, perhaps a bookmarklet or site plugin, or wordpress plugin:

TraceKit.collectWindowErrors = false;
TraceKit.report.subscribe(function bookmarkletErrorReporter(jsonReport) {
  if (dev) {
    console.error('TraceKit caught an error:', jsonReport);
  } else {
    $.post('/jserrorReport', jsonReport);
  }
});

More to come when I have time, or when you bug me @DevinRhode2

Way too comprehensive example:

But there's a lot to learn, it you care to read/review.

/**
 * First attempt at a really comprensive example of using TraceKit for catching ALL errors EVAR
 *
 * first include or compile in TraceKit.js. It's tested on all browsers down to IE6 so this won't throw any errors.
 * And if it does, you'll find out right away, when developing! I digress.
 *
 * There is limited support for Nodejs environments. Notably, make sure console.log writes to to a file in production.
 * node also throws the error 
 *
 * for TraceKit.report.subscribe, you could have this just console.log to a file, or write this to a database for an error dashboard
 * You could consider a service like nodefly.com or newrelic.com or something else.
 * Also watch out for SourceNinja.com for preventing errors with upgrading to new libraries.
 */

var exceptionalException;
(function globalWrapper() {
  try {
    //this comes first because exceptionalException needs to know if we're in node or the browser
    var dev = false;
    if (typeof (window) === 'undefined') {
      //we're in nodejs
      //confirm is used for when errors fail to report. In nodejs, this alias's to console.log.
      //You probably want to alias this something to write the message to a file if console.log output isn't already
      var confirm = console.log;
      var node = true;
    } else {
      //we're in the browser
      var node = false;
    }
    exceptionalException = function exceptionalException(e) {
      //confirm because some team members of some sites may noop alert, but probably not confirm.
      confirm('How ridiculous! Failed to report error: ' +
        (e && e.message ? e.message : 'Unknown error! Crap!') +
        '\n\nPLEASE email this to support@domain.com');
        //You might find a service that can hook into github issues by
        //going to the repo on github, then "Settings" -> "Service hooks"
        //Perhaps you find a service hook to have support emails also get put into github issues..! (COMMENT IF YOU DO)
    };
    
    //if browser
    if (!node && typeof (location) !== 'undefined' && location.hostname === 'localhost') {
      dev = true;
      
    //if node
    } else if (process && process.env) {
      if (process.env.NODE_ENV !== 'production' || process.env.PORT !== 80) {
        dev = true;
      }
    }
    
    TraceKit.report.subscribe(function TraceKitCollector(jsonErrorReport) {
      if (dev) {
        console.error('TraceKit caught:', jsonErrorReport);
        throw 'killing script'; //more straightforward for developers to read than `new Error`
      } else {
        var jsonReport = JSON.stringify(jsonErrorReport);
        if (!node) {
          $.ajax({
            url: 'http://parsing-api.trackif.com/jserror',
            data: jsonReport
          }).fail(function failedToReport() {
            alert('ERROR Failed to report this error: \n' + jsonReport + '\n\n' +
              'PLEASE report this to use at support@trackif.com or elsewhere.');
          });
        } else {
          console.log(jsonReport);
        }
      }
    });
    
  } catch (e) {
      if (exceptionalException) {
          exceptionalException(e);
      } else {
          confirm('How ridiculous! exceptionalException is falsey!' +
            'Please report to DevinRhode2@gmail.com, @DevinRhode2 on twitter, or DevinRhode2 anywhere else, like instagram!');
      }
  }
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.