Posted By


Sephr on 03/29/09

Tagged


Statistics


Viewed 518 times
Favorited by 1 user(s)

ErrorConstructor


/ Published in: JavaScript
Save to your folder(s)

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");

URL: http://eligrey.com/2009/03/29/custom-error-constructors/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.