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

  • 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 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 (
					method: 'post', 
					parameters: pars, 
					onComplete: Prototype.emptyFunction
		} catch (e) {
			//if sending the email fails, just die quietly w/ no browser error
}; //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><br />
	<strong>Error:</strong> #form.message#
Generate the error
function makeError() {
	try {
	} catch(e) {
Result by email

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

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