<?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>code.random() &#187; javascript</title>
	<atom:link href="http://code.pui.ch/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://code.pui.ch</link>
	<description>Random code-snippets and tips</description>
	<lastBuildDate>Sun, 04 Apr 2010 21:42:18 +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>Send javascript errors by mail</title>
		<link>http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/</link>
		<comments>http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/#comments</comments>
		<pubDate>Sat, 06 Oct 2007 12:33:42 +0000</pubDate>
		<dc:creator>philipp.keller</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/</guid>
		<description><![CDATA[I'm running a Django-powered site for a closed user group and added a bit of JavaScript magic here and there (mainly Prototype and Tooltip).
Now Django sends me a mail whenever a 404 or 500 error occurs. But when one of my users encounters a JavaScript-Error, I'm not informed. I thought anyone in the web has [...]]]></description>
			<content:encoded><![CDATA[<p>I'm running <a href="http://extranet.icoc.ch">a Django-powered site for a closed user group</a> and added a bit of JavaScript magic here and there (mainly <a href="http://www.prototypejs.org/">Prototype</a> and <a href="http://codylindley.com/Javascript/219/finding-a-javascript-tool-tip-script">Tooltip</a>).</p>
<p>Now Django sends me a mail whenever a 404 or 500 error occurs. But when one of my users encounters a JavaScript-Error, I'm not informed. I thought anyone in the web has solved this problem but didn't find anything, so here's my take: Just send any error using Ajax (here: using <a href="http://www.prototypejs.org/learn/introduction-to-ajax">Prototypes Ajax abstraction</a>) to the server</p>
<div class="igBar"><span id="ljavascript-3"><a href="#" onclick="javascript:showPlainTxt('javascript-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">JAVASCRIPT:</span>
<div id="javascript-3">
<div class="javascript">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">onerror</span> = Extranet.<span style="color: #006600;">mailError</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> mailError<span style="color: #66cc66;">&#40;</span>msg, url, line<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> postBody = <span style="color: #3366CC;">'url='</span> + url + <span style="color: #3366CC;">'&amp;line='</span> + line + <span style="color: #3366CC;">'&amp;message='</span> + escape<span style="color: #66cc66;">&#40;</span>msg<span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'&amp;useragent='</span> + escape<span style="color: #66cc66;">&#40;</span>navigator.<span style="color: #006600;">userAgent</span><span style="color: #66cc66;">&#41;</span> + <span style="color: #3366CC;">'&amp;user='</span> + escape<span style="color: #66cc66;">&#40;</span>user_name<span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #003366; font-weight: bold;">var</span> myAjax = <span style="color: #003366; font-weight: bold;">new</span> Ajax.<span style="color: #006600;">Request</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'/api/jserror/'</span>, <span style="color: #66cc66;">&#123;</span>method: <span style="color: #3366CC;">'post'</span>, postBody: postBody<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><code>user_name</code> is a JavaScript variable holding the Django username (so I know whom I can inform when the error is fixed).</p>
<p>On the server side, I just send me mails containing the JavaScript error message, the username and the user agent:</p>
<div class="igBar"><span id="lpython-4"><a href="#" onclick="javascript:showPlainTxt('python-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PYTHON:</span>
<div id="python-4">
<div class="python">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #ff7700;font-weight:bold;">def</span> jserror<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:</div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">mail</span> <span style="color: #ff7700;font-weight:bold;">import</span> mail_admins</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; omit_messages = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'pointerobj is not defined'</span>, <span style="color: #483d8b;">'tipobj is not defined'</span>, <span style="color: #483d8b;">'ns6 is not defined'</span>, <span style="color: #483d8b;">'enabletip is not defined'</span><span style="color: black;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'message'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> omit_messages:</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; message = <span style="color: #483d8b;">""</span><span style="color: #483d8b;">"url: %s (%s)</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">%s</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">user-agent: %s</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">username: %s</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #483d8b;">"</span><span style="color: #483d8b;">""</span> % <span style="color: black;">&#40;</span>request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'url'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'line'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'message'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'useragent'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>, request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'user'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; mail_admins<span style="color: black;">&#40;</span><span style="color: #483d8b;">"javascript error"</span>, urldecode<span style="color: black;">&#40;</span>message<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B; ">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponse<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Yeah, that's all very trivial but I wonder what other solutions exist for this problem...</p>
]]></content:encoded>
			<wfw:commentRss>http://code.pui.ch/2007/10/06/send-javascript-errors-by-mail/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
