Last active
November 4, 2022 11:57
-
-
Save getify/7f3be36672397550d9da43750dc69350 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dispatchErrorEvent = (function IIFE(){ | |
var createErrorEvent; | |
try { | |
try { | |
new ErrorEvent("error",{}); | |
createErrorEvent = function createErrorEvent(err) { | |
return new ErrorEvent("error",{ | |
error: err, | |
message: err.message || "general error event" | |
}); | |
}; | |
return dispatchError; | |
} | |
catch(err){} | |
var x = document.createEvent("Event"); | |
x.initEvent("error",false,false); | |
createErrorEvent = function createErrorEvent(err) { | |
var e = document.createEvent("Event"); | |
e.initEvent("error",false,false); | |
e.message = err.message || "general error event"; | |
return e; | |
}; | |
return dispatchError; | |
} | |
catch(err) {} | |
return Function.prototype; // no-op | |
function dispatchError(err) { | |
window.dispatchEvent(createErrorEvent(err)); | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function logMsgs() { | |
var args = [].slice.call(arguments); | |
var elem = document.getElementById("messages"); | |
elem.innerHTML += "<p>" + args.join(" ; ") + "</p>"; | |
} | |
window.onerror = function(err){ | |
logMsgs("onerror",err.message || err); | |
return true; | |
}; | |
window.addEventListener("error",function(evt){ | |
logMsgs("addEL-1",evt.type,evt.message); | |
}); | |
window.addEventListener("error",function(evt){ | |
logMsgs("addEL-2",evt.type,evt.message); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo() { | |
throw new Error("...from foo"); | |
} | |
function bar() { | |
doesntexist1(); | |
} | |
document.getElementById("clickme").addEventListener("click",bar,false); | |
setTimeout(function(){ | |
throw new Error("...from settimeout"); | |
},1000); | |
dispatchErrorEvent(new Error("...from dispatched event")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo(); | |
// note: shouldn't get here | |
doesntexist2(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<meta charset="utf-8"> | |
<title>Creating, dispatching, and catching error events</title> | |
<body> | |
<h1>Creating, dispatching, and catching error events</h1> | |
<button id="clickme">click me</button> | |
<div id="messages"></div> | |
<script src="dispatch-error-event.js"></script> | |
<script src="test-error-1.js"></script> | |
<script src="test-error-2.js"></script> | |
<script src="test-error-3.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment