Commit 3d7ebed36569723961ad7ec0e9e3344537756ceb

Authored by Nathan Broadbent
1 parent 0cbc9681
Exists in master and in 1 other branch production

Added tweaked notifier.js (API ver 2.0) for reporting javascript exceptions

Showing 1 changed file with 293 additions and 0 deletions   Show diff stats
public/javascripts/notifier.js 0 → 100644
... ... @@ -0,0 +1,293 @@
  1 +var Hoptoad = {
  2 + VERSION : '2.0',
  3 + NOTICE_XML : '<?xml version="1.0" encoding="UTF-8"?>\
  4 + <notice version="2.0">\
  5 + <api-key></api-key>\
  6 + <notifier>\
  7 + <name>hoptoad_notifier_js</name>\
  8 + <version>2.0</version>\
  9 + <url>http://hoptoadapp.com</url>\
  10 + </notifier>\
  11 + <error>\
  12 + <class>EXCEPTION_CLASS</class>\
  13 + <message>EXCEPTION_MESSAGE</message>\
  14 + <backtrace>BACKTRACE_LINES</backtrace>\
  15 + </error>\
  16 + <request>\
  17 + <url>REQUEST_URL</url>\
  18 + <component>REQUEST_COMPONENT</component>\
  19 + <action>REQUEST_ACTION</action>\
  20 + </request>\
  21 + <server-environment>\
  22 + <project-root>PROJECT_ROOT</project-root>\
  23 + <environment-name>production</environment-name>\
  24 + </server-environment>\
  25 + </notice>',
  26 + ROOT : window.location.protocol + '//' + window.location.host,
  27 + BACKTRACE_MATCHER : /^(.*)\@(.*)\:(\d+)$/,
  28 + backtrace_filters : [/notifier\.js/],
  29 +
  30 + notify: function(error) {
  31 + var xml = escape(Hoptoad.generateXML(error));
  32 + var host = Hoptoad.host || 'hoptoadapp.com';
  33 + var url = '//' + host + '/notifier_api/v2/notices?data=' + xml;
  34 + var request = document.createElement('iframe');
  35 +
  36 + request.style.width = '1px';
  37 + request.style.height = '1px';
  38 + request.style.display = 'none';
  39 + request.src = url;
  40 +
  41 + document.getElementsByTagName('head')[0].appendChild(request);
  42 + },
  43 +
  44 + setEnvironment: function(value) {
  45 + var matcher = /<environment-name>.*<\/environment-name>/;
  46 +
  47 + Hoptoad.NOTICE_XML = Hoptoad.NOTICE_XML.replace(matcher,
  48 + '<environment-name>' +
  49 + value +
  50 + '</environment-name>')
  51 + },
  52 +
  53 + setHost: function(value) {
  54 + Hoptoad.host = value;
  55 + },
  56 +
  57 + setKey: function(value) {
  58 + var matcher = /<api-key>.*<\/api-key>/;
  59 +
  60 + Hoptoad.NOTICE_XML = Hoptoad.NOTICE_XML.replace(matcher,
  61 + '<api-key>' +
  62 + value +
  63 + '</api-key>');
  64 + },
  65 +
  66 + setErrorDefaults: function(value) {
  67 + Hoptoad.errorDefaults = value;
  68 + },
  69 +
  70 + generateXML: function(errorWithoutDefaults) {
  71 + var error = Hoptoad.mergeDefault(Hoptoad.errorDefaults, errorWithoutDefaults);
  72 +
  73 + var xml = Hoptoad.NOTICE_XML;
  74 + var url = Hoptoad.escapeText(error.url || '');
  75 + var component = Hoptoad.escapeText(error.component || '');
  76 + var action = Hoptoad.escapeText(error.action || '');
  77 + var type = Hoptoad.escapeText(error.type || 'Error');
  78 + var message = Hoptoad.escapeText(error.message || 'Unknown error.');
  79 + var backtrace = Hoptoad.generateBacktrace(error);
  80 +
  81 +
  82 + if (Hoptoad.trim(url) == '' && Hoptoad.trim(component) == '') {
  83 + xml = xml.replace(/<request>.*<\/request>/, '');
  84 + } else {
  85 + var data = '';
  86 +
  87 + var cgi_data = error['cgi-data'] || {};
  88 + cgi_data["HTTP_USER_AGENT"] = navigator.userAgent;
  89 + data += '<cgi-data>';
  90 + data += Hoptoad.generateVariables(cgi_data);
  91 + data += '</cgi-data>';
  92 +
  93 + var methods = ['params', 'session'];
  94 +
  95 + for (var i = 0; i < 2; i++) {
  96 + var type = methods[i];
  97 +
  98 + if (error[type]) {
  99 + data += '<' + type + '>';
  100 + data += Hoptoad.generateVariables(error[type]);
  101 + data += '</' + type + '>';
  102 + }
  103 + }
  104 +
  105 + xml = xml.replace('</request>', data + '</request>')
  106 + .replace('REQUEST_URL', url)
  107 + .replace('REQUEST_ACTION', action)
  108 + .replace('REQUEST_COMPONENT', component);
  109 + }
  110 +
  111 + return xml.replace('PROJECT_ROOT', Hoptoad.ROOT)
  112 + .replace('EXCEPTION_CLASS', type)
  113 + .replace('EXCEPTION_MESSAGE', message)
  114 + .replace('BACKTRACE_LINES', backtrace.join(''));
  115 + },
  116 +
  117 + generateBacktrace: function(error) {
  118 + error = error || {};
  119 +
  120 + if (typeof error.stack != 'string') {
  121 + try {
  122 + (0)();
  123 + } catch(e) {
  124 + error.stack = e.stack;
  125 + }
  126 + }
  127 +
  128 + var backtrace = [];
  129 + var stacktrace = Hoptoad.getStackTrace(error);
  130 +
  131 + for (var i = 0, l = stacktrace.length; i < l; i++) {
  132 + var line = stacktrace[i];
  133 + var matches = line.match(Hoptoad.BACKTRACE_MATCHER);
  134 +
  135 + if (matches && Hoptoad.validBacktraceLine(line)) {
  136 + var file = matches[2].replace(Hoptoad.ROOT, '[PROJECT_ROOT]');
  137 +
  138 + if (i == 0) {
  139 + if (matches[2].match(document.location.href)) {
  140 + backtrace.push('<line method="" file="internal: " number=""/>');
  141 + }
  142 + }
  143 +
  144 + backtrace.push('<line method="' + Hoptoad.escapeText(matches[1]) +
  145 + '" file="' + Hoptoad.escapeText(file) +
  146 + '" number="' + matches[3] + '" />');
  147 + }
  148 + }
  149 +
  150 + return backtrace;
  151 + },
  152 +
  153 + getStackTrace: function(error) {
  154 + var stacktrace = printStackTrace({ e : error, guess : false });
  155 +
  156 + for (var i = 0, l = stacktrace.length; i < l; i++) {
  157 + if (stacktrace[i].match(/\:\d+$/)) {
  158 + continue;
  159 + }
  160 +
  161 + if (stacktrace[i].indexOf('@') == -1) {
  162 + stacktrace[i] += '@unsupported.js';
  163 + }
  164 +
  165 + stacktrace[i] += ':0';
  166 + }
  167 +
  168 + return stacktrace;
  169 + },
  170 +
  171 + validBacktraceLine: function(line) {
  172 + for (var i = 0; i < Hoptoad.backtrace_filters.length; i++) {
  173 + if (line.match(Hoptoad.backtrace_filters[i])) {
  174 + return false;
  175 + }
  176 + }
  177 +
  178 + return true;
  179 + },
  180 +
  181 + generateVariables: function(parameters) {
  182 + var key;
  183 + var result = '';
  184 +
  185 + for (key in parameters) {
  186 + result += '<var key="' + Hoptoad.escapeText(key) + '">' +
  187 + Hoptoad.escapeText(parameters[key]) +
  188 + '</var>';
  189 + }
  190 +
  191 + return result;
  192 + },
  193 +
  194 + escapeText: function(text) {
  195 + return text.replace(/&/g, '&#38;')
  196 + .replace(/</g, '&#60;')
  197 + .replace(/>/g, '&#62;')
  198 + .replace(/'/g, '&#39;')
  199 + .replace(/"/g, '&#34;');
  200 + },
  201 +
  202 + trim: function(text) {
  203 + return text.toString().replace(/^\s+/, '').replace(/\s+$/, '');
  204 + },
  205 +
  206 + mergeDefault: function(defaults, hash) {
  207 + var cloned = {};
  208 + var key;
  209 +
  210 + for (key in hash) {
  211 + cloned[key] = hash[key];
  212 + }
  213 +
  214 + for (key in defaults) {
  215 + if (!cloned.hasOwnProperty(key)) {
  216 + cloned[key] = defaults[key];
  217 + }
  218 + }
  219 +
  220 + return cloned;
  221 + }
  222 +};
  223 +
  224 +
  225 +
  226 +
  227 +// Domain Public by Eric Wendelin http://eriwen.com/ (2008)
  228 +// Luke Smith http://lucassmith.name/ (2008)
  229 +// Loic Dachary <loic@dachary.org> (2008)
  230 +// Johan Euphrosine <proppy@aminche.com> (2008)
  231 +// Øyvind Sean Kinsey http://kinsey.no/blog (2010)
  232 +//
  233 +// Information and discussions
  234 +// http://jspoker.pokersource.info/skin/test-printstacktrace.html
  235 +// http://eriwen.com/javascript/js-stack-trace/
  236 +// http://eriwen.com/javascript/stacktrace-update/
  237 +// http://pastie.org/253058
  238 +// http://browsershots.org/http://jspoker.pokersource.info/skin/test-printstacktrace.html
  239 +//
  240 +//
  241 +// guessFunctionNameFromLines comes from firebug
  242 +//
  243 +// Software License Agreement (BSD License)
  244 +//
  245 +// Copyright (c) 2007, Parakey Inc.
  246 +// All rights reserved.
  247 +//
  248 +// Redistribution and use of this software in source and binary forms, with or without modification,
  249 +// are permitted provided that the following conditions are met:
  250 +//
  251 +// * Redistributions of source code must retain the above
  252 +// copyright notice, this list of conditions and the
  253 +// following disclaimer.
  254 +//
  255 +// * Redistributions in binary form must reproduce the above
  256 +// copyright notice, this list of conditions and the
  257 +// following disclaimer in the documentation and/or other
  258 +// materials provided with the distribution.
  259 +//
  260 +// * Neither the name of Parakey Inc. nor the names of its
  261 +// contributors may be used to endorse or promote products
  262 +// derived from this software without specific prior
  263 +// written permission of Parakey Inc.
  264 +//
  265 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  266 +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  267 +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  268 +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  269 +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  270 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  271 +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  272 +// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  273 +function printStackTrace(a){var b=a&&a.e?a.e:null;a=a?!!a.guess:true;var c=new printStackTrace.implementation;b=c.run(b);return a?c.guessFunctions(b):b}printStackTrace.implementation=function(){};
  274 +printStackTrace.implementation.prototype={run:function(a){var b=this._mode||this.mode();if(b==="other")return this.other(arguments.callee);else{var c;if(!(c=a))a:{try{0()}catch(d){c=d;break a}c=void 0}a=c;return this[b](a)}},mode:function(){try{0()}catch(a){if(a.arguments)return this._mode="chrome";else if(a.stack)return this._mode="firefox";else if(window.opera&&!("stacktrace"in a))return this._mode="opera"}return this._mode="other"},chrome:function(a){return a.stack.replace(/^.*?\n/,"").replace(/^.*?\n/,
  275 +"").replace(/^.*?\n/,"").replace(/^[^\(]+?[\n$]/gm,"").replace(/^\s+at\s+/gm,"").replace(/^Object.<anonymous>\s*\(/gm,"{anonymous}()@").split("\n")},firefox:function(a){return a.stack.replace(/^.*?\n/,"").replace(/(?:\n@:0)?\s+$/m,"").replace(/^\(/gm,"{anonymous}(").split("\n")},opera:function(a){a=a.message.split("\n");var b=/Line\s+(\d+).*?script\s+(http\S+)(?:.*?in\s+function\s+(\S+))?/i,c,d,e;c=4;d=0;for(e=a.length;c<e;c+=2)if(b.test(a[c]))a[d++]=(RegExp.$3?RegExp.$3+"()@"+RegExp.$2+RegExp.$1:
  276 +"{anonymous}()@"+RegExp.$2+":"+RegExp.$1)+" -- "+a[c+1].replace(/^\s+/,"");a.splice(d,a.length-d);return a},other:function(a){for(var b=/function\s*([\w\-$]+)?\s*\(/i,c=[],d=0,e,f;a&&c.length<10;){e=b.test(a.toString())?RegExp.$1||"{anonymous}":"{anonymous}";f=Array.prototype.slice.call(a.arguments);c[d++]=e+"("+printStackTrace.implementation.prototype.stringifyArguments(f)+")";if(a===a.caller&&window.opera)break;a=a.caller}return c},stringifyArguments:function(a){for(var b=0;b<a.length;++b){var c=
  277 +a[b];if(typeof c=="object")a[b]="#object";else if(typeof c=="function")a[b]="#function";else if(typeof c=="string")a[b]='"'+c+'"'}return a.join(",")},sourceCache:{},ajax:function(a){var b=this.createXMLHTTPObject();if(b){b.open("GET",a,false);b.setRequestHeader("User-Agent","XMLHTTP/1.0");b.send("");return b.responseText}},createXMLHTTPObject:function(){for(var a,b=[function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Msxml3.XMLHTTP")},
  278 +function(){return new ActiveXObject("Microsoft.XMLHTTP")}],c=0;c<b.length;c++)try{a=b[c]();this.createXMLHTTPObject=b[c];return a}catch(d){}},getSource:function(a){a in this.sourceCache||(this.sourceCache[a]=this.ajax(a).split("\n"));return this.sourceCache[a]},guessFunctions:function(a){for(var b=0;b<a.length;++b){var c=a[b],d=/{anonymous}\(.*\)@(\w+:\/\/([-\w\.]+)+(:\d+)?[^:]+):(\d+):?(\d+)?/.exec(c);if(d){var e=d[1];d=d[4];if(e&&d){e=this.guessFunctionName(e,d);a[b]=c.replace("{anonymous}",e)}}}return a},
  279 +guessFunctionName:function(a,b){try{return this.guessFunctionNameFromLines(b,this.getSource(a))}catch(c){return"getSource failed with url: "+a+", exception: "+c.toString()}},guessFunctionNameFromLines:function(a,b){for(var c=/function ([^(]*)\(([^)]*)\)/,d=/['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*(function|eval|new Function)/,e="",f=0;f<10;++f){e=b[a-f]+e;if(e!==undefined){var g=d.exec(e);if(g&&g[1])return g[1];else if((g=c.exec(e))&&g[1])return g[1]}}return"(?)"}};
  280 +
  281 +
  282 +
  283 +
  284 +window.onerror = function(message, file, line) {
  285 + setTimeout(function() {
  286 + Hoptoad.notify({
  287 + message : message,
  288 + stack : '()@' + file + ':' + line
  289 + });
  290 + }, 100);
  291 + return true;
  292 +};
  293 +
... ...