devinrhode2 / sendUncaughtException

Catch exceptions and send them to window.onuncaughtException(e). sendUncaughtException(e) is just a more robust way of calling this function
MIT License
0 stars 0 forks source link

Regarding the instanceof an exception #5

Open devinrhode2 opened 10 years ago

devinrhode2 commented 10 years ago

Most people most of the time probably don't care about the instanceof an exception. Here is some code I'm removing but is still pretty valuable:

    if (!ex.name) {
      ['Error'
      ,'EvalError'
      ,'RangeError'
      ,'ReferenceError'
      ,'SyntaxError'
      ,'TypeError'
      ,'URIError'
      ,'InternalError'
      ,'DOMException'
      ,'nsIXPCException'
      ,'nsIException'
      ,'DOMError'
      ,'FileError'
      // There are also errors with html5 api's, like MediaError, MediaKeyError, ErrorEvent,
      // ApplicationCache errors, and other html5 api errors that aren't "exceptions" really
      ].forEach(function(errorType) {
        if (!window[errorType]) return;
        if (ex instanceof window[errorType]) {
          ex.name = errorType;
        }
      });
    }
devinrhode2 commented 10 years ago

Figured out how to list all error types that are an instanceof Error:

var thing;
var forbiddenKeys = [];
var gp = '';
var p = '';
var allKeys = Object.getOwnPropertyNames(window);
var index = allKeys.length;
var errorTypes = [];
while (index > 0) {
  index--
  p = allKeys[index];
  if (~forbiddenKeys.indexOf(p)) return;
  gp = p;
  thing = window[p];
  try {
    var aThing = new thing('a');
    if (aThing instanceof Error) {
      console.log(p);
    } else if (aThing.message && (aThing.stack || aThing.name)) {
      console.log('not error', p);
    }
  } catch ( _ ) {
    _;
  }
}

Checking in Opera, Chorme, Firefox, and Safari these are:

Error
TypeError
SyntaxError
ReferenceError
URIError
EvalError
RangeError

But FF also has InternalError

devinrhode2 commented 10 years ago

I was also curious about all other globals that have "Error" or "Exception" in their identifier, so here's a snippit for that:

var allKeys = Object.getOwnPropertyNames(window);
var p = '';
for (var i = 0; i < allKeys.length; i++) {
  p = allKeys[i];
  try {
  if (~p.indexOf('Error') || ~p.indexOf('Exception') ) {
    console.log(p);
  }
  } catch (e) { e; }
}

Chrome:

ErrorEvent
MediaError
MediaKeyError
webkitSpeechRecognitionError
FileError
DOMException
AutocompleteErrorEvent

Opera:

ErrorEvent
MediaError
MediaKeyError
FileError
DOMException
AutocompleteErrorEvent 

Firefox:

DOMError
MediaError
DOMException
GetUserMediaErrorCallback
DataErrorEvent
GeoPositionErrorCallback
SpeechRecognitionError
GeoPositionError

Safari:

DOMException
SQLException
ErrorEvent
WebKitMediaKeyError
MediaError
XMLHttpRequestException
RangeException
XPathException
FileError
EventException
SVGException

Only "MediaError" is common amongst these 4 browsers, along the above list of Error types.