YOUR FEEDBACK
IBM Buys Its Way Out of Antitrust Trouble
Plato wrote: L.L.Bean was never actually a customer of PSI. At most, they we...
SOA World Conference
Virtualization Conference
$50 Savings Expire June 24, 2008... – Register Today!

SYS-CON.TV

2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
TOP THREE LINKS YOU MUST CLICK ON


Custom Error Handling Using AJAX
Enhancing the interactive experience

Digg This!

Page 2 of 2   « previous page

After the ShowMessage function is written we have to write the logic to check the value of the first node returned. Since we called an update page we expect the first node to be named either "error" or "success" so we create code that will check to see if this is true:

if(response.childNodes[0].nodeName == 'error')
{
ShowMessage(response.childNodes[0].firstChild.nodeValue, true);
}
else
{
ShowMessage(response.childNodes[0].firstChild.nodeValue, false);
}

Let's look at our example of updating the employee record again. In the first case we got an XML message that looked like:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><data>
<Success>Successfully updated employee record.</Success></data>

In this case our script would call the ShowMessage function with the error Boolean value set to false. The resulting page would look something like Figure 2.

Now let's look at the same page if the server returned an error because the employee's extension is used by another employee. As mentioned above, the XML message returned from the ColdFusion template would look like:

<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?><data>
<error>There was a error updating the employee, extension is in use by Desmond Mason.
Please call the help desk at x555.</error></data>

In this case our script would call the ShowMessage function with the error Boolean value set to true. The resulting page would look something like Figure 3.

Coupling with Client-Side and Template Error Checking
To make the messaging system complete we have to integrate traditional ColdFusion error checking as well as client-side error checking.

Let's look at how to integrate traditional ColdFusion error handling into our messaging system first. We'll start by creating two session variables: one to hold a Boolean value to determine if an error has occurred and a second one that will be a string to hold the error message that we're going to return to the visitor. For this example I'll name them session.error and session.errorMessage. The next step is to wrap all your ColdFusion logic in the <cftry> and <cfcatch> statements. In your <cfcatch> statement you'll set the session.error to true and the session.errorMessage equal to the cfcatch.Message.

try
{
Department = CreateObject("Component","CFDJ.Components.Department");
DepartmentArray = Department.GetAllDepartments(#session.dsn#);
}
catch(Any ex)
{
    session.error = true;
    session.errorMessage = ex.Message;
}

Finally we'll have to add some logic to control the visibility of our message area. If the session.error message is false the display area is created exactly as before. However, if an error is found we'll set the default style of the message area to "error" and display the error message:

<cfif NOT session.error>
    <span id="message" name="message" class="hidden"></span>
<cfelse>
<span id="message" name="message" class="error">#session.errorMessage#</span>
    <cfset session.error = false>
</cfif>

Now let's say that the connection to the database was down when the user first entered the application. The function in Listing 1 is used to get a list of all the departments in the company.

If the database server is down the cfcatch will catch a database error and throw a new error message that will tell the user that the database server is down, it's up to the page that calls this function to trap the error and set the session.error and session.errorMessage variables:

<cfscript>
    try
    {
       Department = CreateObject("Component","CFDJ.Components.Department");
       DepartmentArray = Department.GetAllDepartments(#session.dsn#);
    }
    catch(Any ex)
    {
       session.error = true;
       session.errorMessage = ex.Message;
    }
</cfscript>

In this case the employee update page would look like Figure 4 when the user enters the application.

Next let's look at using JavaScript to do some simple client-side error handling. The first step is to check for invalid data. I'll assume that you've had some experience doing simple JavaScript error checking and skip right to displaying the error. For our example, we're going to have the JavaScript check to make sure that the user has selected the name of a user to edit from the user drop-down. The first step would be to make sure that the selected index of a drop-down is not zero. Typically the next step would be to display some sort of message to the user with the Alert() function. In this case we'll simply call the ShowError() function that we wrote above:

if(departmentSelect.selectedIndex != 0)
{
    //do something
}
else
{
    ShowMessage('Please select an employee to update',true);
}

Conclusion
Adding a messaging system to an AJAX application isn't very complicated, but it can be an extremely useful technique for keeping your end users informed. This application can be viewed online at www.innovacreative.com/cfdj/ajax. The source code can be downloaded from www.innovacrative.com/cfdj/ajax/ajax.zip.


Page 2 of 2   « previous page

About Ryan Anklam
Ryan Anklam is the Chief Information Officer at Innova Creative Media, Inc. His current focus is on using ColdFusion to develop large scale hosted applications. Ryan has been developing ColdFusion applications since 1996. In addition, he is also a Microsoft Certified Professional with demonstrated skills in C# and SQL Server.

AJAXWorld News Desk wrote: AJAX has become an increasingly popular tool to develop RIAs. With AJAX, as with many new technologies, developers often overlook core application issues such as error handling. While many current AJAX frameworks come with ways to handle errors, the built-in error-handling methods might not be quite what you need, and it's possible that you might not even want to adopt a specific AJAX framework at all. So how do you handle errors in AJAX?
read & respond »
SYS-CON Australia News Desk wrote: AJAX has become an increasingly popular tool to develop RIAs. With AJAX, as with many new technologies, developers often overlook core application issues such as error handling. While many current AJAX frameworks come with ways to handle errors, the built-in error-handling methods might not be quite what you need, and it's possible that you might not even want to adopt a specific AJAX framework at all. So how do you handle errors in AJAX?
read & respond »
LATEST AJAXWORLD STORIES
Adobe's Kevin Lynch and Microsoft's Scott Guthrie to Keynote AJAX World RIA Conference & Expo
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted
AJAX World RIA Conference & Expo Attracts Top Faculty
RIAs offer the potential to fundamentally change the user experience and in doing so, yield significant business benefits. The theme of this October's AJAXWorld Conference & Expo 2008 West is 'Beyond AJAX to the RIA Era' and the Call for Papers, which is currently still open, spe
Web 2.0 Journal Case Study: Transcending E-mail as a Platform for Multi-Person Collaboration
E-mail is extremely easy to adopt and use, and lends itself very well to certain types of collaboration. When two people are attempting to collaborate asynchronously, e-mail is usually the best solution. It's certainly far less frustrating than phone tag. But once more people are
Google Web Toolkit: Finally Java Has Been Put into JavaScript!
For many years, Java and web developers alike have had to explain to the unwitting that JavaScript had absolutely nothing to do with Java. It was one of the great marketing gaffes of the mid-90s. They were no more alike than chalk and cheese. But in the last six months, I have ha
Catalyst Resources Uses RIAs and SaaS to Shrink Carbon Footprint
Catalyst Resources released a 'green audit' of their software-as-a-service (SaaS) collaborative workspace project that allows distributed business teams of all sizes to work virtually and seamlessly online. The Rich Internet Application (RIA) enabled Catalyst Resources to reduce
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE