Commit a821be3f70be566e7d14717e73b9e04847566f87
Exists in
master
and in
1 other branch
Merge branch 'js_notifier' of https://github.com/crossroads/errbit into pull-request-50
Showing
3 changed files
with
307 additions
and
9 deletions
Show diff stats
app/views/apps/_configuration_instructions.html.haml
| @@ -2,8 +2,8 @@ | @@ -2,8 +2,8 @@ | ||
| 2 | %code | 2 | %code |
| 3 | :preserve | 3 | :preserve |
| 4 | 4 | ||
| 5 | - # Require the hoptoad_notifier gem in you App. | ||
| 6 | - # -------------------------------------------- | 5 | + # Require the hoptoad_notifier gem in your App. |
| 6 | + # --------------------------------------------- | ||
| 7 | # | 7 | # |
| 8 | # Rails 3 - In your Gemfile | 8 | # Rails 3 - In your Gemfile |
| 9 | # gem 'hoptoad_notifier' | 9 | # gem 'hoptoad_notifier' |
| @@ -13,12 +13,15 @@ | @@ -13,12 +13,15 @@ | ||
| 13 | # | 13 | # |
| 14 | # Then add the following to config/initializers/errbit.rb | 14 | # Then add the following to config/initializers/errbit.rb |
| 15 | # ------------------------------------------------------- | 15 | # ------------------------------------------------------- |
| 16 | + | ||
| 16 | HoptoadNotifier.configure do |config| | 17 | HoptoadNotifier.configure do |config| |
| 17 | - config.api_key = '#{app.api_key}' | ||
| 18 | - config.host = '#{request.host}' | ||
| 19 | - config.port = #{request.port} | ||
| 20 | - config.secure = config.port == 443 | 18 | + config.api_key = '#{app.api_key}' |
| 19 | + config.host = '#{request.host}' | ||
| 20 | + config.port = #{request.port} | ||
| 21 | + config.secure = config.port == 443 | ||
| 22 | + config.js_notifier = true | ||
| 21 | end | 23 | end |
| 24 | + | ||
| 22 | # | 25 | # |
| 23 | # Testing | 26 | # Testing |
| 24 | # ------- | 27 | # ------- |
lib/hoptoad.rb
| @@ -3,14 +3,14 @@ module Hoptoad | @@ -3,14 +3,14 @@ module Hoptoad | ||
| 3 | require 'digest/md5' | 3 | require 'digest/md5' |
| 4 | 4 | ||
| 5 | class ApiVersionError < StandardError | 5 | class ApiVersionError < StandardError |
| 6 | - def initialize | ||
| 7 | - super "Wrong API Version: Expecting v2.0" | 6 | + def initialize(version) |
| 7 | + super "Wrong API Version: Expecting v2.0, got version: #{version}" | ||
| 8 | end | 8 | end |
| 9 | end | 9 | end |
| 10 | 10 | ||
| 11 | def self.parse_xml(xml) | 11 | def self.parse_xml(xml) |
| 12 | parsed = ActiveSupport::XmlMini.backend.parse(xml)['notice'] | 12 | parsed = ActiveSupport::XmlMini.backend.parse(xml)['notice'] |
| 13 | - raise ApiVersionError unless parsed && parsed['version'] == '2.0' | 13 | + raise ApiVersionError(parsed['version']) unless parsed && parsed['version'].to_s == '2.0' |
| 14 | rekeyed = rekey(parsed) | 14 | rekeyed = rekey(parsed) |
| 15 | rekeyed['fingerprint'] = Digest::MD5.hexdigest(rekeyed['error']['backtrace'].to_s) | 15 | rekeyed['fingerprint'] = Digest::MD5.hexdigest(rekeyed['error']['backtrace'].to_s) |
| 16 | rekeyed | 16 | rekeyed |
| @@ -43,3 +43,4 @@ module Hoptoad | @@ -43,3 +43,4 @@ module Hoptoad | ||
| 43 | end | 43 | end |
| 44 | end | 44 | end |
| 45 | end | 45 | end |
| 46 | + |
| @@ -0,0 +1,294 @@ | @@ -0,0 +1,294 @@ | ||
| 1 | +var Errbit = { | ||
| 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>errbit_notifier_js</name>\ | ||
| 8 | + <version>2.0</version>\ | ||
| 9 | + <url>http://NOTIFIER_HOST</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(Errbit.generateXML(error)); | ||
| 32 | + var host = Errbit.host || 'github.com/jdpace/errbit'; | ||
| 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 | + Errbit.NOTICE_XML = Errbit.NOTICE_XML.replace(matcher, | ||
| 48 | + '<environment-name>' + | ||
| 49 | + value + | ||
| 50 | + '</environment-name>') | ||
| 51 | + }, | ||
| 52 | + | ||
| 53 | + setHost: function(value) { | ||
| 54 | + Errbit.host = value; | ||
| 55 | + }, | ||
| 56 | + | ||
| 57 | + setKey: function(value) { | ||
| 58 | + var matcher = /<api-key>.*<\/api-key>/; | ||
| 59 | + | ||
| 60 | + Errbit.NOTICE_XML = Errbit.NOTICE_XML.replace(matcher, | ||
| 61 | + '<api-key>' + | ||
| 62 | + value + | ||
| 63 | + '</api-key>'); | ||
| 64 | + }, | ||
| 65 | + | ||
| 66 | + setErrorDefaults: function(value) { | ||
| 67 | + Errbit.errorDefaults = value; | ||
| 68 | + }, | ||
| 69 | + | ||
| 70 | + generateXML: function(errorWithoutDefaults) { | ||
| 71 | + var error = Errbit.mergeDefault(Errbit.errorDefaults, errorWithoutDefaults); | ||
| 72 | + | ||
| 73 | + var xml = Errbit.NOTICE_XML; | ||
| 74 | + var url = Errbit.escapeText(error.url || ''); | ||
| 75 | + var component = Errbit.escapeText(error.component || ''); | ||
| 76 | + var action = Errbit.escapeText(error.action || ''); | ||
| 77 | + var type = Errbit.escapeText(error.type || 'Error'); | ||
| 78 | + var message = Errbit.escapeText(error.message || 'Unknown error.'); | ||
| 79 | + var backtrace = Errbit.generateBacktrace(error); | ||
| 80 | + | ||
| 81 | + | ||
| 82 | + if (Errbit.trim(url) == '' && Errbit.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 += Errbit.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 += Errbit.generateVariables(error[type]); | ||
| 101 | + data += '</' + type + '>'; | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + xml = xml.replace('</request>', data + '</request>') | ||
| 106 | + .replace('NOTIFIER_HOST', host) | ||
| 107 | + .replace('REQUEST_URL', url) | ||
| 108 | + .replace('REQUEST_ACTION', action) | ||
| 109 | + .replace('REQUEST_COMPONENT', component); | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + return xml.replace('PROJECT_ROOT', Errbit.ROOT) | ||
| 113 | + .replace('EXCEPTION_CLASS', type) | ||
| 114 | + .replace('EXCEPTION_MESSAGE', message) | ||
| 115 | + .replace('BACKTRACE_LINES', backtrace.join('')); | ||
| 116 | + }, | ||
| 117 | + | ||
| 118 | + generateBacktrace: function(error) { | ||
| 119 | + error = error || {}; | ||
| 120 | + | ||
| 121 | + if (typeof error.stack != 'string') { | ||
| 122 | + try { | ||
| 123 | + (0)(); | ||
| 124 | + } catch(e) { | ||
| 125 | + error.stack = e.stack; | ||
| 126 | + } | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + var backtrace = []; | ||
| 130 | + var stacktrace = Errbit.getStackTrace(error); | ||
| 131 | + | ||
| 132 | + for (var i = 0, l = stacktrace.length; i < l; i++) { | ||
| 133 | + var line = stacktrace[i]; | ||
| 134 | + var matches = line.match(Errbit.BACKTRACE_MATCHER); | ||
| 135 | + | ||
| 136 | + if (matches && Errbit.validBacktraceLine(line)) { | ||
| 137 | + var file = matches[2].replace(Errbit.ROOT, '[PROJECT_ROOT]'); | ||
| 138 | + | ||
| 139 | + if (i == 0) { | ||
| 140 | + if (matches[2].match(document.location.href)) { | ||
| 141 | + backtrace.push('<line method="" file="internal: " number=""/>'); | ||
| 142 | + } | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + backtrace.push('<line method="' + Errbit.escapeText(matches[1]) + | ||
| 146 | + '" file="' + Errbit.escapeText(file) + | ||
| 147 | + '" number="' + matches[3] + '" />'); | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + return backtrace; | ||
| 152 | + }, | ||
| 153 | + | ||
| 154 | + getStackTrace: function(error) { | ||
| 155 | + var stacktrace = printStackTrace({ e : error, guess : false }); | ||
| 156 | + | ||
| 157 | + for (var i = 0, l = stacktrace.length; i < l; i++) { | ||
| 158 | + if (stacktrace[i].match(/\:\d+$/)) { | ||
| 159 | + continue; | ||
| 160 | + } | ||
| 161 | + | ||
| 162 | + if (stacktrace[i].indexOf('@') == -1) { | ||
| 163 | + stacktrace[i] += '@unsupported.js'; | ||
| 164 | + } | ||
| 165 | + | ||
| 166 | + stacktrace[i] += ':0'; | ||
| 167 | + } | ||
| 168 | + | ||
| 169 | + return stacktrace; | ||
| 170 | + }, | ||
| 171 | + | ||
| 172 | + validBacktraceLine: function(line) { | ||
| 173 | + for (var i = 0; i < Errbit.backtrace_filters.length; i++) { | ||
| 174 | + if (line.match(Errbit.backtrace_filters[i])) { | ||
| 175 | + return false; | ||
| 176 | + } | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + return true; | ||
| 180 | + }, | ||
| 181 | + | ||
| 182 | + generateVariables: function(parameters) { | ||
| 183 | + var key; | ||
| 184 | + var result = ''; | ||
| 185 | + | ||
| 186 | + for (key in parameters) { | ||
| 187 | + result += '<var key="' + Errbit.escapeText(key) + '">' + | ||
| 188 | + Errbit.escapeText(parameters[key]) + | ||
| 189 | + '</var>'; | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + return result; | ||
| 193 | + }, | ||
| 194 | + | ||
| 195 | + escapeText: function(text) { | ||
| 196 | + return text.replace(/&/g, '&') | ||
| 197 | + .replace(/</g, '<') | ||
| 198 | + .replace(/>/g, '>') | ||
| 199 | + .replace(/'/g, ''') | ||
| 200 | + .replace(/"/g, '"'); | ||
| 201 | + }, | ||
| 202 | + | ||
| 203 | + trim: function(text) { | ||
| 204 | + return text.toString().replace(/^\s+/, '').replace(/\s+$/, ''); | ||
| 205 | + }, | ||
| 206 | + | ||
| 207 | + mergeDefault: function(defaults, hash) { | ||
| 208 | + var cloned = {}; | ||
| 209 | + var key; | ||
| 210 | + | ||
| 211 | + for (key in hash) { | ||
| 212 | + cloned[key] = hash[key]; | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + for (key in defaults) { | ||
| 216 | + if (!cloned.hasOwnProperty(key)) { | ||
| 217 | + cloned[key] = defaults[key]; | ||
| 218 | + } | ||
| 219 | + } | ||
| 220 | + | ||
| 221 | + return cloned; | ||
| 222 | + } | ||
| 223 | +}; | ||
| 224 | + | ||
| 225 | + | ||
| 226 | + | ||
| 227 | + | ||
| 228 | +// Domain Public by Eric Wendelin http://eriwen.com/ (2008) | ||
| 229 | +// Luke Smith http://lucassmith.name/ (2008) | ||
| 230 | +// Loic Dachary <loic@dachary.org> (2008) | ||
| 231 | +// Johan Euphrosine <proppy@aminche.com> (2008) | ||
| 232 | +// Øyvind Sean Kinsey http://kinsey.no/blog (2010) | ||
| 233 | +// | ||
| 234 | +// Information and discussions | ||
| 235 | +// http://jspoker.pokersource.info/skin/test-printstacktrace.html | ||
| 236 | +// http://eriwen.com/javascript/js-stack-trace/ | ||
| 237 | +// http://eriwen.com/javascript/stacktrace-update/ | ||
| 238 | +// http://pastie.org/253058 | ||
| 239 | +// http://browsershots.org/http://jspoker.pokersource.info/skin/test-printstacktrace.html | ||
| 240 | +// | ||
| 241 | +// | ||
| 242 | +// guessFunctionNameFromLines comes from firebug | ||
| 243 | +// | ||
| 244 | +// Software License Agreement (BSD License) | ||
| 245 | +// | ||
| 246 | +// Copyright (c) 2007, Parakey Inc. | ||
| 247 | +// All rights reserved. | ||
| 248 | +// | ||
| 249 | +// Redistribution and use of this software in source and binary forms, with or without modification, | ||
| 250 | +// are permitted provided that the following conditions are met: | ||
| 251 | +// | ||
| 252 | +// * Redistributions of source code must retain the above | ||
| 253 | +// copyright notice, this list of conditions and the | ||
| 254 | +// following disclaimer. | ||
| 255 | +// | ||
| 256 | +// * Redistributions in binary form must reproduce the above | ||
| 257 | +// copyright notice, this list of conditions and the | ||
| 258 | +// following disclaimer in the documentation and/or other | ||
| 259 | +// materials provided with the distribution. | ||
| 260 | +// | ||
| 261 | +// * Neither the name of Parakey Inc. nor the names of its | ||
| 262 | +// contributors may be used to endorse or promote products | ||
| 263 | +// derived from this software without specific prior | ||
| 264 | +// written permission of Parakey Inc. | ||
| 265 | +// | ||
| 266 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | ||
| 267 | +// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||
| 268 | +// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| 269 | +// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 270 | +// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| 271 | +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
| 272 | +// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
| 273 | +// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 274 | +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(){}; | ||
| 275 | +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/, | ||
| 276 | +"").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: | ||
| 277 | +"{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= | ||
| 278 | +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")}, | ||
| 279 | +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}, | ||
| 280 | +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"(?)"}}; | ||
| 281 | + | ||
| 282 | + | ||
| 283 | + | ||
| 284 | + | ||
| 285 | +window.onerror = function(message, file, line) { | ||
| 286 | + setTimeout(function() { | ||
| 287 | + Errbit.notify({ | ||
| 288 | + message : message, | ||
| 289 | + stack : '()@' + file + ':' + line | ||
| 290 | + }); | ||
| 291 | + }, 100); | ||
| 292 | + return true; | ||
| 293 | +}; | ||
| 294 | + |