<<

Error Handling and Debugging

Hands On 31

By Simon Free (Bio)
gist

In this hands on, you are going to add an Error Handling solution to the web site.

Tags Used: <cfdump>

Functions Used: include, mail

  1. Open up the /www/Application.cfc file in your code editor.
  2. After the onRequestStart function, create a new function called onError. The function will accept 2 arguments. The first is of type any and is called Exception. The second is of type String and is called EventName. Your code should look similar to this:
    function onError( any Exception, string EventName ) {
    	
    }
    
  3. Inside the function, include the file sorry.cfm.
  4. After the file, place the following code, which will email the error information to you:
    var errorEmail = new mail();
    errorEmail.setTo('you@domain.com');
    errorEmail.setFrom('system@domain.com');
    errorEmail.setSubject('An Error has Occured');
    errorEmail.setBody('
    	Message: #arguments.exception.message# <br />
    	Details: #arguments.exception.detail# <br />
    	Type: #arguments.exception.type# <br />
    ');
    errorEmail.setType('html');
    errorEmail.send();	
    
  5. Your function should look similar to this:
    function onError( any Exception, string EventName ) {
    	include 'sorry.cfm';
    	var errorEmail = new mail();
    	errorEmail.setTo('you@domain.com');
    	errorEmail.setFrom('system@domain.com');
    	errorEmail.setSubject('An Error has Occured');
    	errorEmail.setBody('
    		Message: #arguments.exception.message# <br />
    		Details: #arguments.exception.detail# <br />
    		Type: #arguments.exception.type# <br />
    	');
    	errorEmail.setType('html');
    	errorEmail.send();
    
    }	
    
  6. Open up the /www/throwError.cfm file in your browser. You should now see a 'sorry' page.
  7. Open up your ColdFusion Administrator and login. The URL for the ColdFusion Administrator is most likely http://localhost:8500/CFIDE/administrator/.
  8. Click on 'Mail' under Server Settings.
  9. Click on the 'View Undeliverable Mail'.
  10. The first item in the list should be your error email. Review the contents of the email that is sent when an email is thrown.
  11. Open up the /www/throwError.cfm file in your code editor.
  12. Change the contents of the file to the following code:
    <cfdump value="2" />	
    
  13. Open up the /www/throwError.cfm file in your browser and notice that an error is thrown. onError does not catch tag syntax errors. For that, we must rely on the site-wide error handler.
  14. Create a new file called sitewideErrorHandler.cfm in the / folder.
  15. Open the /sitewideErrorHandler.cfm file in your code editor.
  16. Add the following line of text to the file:
    An error has occurred!
    
  17. Go back to your ColdFusion Administrator.
  18. Click on 'Settings' under Server Settings.
  19. Go to the Site-wide Error Handler input and enter the following value:
    /learncfinaweek/sitewideErrorHandler.cfm
    
  20. Go back to the /www/throwError.cfm page in your browser and refresh.
  21. You will now see the message 'An error has occurred!'. The site wide error handler has caught the exception and displayed a friendlier message to the user. You can now find details about the specific error from the server log files.