The Validation Error Template ColdFusion Help

First, creale the error template for validation errors. This tmplate”Will be called error _validation. efm and should be saved to the sybex folder in your Web server root directory. Because you want to provide useful information to the user about the specific error that has occurred, ColdFusion has provided several built-in validation error variables that you can add to your template. Listing 18.2 shows how to create a basic validation error template. You will notice that no other ColdFusion code is used except a reference to an error variable.

Now that you have added a (FERRORvalidation tag to your App1;cat; on. cfm template and have created a validation error template, you can test the results. If you have not already done so, make sure that the following files from the chapterl? folder on the CD-ROM are in the sybex folder in.your Web server root directory:
• Applicat; on18a. cfm (rename it as Applicat; on. cfm)
• error_validation.cfm
home.cfm
• login. cfm
• login2 . cfm
• OnRequestEnd.cfm
• quiz_results.cfm
• quiz. mdb (If you have not already done so, you need to add this Access database as a data source in the Cold Fusion Administrator.)

You will probably have to log in first (with the usemame kristin and the password mot-lagh). Then you will see a quiz form (see Figure 18.5). Select Score Question! but don’t select a quiz answer and you should see an error template similar·

The Request Error Template

Accounting for request errors is similar to accounting for validation errors. You need to add a CFERRORrequesttag to your Appl; cat; on. cfm template (or any template in which you would like to catch errors). Your Appl; cat ion. cfm template would then look like that in Listing 18.3

Now, you can create the error template for request errors. This template will be called error_request. cfm and should be saved to the sybex folder in your Web server root directory. There are built-in error variables for request errors that provide useful information to the user about the specific error that has occurred how to create a basic request error template. Remember that you cannot use any ColdFusion code other than the error variables provided by ERROR.

To test the results, you need to first create aD.error. To create a simple error, rename any variable being referenced in your home. cfm template to a variable name that has not yet been created. Now go to your browser. and open http://12 7.0.0.1/ sybex/ home.cfm.

If your session has timed out, you may need to log in again (using the usemame kristin and the password motlagh). You should see an error template like that.

The Exception Error Template

With ColdFusion 4.5, The Except; on type was added to the CFERRORtag that allowed the CFERRORtag to be used to trap specific types of errors, or exceptions, and provide a custom error handling template for those errors. Exceptions in the context of the CFCATCHand CFTHROWtags will be discussed later in the section, “Working with Customized Exception Handling.” We will cover the specifics of using CFEROR with  exceptions then.

Logging Error Messages When Using CFERROR

One of the problems with using CFERROR tags, and the resulting error templates, is that you cannot incorporate any Cold Fusion code. As a result, you cannot log any of the error information into a database or automatically send an e-mail to the administrator with all the diagnostic information attached.
There is a way to get around this, however. This section will show you how to logrequest error information to a database by using JavaScript to redirect the browser to a  ColdFusion template that can include ColdFusion code. The procedure to do this is as follows:

1. A request error is encountered, and the user is directed to the request error template, error_request. cfm. .
2. Because the App 1tea ti on. cfm template is run for each page within your application, the CFERROR tags in the App l; cat; on. cfm template are disabled when accessing the error -“request. cfm template. If they are not disabled, an error loop could potentially occur.
3. The error request. cfm template contains a form with hidden fields and a text area field. These fields store the error variables that will be passed on to the next template, error _request. cfm.
4. error_request. cfm has a JavaScript event command when loading the page to automatically submit the form. This means that when error_request. cfm is accessed, the form containing the error variables is automatically submitted to error _request2. cfm. The CFERROR tags in the Application. cfm template are also disabled for error  cfm.
5. Because error Request. cfm is not the request error template, you can add Cold-Fusion code that will log the error variables into a database. These error variableswere passed as hidden form fields from the request error template through the URL.

The first step is to modify your Application: cfm template to disable error handling if the current template is error_request. cfm or error request2. cfm. You can determine the current template path by using the function and argument.

GETDIRECTORYFROMPATH(#CGI. CF_ TEMPLATE_PATH#).

To test this code, create an error by renaming a variable in your home. cfm template to an unassigned variable name. Load home. cfm in your browser using the following URL: http://127 .0.01/sybex/home. cfm. You should see error Jequest ..cfm pop up briefly and ther be redirected to error _request. cfm. If you were to check the databetable, the new error information would be logged in the errors table of the quiz  md file.

Posted on November 17, 2015 in Implementing Error Control

Share the Story

Back to Top
Share This