130 likes | 233 Views
Error Catching with CFERROR and CFCATCH. Douglas Smith, Application Architect TeraTech, Inc. Tuesday, October 9, 2001. Welcome/Goals. This presentation is about catching and monitoring errors, not general error handling Quick Review of CFERROR and CFCATCH When to use CFERROR vs. CFCATCH
E N D
Error Catching with CFERROR and CFCATCH Douglas Smith, Application Architect TeraTech, Inc. Tuesday, October 9, 2001
Welcome/Goals • This presentation is about catching and monitoring errors, not general error handling • Quick Review of CFERROR and CFCATCH • When to use CFERROR vs. CFCATCH • Tips and Tricks using CFERROR and CFCATCH
Why Catch Errors • Users don't report errors! • Users go away if there are errors! • Client's don't report errors! • Remote web site (no access to CF log) • Errors are reported incorrectly (do you remember the "exact" error message, sir?") • Don't know how many errors there really are! • Higher confidence in your web site. • Security - Hide errors message from potential hackers, so they can't see code or database info
How to Catch Errors • Display on the screen • Emailing Your Errors to yourself • Logging Your Errors to a DB or Text file
CFERROR • Type=Request • Outdated. Use a global 404 handler for this. • Can't run CF code. • Type=Exception • Can run ANY cf code! • Type=Monitor • Very similar to Exception, except not visible to the user. • Type=Validation • Used for form validation • We are mainly concerned with "Exception" type for this presentation
CFERROR Variables • Error.Diagnostics - Detailed error diagnostics from ColdFusion Server. • Error.MailTo - Email address of administrator who should be notified (corresponds to the value set in the MAILTO attribute of CFERROR). • Error.DateTime - Date and time when the error occurred. • Error.Browser - Browser that was running when the error occurred. • Error.GeneratedContent - The failed request's generated content . • Error.RemoteAddress - IP address of the remote client. • Error.HTTPReferer – Page from which the client accessed the link to the page where the error occurred. • Error.Template - Page being executed when the error occurred. • Error.QueryString - URL query string of the client's request.
CFCATCH • Type = Any (default) • Type = APPLICATION (default) • Type = Database • Type = Template • Type = Security • Type = Object • Type = MissingInclude • Type = Expression • Type = Lock • Type = Custom_type
CFCATCH Example • Current example is intended to be used as an include. • Can be setup to be a custom tag or module
CFCATCH Variables • CFCATCH.Type - Exception type • CFCATCH.Message - The exception's diagnostic message, if one was provided. If no diagnostic message is available, this is an empty string. • CFCATCH.Detail - A detailed message from the CFML interpreter. This message, which contains HTML formatting, can help determine which tag threw the exception. • CFCATCH.TagContext - The tag stack: the name and position of each tag in the tag stack, and the full path names of the files that contain the tags in the tag stack. See the note that follows this list for more information. • CFCATCH.NativeErrorCode - TYPE=Database only. The native error code associated with this exception. Database drivers typically provide error codes to assist diagnosis of failing database operations. If no error code was provided, the value of NativeErrorCode is -1. • CFCATCH.SQLSTATE - TYPE=Database only. The SQLState associated with this exception. Database drivers typically provide error codes to assist diagnosis of failing database operations. If no SQLState value was provided, the value of SQLSTATE is -1. • CFCATCH.ErrNumber - TYPE=Expression only. Internal expression error number. • CFCATCH.MissingFileName - TYPE=MissingInclude only. Name of the file that could not be included. • CFCATCH.LockName - TYPE=Lock only. The name of the affected lock (set to anonymous if the lock was unnamed). • CFCATCH.LockOperation - TYPE=Lock only. The operation that failed (set to Timeout, Create Mutex, or Unknown). • CFCATCH.ErrorCode - TYPE=Custom type only. A string error code. • CFCATCH.ExtendedInfo - TYPE=APPLICATION and custom only. A custom error message.
Other Variables that can be used • CGI.REMOTE_ADDR • CGI.HTTP_Referer • CGI.SCRIPT_NAME • CGI.HTTP_USER_AGENT • CGI.SERVER_NAME • user id if you have a members only site • Other app variables that tell you stuff
CFERROR Summary • Pros • Global for entire application • Setup in one location • Easy! • When to Use • When you don't feel like learning about CFTRY/CFCATCH • When you need a quick, global solution • Tips and Tricks • Treat like any other page on your CF web site, give them a complete menu, etc. • Can contain custom tags and includes (header, footers, etc.)
CFCATCH Summary • Pros • Can handle different exception types differently! • Can totally hide or ignore errors, as needed. • When to Use • You don't want to set the user off on another direction. • You want to handle different errors differently • You want to throw (and catch) your own errors • Want to retry a locked database error • When using Fusebox • Tips and Tricks • Treat like an include or module • Can be used in conjunction with CFERROR Type=Monitor • Use the context array variable! • Always use Type=Any unless you have a reason not to • If specifying the error type, always use ANY as catch all afterwards
Contact Info • Douglas Smith, Douglas@teratech.com • http://www.teratech.com • Source Code: http://www.cfug-md.org/meetings.cfm