Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.60 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to roll up a JavaScript error?
  2. try {
  3.   setTimeout(function() {
  4.     console.log("Throwing Error...");
  5.     throw({message:"Ouch!"});
  6.   }, 500);
  7. } catch(e) {
  8.   console.log(e.message);
  9. }
  10.        
  11. Uncaught #<Object>
  12.   (anonymous function)
  13.        
  14. (function() {
  15. try {
  16.   var scriptVersion = "1.0.0.1"
  17.   window.onload = function() {
  18.     var script = document.createElement("script");
  19.     script.type = "text/javascript";
  20.     script.src = "//content.com/pkg/" + scriptVersion + "/require-jquery.js";
  21.     script.async = false;
  22.     script.done = false;
  23.     // OnReadyStateChange for older IE browsers
  24.     script.onload = script.onreadystatechange = function() {
  25.       if(!(this.done) && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
  26.         this.done = true;
  27.         require.config({
  28.           baseUrl: "//content.com/pkg/" + scriptVersion
  29.         });
  30.         require(["bob"]);
  31.       }
  32.     }
  33.     document.getElementsByTagName("head")[0].appendChild(script);
  34.   }
  35. } catch(e) {
  36.   console.log(e);
  37. }
  38. })();
  39.        
  40. setTimeout(function() {
  41.     try {
  42.         console.log("Throwing Error...");
  43.         throw({message:"Ouch!"});
  44.     } catch(e) {
  45.         console.log(e.message);
  46.     }
  47.   }, 500);
  48.        
  49. /**
  50.  * Any errors that require explicitly generates will be passed to this
  51.  * function. Intercept/override it if you want custom error handling.
  52.  * @param {Error} err the error object.
  53.  */
  54. req.onError = function (err) {
  55.     throw err;
  56. };
  57.        
  58. setTimeout(function() {
  59.      try {
  60.          console.log("Throwing Error...");
  61.          throw({message:"Ouch!"});
  62.      } catch(e) {
  63.        console.log(e.message);
  64.      }
  65.       }, 500);