In this chapter, you will learn how to customize the error messages that are displayed to your Web site users. Not only will this make it easier for users to decipher returned error messages, but the messages will also fit into the look and feel of your application. The examples in this chapter are built from those you worked with in Chapter 17, “Implementing the Cold Fusion Application Framework.” Please refer to Chapter 17 if you have questions about the application features of this chapter’s examples.
Handling Errors through the Application Framework
In the preceding chapter, you learned about creating ColdFusion applications using the ColdFusion Application Framework. In this section, you will learn how to implement generalized error control using that same application framework and a ColdFusion tag, (FERROR.
ColdFusion allows for two types of generalized error handling within the application framework (more specialized error handling for all situations will be covered in the “Working with Customized ExceptionHandling” section of this chapter). The types of general errors are validation errors, request errors, and exceptions.Validatj!)n errors occur when a user has improperly submitted a form. Perhaps they didn’t fill out required form fields, or the data they entered is not in the correct format. In Chapter 15, “Forms,” you learned how to use ColdFusion form tags to account for these user errors. The ColdFusion form tags then generated error messages to the user describing which fields were filled incorrectly or still needed to be filled. The error page that the user viewed, though, was not customized and did not necessarily flt into thelook and feel of your ColdFusion application (see Figure 18.1). ColdFusion enables you to create a customized template to display the same validation error information using the (FERROR tag.
Request errors occur during the normal processing of a ColdFusion template. These’ errors may be due to misspelled variable names, nonexistence of included files, or other template processing problems. Normally, if a variable name is misspelled, you will receive an error like that in Figure 18.3. But when using Cold Fusion error handling, you can customize the error messages.
To work with theColdFusion ‘error-handling mechanism, you need to do two things:
1. Place a CFERROR tag in the template that you would like to have ColdFusion check for errors.
2. Create a customized error template that ColdFusion jumps to whenever an error is encountered.
Understanding the CFERROR Tag
The CFELROR tag is placed in a template to catch application errors. As discussed previously, CFERROR is able to catch two types of errors: validation and request. The following code shows the correct syntax for both types of CFERROR tags:
Although the (FERROR tag can be used in any template in which you would like to catch errors, it can also be placed in the App ;eat; on. efm template to catch errors throughout your entire application. This is, in most cases, the best solution for error handling.
Because the App 1;eat; on. efm template is processed before each template in your application framework, adding your (FERROR tags to this template is useful for.catching all errors in your application.
Listing 18.1 shows how a (FERROR tag was added to the App 1; eat; on . efm template.
Creating Error Templates
In the preceding section, you added a (FERRORtag to your App it; on. cfm template This error tag pointed to a specific template intended to handle validation errors. The only problem is that you have. not yet created this template. In this section, you will ere ate error templates for both validation and request errors.
Before you start, you must know several facts about creating error templates:
• ColdFusion provides several error variables that you can access in your templates. These variables are different for validation and request error templates.
• You cannot use any ColdFusion code within your error templates except the error variables. This includes CFOUTPUT and CFIF tags, as well as any variables besides the error variables described above. Using other ColdFusion code could potentially create another error in your error template, and then the browser would be in a loop trying to process the errors .
• Because ColdFusion processing is limited on ColdFusion error templates, you also cannot encrypt them.