<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Inside Snipplr.com &#187; errors</title>
	<atom:link href="http://snipplr.com/blog/tag/errors/feed/" rel="self" type="application/rss+xml" />
	<link>http://snipplr.com/blog</link>
	<description>s nipple r &#60;em&#62;dot com!&#60;/em&#62;</description>
	<lastBuildDate>Wed, 23 Nov 2011 17:51:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Custom Error Constructors in JavaScript</title>
		<link>http://snipplr.com/blog/2009/07/29/custom-error-constructors-in-javascript/</link>
		<comments>http://snipplr.com/blog/2009/07/29/custom-error-constructors-in-javascript/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 16:10:44 +0000</pubDate>
		<dc:creator>Elijah Grey</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[constructors]]></category>
		<category><![CDATA[errors]]></category>

		<guid isPermaLink="false">http://snipplr.com/blog/?p=126</guid>
		<description><![CDATA[[Note to person reviewing this: You should make a JavaScript post category.]

Most of the time, the standard six native error constructors and the one generic error constructor are not specific enough for an error. What if you want your library to throw a custom  SecurityError if it detects an XSS vector on a website? [...]]]></description>
			<content:encoded><![CDATA[<p>[Note to person reviewing this: You should make a JavaScript post category.]</p>

<p>Most of the time, the <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error#Error_types">standard six native error constructors</a> and the one generic error constructor are not specific enough for an error. What if you want your library to throw a custom  <code>SecurityError</code> if it detects an XSS vector on a website? I made a function to create such constructors that behave the exact same way the native error constructors, like SyntaxError by using methods like <code>Error.prototype.toString</code> and the standard error object format. This code makes throwing a custom fake error constructor made with <code>ErrorConstructor("SyntaxError")</code> have the same output as a native <code>SyntaxError</code> in a <a href="http://code.eligrey.com/shell/shell.html">JavaScript shell</a>. I&#8217;ve tested the code in Firefox 3/3.5 and Opera 9.6 and it seems to work fine. Comment and say if it works in your browser too.</p>

<pre><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;
}</code></pre>

<p>Usage: <code>ErrorConstructor([constructorName])</code></p>

<p><em>Note: If no constructorName is specified, the default of <code>Error.prototype.name</code> is used</em></p>

<p>Usage for generated error constructor: <code>errorConstructor([message[, location[, lineNumber]])</code></p>

<p>Examples:</p>

<pre><code>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");</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://snipplr.com/blog/2009/07/29/custom-error-constructors-in-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

