Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

JavaScript self-made Error object comparison

By: a guest on Mar 9th, 2011  |  syntax: JavaScript  |  size: 3.41 KB  |  views: 753  |  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. <html><head><title> JavaScript self-made Error object comparison </title></head>
  2. <body><script type="text/javascript">
  3.  
  4. /*** Error classes ****************************************/
  5.  
  6. /* Implementation 1: The very simple one */
  7. function MyError1(message) {
  8.     this.name = 'MyError1';
  9.     this.message = message;
  10.     this.stack = (new Error()).stack;
  11. }
  12. MyError1.prototype = new Error;  // <-- remove this line if you do not
  13.                                  //     want MyError1 to inherit Error
  14.  
  15. /* Implementation 2:
  16. http://stackoverflow.com/questions/1382107/whats-a-good-way-to-extend-error-in-javascript/3535758#3535758 */
  17. function MyError2(message, fileName, lineNumber) {
  18.     var err = new Error();
  19.     if (err.stack) {
  20.         // remove one stack level:
  21.         if (typeof(Components) != 'undefined') {
  22.             // Mozilla:
  23.             this.stack = err.stack.substring(err.stack.indexOf('\n')+1);
  24.         }
  25.         else if (typeof(chrome) != 'undefined' || typeof(process) != 'undefined') {
  26.             // Google Chrome/Node.js:
  27.             this.stack = err.stack.replace(/\n[^\n]*/,'');
  28.         }
  29.         else {
  30.             this.stack = err.stack;
  31.         }
  32.     }
  33.     this.message    = message    === undefined ? err.message    : message;
  34.     this.fileName   = fileName   === undefined ? err.fileName   : fileName;
  35.     this.lineNumber = lineNumber === undefined ? err.lineNumber : lineNumber;
  36. }
  37.  
  38. /* Implementation 3:
  39. http://blog.getify.com/2010/02/howto-custom-error-types-in-javascript/ */
  40. var MyError3 = (function() {
  41.    function F(){}
  42.    function CustomError() {
  43.       var _this = (this===window) ? new F() : this,
  44.           tmp = Error.prototype.constructor.apply(_this,arguments)
  45.       ;
  46.       for (var i in tmp) {
  47.          if (tmp.hasOwnProperty(i)) _this[i] = tmp[i];
  48.       }
  49.       return _this;
  50.    }
  51.    function SubClass(){}
  52.    SubClass.prototype = Error.prototype;
  53.    F.prototype = CustomError.prototype = new SubClass();
  54.    CustomError.prototype.constructor = CustomError;
  55.    return CustomError;
  56. })();
  57.  
  58. /*** Run a set of tests ***********************************/
  59.  
  60. function test(name, errorObject) {
  61.     document.write('<h3>'+ name +'</h3>');
  62.     document.write('<p>instance of Error: ')
  63.     document.write(errorObject instanceof Error);
  64.     document.write('<br>instance of '+ name +': ');
  65.     document.write(eval('errorObject instanceof '+ name));
  66.     document.write('</p>')
  67.     try {
  68.         for (var key in errorObject) {
  69.             document.write('<dl><dt>'+ key +'</dt><dd>');
  70.             if (key == 'lineNumber') {
  71.                 document.write(errorObject.lineNumber);
  72.                 document.write(errorObject.lineNumber === test.lineNum
  73.                                ? ' === ' : ' !== ');
  74.                 document.write(test.lineNum);
  75.             } else {
  76.                 document.write(errorObject[key]);
  77.             }
  78.             document.write('</dd></dl>');
  79.         }
  80.     } catch (e) {
  81.         document.write('</dd></dl>');
  82.         document.write('<p>failed: '+ e +'</p>');
  83.     }
  84.     ++test.lineNum;
  85. }
  86. test.lineNum = (new Error()).lineNumber + 1;
  87. try { throw new Error('message') } catch (e) {     test('Error', e) }
  88. try { throw new MyError1('message1') } catch (e) { test('MyError1', e) }
  89. try { throw new MyError2('message2') } catch (e) { test('MyError2', e) }
  90. try { throw new MyError3('message3') } catch (e) { test('MyError3', e) }
  91.  
  92. </script></body></html>