Skip to content

Instantly share code, notes, and snippets.

@markjlorenz
Created December 17, 2012 21:36
Show Gist options
  • Save markjlorenz/4322508 to your computer and use it in GitHub Desktop.
Save markjlorenz/4322508 to your computer and use it in GitHub Desktop.
Report client side JS errors back to the application
// If you pash an object of the same format as `testCases` it will be merged into test cases
// requires jQuery
var objectSupport = function(additionalObjects){
additionalObjects = typeof additionalObjects !== 'undefined' ? additionalObjects : {};
if(!jQuery){throw new Error("clientJSError requires jQuery");}
//takes an object path, returns true of it's available. For Example:
// >objectSupported("Array.prototype.forEach")
// >false //false in IE7
//
// >objectSupported("JSON")
// >false //false in IE7
var objectSupported = function(pathString){var prev = window; var a = pathString.split(/\./); for (var i in a){if(!prev){break;};prev = prev[a[i]]}; return !!prev};
//http://pointedears.de/scripts/test/es-matrix/
var testCases = {
"Array":null,
"Array.isArray":null,
"Array.prototype.every":null,
"Array.prototype.filter":null,
"Array.prototype.indexOf":null,
"Array.prototype.join":null,
"Array.prototype.lastIndexOf":null,
"Array.prototype.push":null,
"Array.prototype.reduce":null,
"Array.prototype.reduceRight":null,
"Array.prototype.slice":null,
"Array.prototype.some":null,
"Array.prototype.splice":null,
"Array.prototype.toSource":null,
"Array.prototype.toString":null,
"Array.prototype.concat":null,
"Array.prototype.forEach":null,
"Array.prototype.pop":null,
"Array.prototype.map":null,
"Array.prototype.reverse":null,
"Array.prototype.shift":null,
"Array.prototype.sort":null,
"Array.prototype.unshift":null,
"JSON":null,
"JSON.parse":null,
"JSON.stringify":null,
"console":null,
"console.log":null,
"Date":null,
"Date.now":null,
"Date.prototype.toISOString":null,
"Date.prototype.toJSON":null,
"Object":null,
"Object.keys":null,
"Object.defineProperty":null,
"Object.defineProperties":null,
"Object.getOwnPropertyNames":null,
"Object.getPrototypeOf":null
};
$.extend(testCases, additionalObjects);
for (var i in testCases){
testCases[i] = objectSupported(i);
}
return testCases;
}
window.onerror = function(message, url, line){
$.post('/app/client-error',
{message:message,
url:url,
line:line,
browser:$.browser,
objectsSupported:objectSupport()} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment