In this hands on, you are going to add an Error Handling solution to the web site.
Tags Used: <cfdump>
- Open up the /www/Application.cfc file in your code editor.
-
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 ) {
}
- Inside the function, include the file sorry.cfm.
-
After the file, place the following code, which will email the error information to you:
var errorEmail = new mail(); errorEmail.setTo('[email protected]'); errorEmail.setFrom('[email protected]'); 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();
</li> <li> Your function should look similar to this:
function onError( any Exception, string EventName ) { include 'sorry.cfm'; var errorEmail = new mail(); errorEmail.setTo('[email protected]'); errorEmail.setFrom('[email protected]'); 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(); }
</li> <li> Open up the <span class="code">/www/throwError.cfm</span> file in your browser. You should now see a 'sorry' page. </li> <li> Open up your ColdFusion Administrator and login. The URL for the ColdFusion Administrator is most likely <a href="http://localhost:8500/CFIDE/administrator/" target="_new">http://localhost:8500/CFIDE/administrator/</a>. </li> <li> Click on 'Mail' under Server Settings. </li> <li> Click on the 'View Undeliverable Mail'. </li> <li> 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. </li> <li> Open up the <span class="code">/www/throwError.cfm</span> file in your code editor. </li> <li> Change the contents of the file to the following code:
<cfdump value="2" />
</li> <li> Open up the <span class="code">/www/throwError.cfm</span> file in your browser and notice that an error is thrown. <span class="code">onError</span> does not catch tag syntax errors. For that, we must rely on the site-wide error handler. </li> <li> Create a new file called <span class="code">sitewideErrorHandler.cfm</span> in the <span class="code">/</span> folder. </li> <li> Open the <span class="code">/sitewideErrorHandler.cfm</span> file in your code editor. </li> <li> Add the following line of text to the file:
An error has occurred!
</li> <li> Go back to your ColdFusion Administrator. </li> <li> Click on 'Settings' under Server Settings. </li> <li> Go to the Site-wide Error Handler input and enter the following value:
/learncfinaweek/sitewideErrorHandler.cfm
</li> <li> Go back to the <span class="code">/www/throwError.cfm</span> page in your browser and refresh. </li> <li> 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. </li> </ol>