Welcome!

ColdFusion Authors: Maureen O'Gara, Hovhannes Avoyan, Yakov Fain, Pat Romanski, Liz McMillan

Related Topics: ColdFusion, AJAX & REA

ColdFusion: Article

AJAX Custom Error Handling: Enhancing the Interactive User Experience

AJAX and Rich Internet Applications

If everything in the update goes okay the ColdFusion template will send the page a success XML message that would look something like this:

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

Now, let's say that the extension is in use. Our ColdFusion template will return a error XML message that would look something 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>

To complete the application we have to have a way to handle the XML sent back to the requesting page.

Creating JavaScript to Handle the Errors
Let's take a look at what we've done so far: we've created our CSS style classes, we've created our display container, we have our query, our template to update the employee record, and created our error-handling logic. The next step is to read the response from the server, parse through the XML, and set the appropriate display condition. We'll start this step by creating a function to show error messages. This function will need two parameters: the message text and an indication of whether or not this is an error message.

We'll start writing this function by setting a variable with a value that will represent the name of the message area - this, again, is our display container. Now that we have our display container we have to set the class of the display container conditionally based on the error parameter. Finally, we can set the innerHTML property of message node equal to the message's value:

function ShowMessage(message, isError)
{
    messageArea = document.getElementsByName('message')[0];

    if(isError)
    {
      messageArea.className = 'error';
    }
    else
    {
      messageArea.className = 'message';
    }

    messageArea.style.display = 'block';
    messageArea.innerHTML = message;
}
if(response.childNodes[0].nodeName == 'error')
{
ShowMessage(response.childNodes[0].firstChild.nodeValue,true);
}

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>

More Stories By 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.

Comments (6) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Kim 03/20/06 03:07:22 PM EST

On page two, it reads Creating JavaScript to Handle the Errors
Let's take a look at what we've done so far: we've created our CSS style classes, we've created our display container, we have our query, our template to update the employee record, and created our error-handling logic.

I don't see where we created the query or the template to update the employee record so far in the article.

Dave 03/20/06 12:34:38 PM EST

Unfortunately the links to the demo and zip file are not working for this article. Is there another place one can test this out and download the source?

Thanks,
Dave

SYS-CON India News Desk 03/19/06 10:31:11 AM EST

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?

SYS-CON Italy News Desk 03/19/06 09:50:32 AM EST

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?

Dev 03/19/06 08:30:53 AM EST

The URL for download the source cannot be reached. Is there any other way to get the source ?

Dev 03/19/06 08:30:43 AM EST

The URL for download the source cannot be reached. Is there any other way to get the source ?