Over a million developers have joined DZone.

Email A JavaScript Error Using ColdFusion

·
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


	JavaScript Error: #form.funcname# threw the following error:

Error name: #form.name#
Error: #form.message#
Generate the error function makeError() { try { alert(anError); } catch(e) { wwJsError.send('cf/processJSerror.cfm','makeError()',e.name,e.message); } } Result by email JavaScript Error: makeError threw the following error: Error name: TypeError Error: 'anError' is undefined
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}