Trackets client testing

by darthdeus

JavaScript

/**
 * This is a client side error tracking script for Trackets.
 *
 * First you need to initialize the tracking with an API key.
 *
 *     Trackets.init({ api_key: "XXX" });
 *
 * You can also provide one of the following options to further
 * customize the tracking:
 *
 * - `api_base_url` for a custom API endpoint
 * - `custom_data` which can be any arbitrary object which si stored
 *   along with each error report. You can use it for example for
 *   custom data about the current user session.
 * - `callback` which is invoked with every captured error
 */
(function(window, document) {
  var CHROME_REGEX = /at (\w+) \((.+?):(\d+):\d+\)/;

  // Safari and Firefox use the same format for stacktraces,
  // hence we can use the same regex for both
  var FIREFOX_AND_SAFARI_REGEX = /(\w+)@(.+):(\d+)/;

  var PARSERS = [FIREFOX_AND_SAFARI_REGEX, CHROME_REGEX];

  /**
   * Match stacktrace line against all available regular expressions for
   * all browsers and return the first match.
   */
  function matchStackLine(line) {
    var match;

    for (var i = 0; i < PARSERS.length; i++) {
      if (match = line.match(PARSERS[i])) {
        return match;
      }
    }
    return null;
  }

  /**
   * Parse stacktrace from any browser and return a unified representation
   */
  function parseStack(stack) {
    var lines = stack.split("\n");
    var results = [];
    for (var i = 0; i < lines.length; i++) {
      var match = matchStackLine(lines[i]);
      if (match) {
        results.push({
          "function": match[1],
          "file": match[2],
          "line": match[3]
        });
      }
    }
    return results;
  }

  /**
   * Join a parsed stacktrace into a single string again.
   */
  function joinParsedStack(lines) {
    var results = [];

    for (var i = 0; i < lines.length; i++) {
      var line = lines[i];
      results.push(line["function"] + " at " + line["file"] + ":" + line["line"]);
    }

    return results.join("\n");
  }

  /**
   * Parse stacktrace...