DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Email A JavaScript Error Using ColdFusion

12.15.2006
| 4462 views |
  • submit to reddit
        This script emails an administrator when an error occurs, and could easily be adapted to log the error instead

/*
wwJsError (requires prototype.js)
		send:	sends javascript error output email via coldfusion
		--------------------
			path:		relative path from the calling page to the coldfusion
					email handler for this application (usually processJSerror.cfm)
			funcname:	name of the JavaScript function that threw an error
			name:		JavaScript error name (usually e.name in a catch block)
			message:	JavaScript error message (usually e.message in a catch block)
*/
var wwJsError = {
	send: function(path,funcname,name,message) {
		try {

			var url = path;

			var pars = 'message=' + escape(message) + '&name=' +
					escape(name) + '&funcname=' + escape(funcname);
			
			var httpMail = new Ajax.Request (
				url, 
				{
					method: 'post', 
					parameters: pars, 
					onComplete: Prototype.emptyFunction
				}
			);
	
		} catch (e) {
			//if sending the email fails, just die quietly w/ no browser error
			Prototype.emptyFunction();
		}
	}
}; //end wwJsError
Sample server-side script in ColdFusion
<cfmail to="#application.administratoremail#" from="#application.administratoremail#"
		subject="#application.apptitle# Error" type="HTML">
	<strong>JavaScript Error:</strong> #form.funcname# threw the following error:<br />
	<br />
	<strong>Error name:</strong> #form.name#<br />
	<strong>Error:</strong> #form.message#
</cfmail>
Generate the error
function makeError() {
	try {
		alert(anError);
	} catch(e) {
		wwJsError.send('cf/processJSerror.cfm','makeError()',e.name,e.message);
	}
}
Result by email

<b>JavaScript Error:</b> makeError threw the following error:

<b>Error name:</b> TypeError
<b>Error:</b> 'anError' is undefined