catch-all-errors
Version:
Catch all JavaScript errors and post them to your server
1 lines • 2.99 kB
JavaScript
(function(){function b(d,e,g){function a(j,i){if(!e[j]){if(!d[j]){var f="function"==typeof require&&require;if(!i&&f)return f(j,!0);if(h)return h(j,!0);var c=new Error("Cannot find module '"+j+"'");throw c.code="MODULE_NOT_FOUND",c}var k=e[j]={exports:{}};d[j][0].call(k.exports,function(b){var c=d[j][1][b];return a(c||b)},k,k.exports,b,d,e,g)}return e[j].exports}for(var h="function"==typeof require&&require,c=0;c<g.length;c++)a(g[c]);return a}return b})()({1:[function(){function a(b,c,d){var f=document.createElement(b);for(var e in c)f.setAttribute(e,c[e]);return f.innerHTML=d||"",f}var b=window.onerror,c=document.currentScript,d={},f={};d.post_url=c.getAttribute("data-post-url")||null,d.callback=c.getAttribute("data-callback")||null,d.email_to=c.getAttribute("data-email-to")||null,f.continuous=null!=c.getAttribute("data-continuous"),f.prevent_default=null!=c.getAttribute("data-prevent-default"),window.onerror=function(){var a=window.onerror;window.onerror=null;var b=g.apply(null,arguments);return console.log("extracted error:",b),d.callback&&window[d.callback]&&window[d.callback](b),d.post_url&&h(d.post_url,b),d.email_to&&i(d.email_to,b),f.continuous&&(window.onerror=a),f.prevent_default};var g=function(a,b,c,d,f){console.log("onerror",arguments);var g={message:a,location:document.location.href,url:b,line:c,column:d,useragent:navigator.userAgent};return f&&(f.stack&&(g.stack=f.stack),f.fileName&&(g.file=f.fileName),f.message&&(g.message=f.message),f.description&&(g.message=f.description)),g},h=function(a,b){console.log("_action_post_url",a,b);var c=new XMLHttpRequest,d=[];for(var e in b)d.push(e+"="+encodeURIComponent(b[e]));c.open("POST",a,!0),c.setRequestHeader("Content-type","application/x-www-form-urlencoded"),c.onreadystatechange=function(){4==c.readyState&&200==c.status?console.log("catch-all-errors successful post:",c.responseText):4==c.readyState&&console.log("catch-all-errors:",c.status,c.responseText)},c.send(d.join("&"))},i=function(b,c){function d(){document.body.removeChild(e)}console.log("_action_email_to",b,c);var e=a("div",{class:"catch-all-errors-modal"}),f=a("div",{},"Oops an error occured. Email this to the developers?<br/>"),g=a("a",{href:"#"},"Cancel"),h=a("a",{href:"mailto:"+b+"?subject="+encodeURIComponent("JavaScript error from "+c.url)+"&body="+encodeURIComponent("Hi, just letting you know I saw the following error:\n\n"+JSON.stringify(c,null,2)+"\n")},"Send");document.body.appendChild(e),e.appendChild(f),f.appendChild(g),f.appendChild(h),e.style="position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; overflow: none; background-color: rgba(0,0,0,0.4); margin: auto auto;",f.style="margin: 1em auto; width: 200px; text-align: center; padding: 2em; background-color: white; border-radius: 3px;",g.style=h.style="margin: 2em 1em 0em 1em; display: inline-block;",g.onclick=function(a){a.preventDefault(),d()},h.onclick=function(){setTimeout(function(){document.body.removeChild(e)},1)}}},{}]},{},[1]);