Sha256: b291bb31261f5bff0b5903ece131631473139329e3cc0a8404e3d97c9f56a7d0

Contents?: true

Size: 1.77 KB

Versions: 29

Compression:

Stored size: 1.77 KB

Contents

parseJson = JSON.parse || jQuery.parseJSON

#Send the error to the same host that served this file (I think)
class @WatCatcher
  constructor: (target=window) ->
    scripts = document.getElementsByTagName("script");
    node = scripts[scripts.length - 1];
    @attrs = {}
    for attr in node.attributes
      attrs = /data-(.*)/.exec(attr.nodeName)
      if attrs?
        @attrs[attrs[1]] = attr.nodeValue


    @app_user = parseJson(@attrs["app-user"]) if (parseJson? && @attrs["app-user"])

    @oldErrorHandler = target.onerror
    target.onerror = @watHandler

  toQuery: (params, prefix="") ->
    query = ""

    unless params instanceof Object
      return "#{prefix}=#{params}&"

    for k, v of params
      k = encodeURIComponent(k)
      k = if prefix == "" then k else "#{prefix}[#{k}]"
      if v instanceof Array
        for i in v
          query += @toQuery(i, "#{k}[]")
      else if v instanceof Object
        query += "#{@toQuery(v, k)}"
      else
        query += "#{k}=#{encodeURIComponent(v)}&"
    query

  watHandler: (msg, url, line) =>
    try
      params = {
        wat: {
          page_url:  window.location.toString()
          message:   msg
          backtrace: [url+":"+line]
          language: "javascript"
          app_user: @app_user
        }
      }

      xmlhttp = if window.XMLHttpRequest
          new XMLHttpRequest()
      else
          new ActiveXObject("Microsoft.XMLHTTP")

      xmlhttp.open("POST", @attrs.route, true);
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
      xmlhttp.send(@toQuery(params));

    catch error
      if typeof @oldErrorHandler == 'function'
        @oldErrorHandler(arguments...)


window.watCatcher = new WatCatcher()

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
wat_catcher-0.8.2 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.8.1 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.8.0 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.7.2 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.7.1 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.7.0 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.6.0 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.5.5 vendor/assets/javascripts/wat_catcher.coffee
wat_catcher-0.5.4 vendor/assets/javascripts/wat_catcher.coffee