Revision: 12796
Updated Code
at April 19, 2009 14:56 by Sephr
Updated Code
function ErrorConstructor(constructorName) { var errorConstructor = function(message, fileName, lineNumber) { // don't directly name this function, .name is used by Error.prototype.toString if (this == window) return new arguments.callee(message, fileName, lineNumber); this.name = errorConstructor.name; this.message = message||""; this.fileName = fileName||location.href; if (!isNaN(+lineNumber)) this.lineNumber = +lineNumber; else this.lineNumber = 1; } errorConstructor.name = constructorName||Error.prototype.name; errorConstructor.prototype.toString = Error.prototype.toString; return errorConstructor; } // This part is optional and simulates an Error object for Object.prototype.toString (function(){ var realObjectToString = Object.prototype.toString, realFunctionToString = Function.prototype.toString; Object.prototype.toString = function() { if (this.constructor.prototype.toString === Error.prototype.toString) return "[object Error]"; else return realObjectToString.call(this); }; Function.prototype.toString = function() { if (this === Function.prototype.toString || this === Object.prototype.toString) return realObjectToString.toString(); else return realFunctionToString.call(this); } })()
Revision: 12795
Updated Code
at March 29, 2009 01:35 by Sephr
Updated Code
function ErrorConstructor(constructorName) { var errorConstructor = function(message, fileName, lineNumber) { // don't directly name this function, .name is used by Error.prototype.toString if (this == window) return new arguments.callee(message, fileName, lineNumber); this.name = errorConstructor.name; this.message = message||""; this.fileName = fileName||location.href; if (!isNaN(+lineNumber)) this.lineNumber = +lineNumber; else this.lineNumber = 1; } errorConstructor.name = constructorName||Error.prototype.name; errorConstructor.prototype.toString = Error.prototype.toString; return errorConstructor; }
Revision: 12794
Updated Code
at March 29, 2009 00:52 by Sephr
Updated Code
function ErrorConstructor(constructorName) { var errorConstructor = function(message, fileName, lineNumber) { if (this == window) return new arguments.callee(message, fileName, lineNumber); this.name = errorConstructor.name; this.message = message||""; this.fileName = fileName||location.href; if (!isNaN(+lineNumber)) this.lineNumber = +lineNumber; else this.lineNumber = 1; } errorConstructor.name = constructorName||Error.prototype.name; errorConstructor.prototype.toString = Error.prototype.toString; return errorConstructor; }
Revision: 12793
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at March 29, 2009 00:27 by Sephr
Initial Code
function ErrorConstructor(constructorName) { var errorConstructor = function(message, fileName, lineNumber) { if (this == window) return new arguments.callee(message, fileName, lineNumber); this.name = errorConstructor.name; if (typeof message == "undefined") message = ""; this.message = message; this.fileName = fileName||location.href; if (!isNaN(lineNumber)) this.lineNumber = lineNumber; else this.lineNumber = 1; } errorConstructor.name = constructorName||Error.prototype.name; errorConstructor.prototype.toString = Error.prototype.toString; return errorConstructor; }
Initial URL
http://eligrey.com/2009/03/29/custom-error-constructors/
Initial Description
ErrorConstructor produces error constructors that behave the same way as the seven native error constructors. Usage: `ErrorConstructor([constructorName])` *If no constructorName is specified, the default of `Error.prototype.name` is used* Usage for generated error constructor: `errorConstructor([message[, location[, lineNumber]])` Examples: var SecurityError = ErrorConstructor("Security Error"), MarkupError = ErrorConstructor("(X)HTML Markup Error"); //these will both throw a SecurityError starting with "Security Error on line 83:" var xss_error = "Possible XSS Vector\n\ JSON XHR response parsed with eval()\n\ Recommended fix: Parse JSON with JSON.parse"; throw new SecurityError(xss_error, "/js/searchResultsJSONloader.js", 83); throw SecurityError(xss_error, "/js/searchResultsJSONloader.js", 83); //these will both throw the following MarkupError: //"(X)HTML Markup Error on line 1: Invalid DOCTYPE" throw new MarkupError("Invalid DOCTYPE"); throw MarkupError("Invalid DOCTYPE");
Initial Title
ErrorConstructor
Initial Tags
error
Initial Language
JavaScript