Difference between revisions of "Error Handling"

From Hostek.com Wiki
Jump to: navigation, search
m (Briana moved page Error Page Handling to Error Handling)
(ColdFusion)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Change Error Page Handling==
+
__FORCETOC__
 +
 
 +
== cPanel ==
 +
We have a video tutorial for this if you would rather watch it instead of reading below.
 +
 
 +
http://hostek.com/tutorials/cpanel-x3-error.html
 +
 
 +
 
 +
You will need to log into your cPanel and then click on Error pages in the Advanced section.
 +
 
 +
To edit an error page:
 +
 
 +
1. Click the error code for the page you wish to edit.
 +
<pre style="color:red"> If you don’t see the error code for the page you wish to edit, click the Show All HTTP Error Status Codes tab.</pre>
 +
 +
2. Type a message into the text box. ◦ You can select certain HTML tags to be added to a custom error page. These tags will display facts on the error page about the visitor accessing your website.
 +
<pre style="color:red">You can further customize your error pages by keying in HTML code. </pre>
 +
 
 +
3. Click Save.
 +
 
 +
Alternatively you can design these pages offline and upload them via FTP like normal pages to your public_html folder. Remember, these files must have the .shtml extension.
 +
 
 +
i.e.
 +
 
 +
*400.shtml
 +
*401.shtml
 +
*403.shtml
 +
*404.shtml
 +
*500.shtml
 +
 
 +
== Windows based (WCP) ==
 +
* Login to WCP
 +
* In the Website Settings section, click on IIS Settings
 +
* Click the pencil/edit icon to change the handling for the specific error code
 +
* The Help in WCP has additional information
 +
 
 +
== cPanel VPS ==
 +
You will need to log into your cPanel and then click on Error pages in the Advanced section.
 +
 
 +
To edit an error page:
 +
 
 +
1. Click the error code for the page you wish to edit.
 +
<pre style="color:red"> If you don’t see the error code for the page you wish to edit, click the Show All HTTP Error Status Codes tab.</pre>
 +
 +
2. Type a message into the text box. ◦ You can select certain HTML tags to be added to a custom error page. These tags will display facts on the error page about the visitor accessing your website.
 +
<pre style="color:red">You can further customize your error pages by keying in HTML code. </pre>
 +
 
 +
3. Click Save.
 +
 
 +
Alternatively you can design these pages offline and upload them via FTP like normal pages to your public_html folder. Remember, these files must have the .shtml extension.
 +
 
 +
i.e.
 +
 
 +
*400.shtml
 +
*401.shtml
 +
*403.shtml
 +
*404.shtml
 +
*500.shtml
 +
 
 +
== Windows VPS ==
 
You can change what type of error pages are served from your Windows or ColdFusion Windows VPS in IIS by using the following steps:
 
You can change what type of error pages are served from your Windows or ColdFusion Windows VPS in IIS by using the following steps:
  
Line 13: Line 72:
 
* <b>Detailed error pages for local requests, custom error pages for remote requests</b> - Any requests coming directly from the VPS server will display detailed error pages, any requests from outside the VPS server will display custom error pages
 
* <b>Detailed error pages for local requests, custom error pages for remote requests</b> - Any requests coming directly from the VPS server will display detailed error pages, any requests from outside the VPS server will display custom error pages
  
 +
 +
== ColdFusion ==
 +
Here is an example for handling errors within your '''application.cfc''' file:
 +
 +
<pre style="white-space: pre-wrap">
 +
<cfcomponent>
 +
<cfset This.name = "ErrorHanldingExample">
 +
<cfset This.LogPath = expandPath("CFError.log")>
 +
<cfset This.ErrorTemplate = "MyErrorPage.cfm">
 +
 +
<!--- Handle 404 errors --->
 +
<cffunction name="onMissingTemplate" returnType="boolean">
 +
    <cfargument type="string" name="targetPage" required=true/>
 +
 +
<cftry>
 +
 
 +
    <!--- set response to 404 for Search Engine and statistical purposes --->
 +
    <cfheader
 +
      statusCode = "404"
 +
      statusText = "Page Not Found"
 +
    >
 +
 
 +
    <!--- retrieve the error template from the application scope --->
 +
    <cflock timeout="5" scope="application">
 +
        <cfset ErrorTemplate = This.ErrorTemplate>
 +
    </cflock>
 +
   
 +
    <!--- include a template to show to the user ---> 
 +
    <cfinclude template = "#ErrorTemplate#">
 +
 
 +
    <!--- return true to prevent the default ColdFusion error handler from running --->
 +
    <cfreturn true />
 +
 
 +
    <cfcatch>
 +
    <!--- If an error occurs within the error handler routine, allow ColdFusion's default error handler to run --->
 +
        <cfreturn false />
 +
    </cfcatch>
 +
</cftry>
 +
 +
</cffunction>
 +
 +
<!--- Handle script related / 500 errors --->
 +
<cffunction name="onError">
 +
    <!--- The onError method gets two arguments:
 +
            * An exception structure, which is identical to a cfcatch variable.
 +
            * The name of the Application.cfc method, if any, in which the error happened. --->
 +
    <cfargument name="Except" required=true/>
 +
    <cfargument type="String" name = "EventName" required=true/>
 +
 +
<!--- Throw validation errors to ColdFusion for handling. --->
 +
    <cfif Find("coldfusion.filter.FormValidationException", Arguments.Except.StackTrace)>
 +
        <cfthrow object="#except#">
 +
<cfelse>
 +
    <cftry>
 +
            <!--- Set response to 500 for Search Engine and statistics purposes --->
 +
            <cfheader
 +
              statusCode = "500"
 +
              statusText = "Internal Server Error"
 +
            >
 +
    <!--- Log all errors in an application-specific log file. --->
 +
<cfset NewLine = chr(13) & chr(10)>
 +
<cfset ErrorMessage = "Event: #Eventname##NewLine#Error: #Except.Message##NewLine#">
 +
               
 +
                <!--- retrieve the log path from the application scope --->
 +
                <cflock timeout="5" scope="application">
 +
                    <cfset logPath = This.LogPath>
 +
</cflock>
 +
 +
<cffile
 +
  action = "append"
 +
  file = "#logPath#"
 +
  output = "#ErrorMessage#"
 +
>
 +
 +
    <!--- Run any application specific error handling code and include a template to be shown to the user --->
 +
 +
                <!--- retrieve the error template from the application scope --->
 +
                <cflock timeout="5" scope="application">
 +
                    <cfset ErrorTemplate = This.ErrorTemplate>
 +
                </cflock>
 +
   
 +
                <!--- include a template to show to the user ---> 
 +
                <cfinclude template = "#ErrorTemplate#">
 +
       
 +
    <!--- If an error occurs within the error handler, the cfcatch routine will run.  In that case, we will return a custom error to the user.  This should only happen if there is a permissions problem writing to the log or the error template does not exist. --->
 +
    <cfcatch>
 +
        <cfthrow message="An error has occured.  Additionally an error was encountered when processing the site's error handling routine. <br><br> #cfcatch.message#">
 +
    </cfcatch>
 +
        </cftry>
 +
    </cfif>
 +
 +
</cffunction>
 +
</cfcomponent>
 +
</pre>
 +
 +
ColdFusion 8 and above provide handling of 404 errors and 500 errors through two separate functions in the application.cfc. The 'onMissingTemplate' function is used to handle 404 errors and the 'onError' function is used to handle 500 errors.
 +
 +
In the above example, there are two application variables that determine in what file 500 errors are logged and which page to display to the user when an error occurs:
 +
 +
<pre>
 +
<cfset This.LogPath = expandPath("CFError.log")>
 +
<cfset This.ErrorTemplate = "MyErrorPage.cfm">
 +
</pre>
 +
 +
This is just an example to show those new to error handling in ColdFusion how these functions can be used.
 +
 +
'''NOTE:''' If your site uses an '''application.cfm''' file, it would need to be converted to an '''application.cfc''' file(requires code changes) in order to utilize these functions. If both an '''application.cfc''' and an '''application.cfm''' are present in the same folder of your ColdFusion application, only the '''application.cfc''' will run and the '''application.cfm''' will be ignored.
 +
 +
[[Category:Windows]]
 +
[[Category:ColdFusion]]
 
[[Category:ColdFusion-VPS]]
 
[[Category:ColdFusion-VPS]]
 
[[Category:Windows-VPS]]
 
[[Category:Windows-VPS]]

Latest revision as of 16:17, 12 December 2012


cPanel

We have a video tutorial for this if you would rather watch it instead of reading below.

http://hostek.com/tutorials/cpanel-x3-error.html


You will need to log into your cPanel and then click on Error pages in the Advanced section.

To edit an error page:

1. Click the error code for the page you wish to edit.

 If you don’t see the error code for the page you wish to edit, click the Show All HTTP Error Status Codes tab.

2. Type a message into the text box. ◦ You can select certain HTML tags to be added to a custom error page. These tags will display facts on the error page about the visitor accessing your website.

You can further customize your error pages by keying in HTML code. 

3. Click Save.

Alternatively you can design these pages offline and upload them via FTP like normal pages to your public_html folder. Remember, these files must have the .shtml extension.

i.e.

  • 400.shtml
  • 401.shtml
  • 403.shtml
  • 404.shtml
  • 500.shtml

Windows based (WCP)

  • Login to WCP
  • In the Website Settings section, click on IIS Settings
  • Click the pencil/edit icon to change the handling for the specific error code
  • The Help in WCP has additional information

cPanel VPS

You will need to log into your cPanel and then click on Error pages in the Advanced section.

To edit an error page:

1. Click the error code for the page you wish to edit.

 If you don’t see the error code for the page you wish to edit, click the Show All HTTP Error Status Codes tab.

2. Type a message into the text box. ◦ You can select certain HTML tags to be added to a custom error page. These tags will display facts on the error page about the visitor accessing your website.

You can further customize your error pages by keying in HTML code. 

3. Click Save.

Alternatively you can design these pages offline and upload them via FTP like normal pages to your public_html folder. Remember, these files must have the .shtml extension.

i.e.

  • 400.shtml
  • 401.shtml
  • 403.shtml
  • 404.shtml
  • 500.shtml

Windows VPS

You can change what type of error pages are served from your Windows or ColdFusion Windows VPS in IIS by using the following steps:

  1. Open IIS 7.5 from the desktop of your Windows or ColdFusion Windows VPS
  2. Click the name of your VPS server in IIS
  3. Double click "Error Pages" under the "IIS" section
  4. Click "Edit Feature Settings" under the "Actions" pane on the right
  5. Select the type of error pages you would like to be displayed for your sites on the VPS (the types of error pages are described below)

Types of Error Pages

  • Custom error pages - Will display any custom error pages you have set up in the WCP
  • Detailed error pages - Will display either an IIS error page, or a ColdFusion error page depending on what type of file is being served
  • Detailed error pages for local requests, custom error pages for remote requests - Any requests coming directly from the VPS server will display detailed error pages, any requests from outside the VPS server will display custom error pages


ColdFusion

Here is an example for handling errors within your application.cfc file:

<cfcomponent>
<cfset This.name = "ErrorHanldingExample">
<cfset This.LogPath = expandPath("CFError.log")>
<cfset This.ErrorTemplate = "MyErrorPage.cfm">

<!--- Handle 404 errors --->
<cffunction name="onMissingTemplate" returnType="boolean">
    <cfargument type="string" name="targetPage" required=true/>

<cftry>
   
    <!--- set response to 404 for Search Engine and statistical purposes --->
    <cfheader 
       statusCode = "404"
       statusText = "Page Not Found"
    >
   
    <!--- retrieve the error template from the application scope --->
    <cflock timeout="5" scope="application">
        <cfset ErrorTemplate = This.ErrorTemplate>
    </cflock>
    
    <!--- include a template to show to the user --->   
    <cfinclude template = "#ErrorTemplate#">
   
    <!--- return true to prevent the default ColdFusion error handler from running --->
    <cfreturn true />
   
    <cfcatch>
    <!--- If an error occurs within the error handler routine, allow ColdFusion's default error handler to run --->
        <cfreturn false />
    </cfcatch>
</cftry>

</cffunction>

<!--- Handle script related / 500 errors --->
<cffunction name="onError">
    <!--- The onError method gets two arguments:
            * An exception structure, which is identical to a cfcatch variable.
            * The name of the Application.cfc method, if any, in which the error happened. --->
    <cfargument name="Except" required=true/>
    <cfargument type="String" name = "EventName" required=true/>
	
	<!--- Throw validation errors to ColdFusion for handling. --->
    <cfif Find("coldfusion.filter.FormValidationException", Arguments.Except.StackTrace)>
        <cfthrow object="#except#">
	<cfelse>
	    <cftry>
            <!--- Set response to 500 for Search Engine and statistics purposes --->
            <cfheader 
               statusCode = "500"
               statusText = "Internal Server Error"
            >
	    <!--- Log all errors in an application-specific log file. --->
		<cfset NewLine = chr(13) & chr(10)>
		<cfset ErrorMessage = "Event: #Eventname##NewLine#Error: #Except.Message##NewLine#">
                
                <!--- retrieve the log path from the application scope --->
                <cflock timeout="5" scope="application">
                    <cfset logPath = This.LogPath>
		</cflock>

		<cffile
		   action = "append"
		   file = "#logPath#"
		   output = "#ErrorMessage#"
		>
		
    		<!--- Run any application specific error handling code and include a template to be shown to the user --->

                <!--- retrieve the error template from the application scope --->
                <cflock timeout="5" scope="application">
                    <cfset ErrorTemplate = This.ErrorTemplate>
                </cflock>
    
                <!--- include a template to show to the user --->   
                <cfinclude template = "#ErrorTemplate#">
        
	    <!--- If an error occurs within the error handler, the cfcatch routine will run.  In that case, we will return a custom error to the user.  This should only happen if there is a permissions problem writing to the log or the error template does not exist. --->
	    <cfcatch>
	        <cfthrow message="An error has occured.  Additionally an error was encountered when processing the site's error handling routine. <br><br> #cfcatch.message#">
	    </cfcatch>
        </cftry>
    </cfif>
	
</cffunction>
</cfcomponent>

ColdFusion 8 and above provide handling of 404 errors and 500 errors through two separate functions in the application.cfc. The 'onMissingTemplate' function is used to handle 404 errors and the 'onError' function is used to handle 500 errors.

In the above example, there are two application variables that determine in what file 500 errors are logged and which page to display to the user when an error occurs:

<cfset This.LogPath = expandPath("CFError.log")>
<cfset This.ErrorTemplate = "MyErrorPage.cfm">

This is just an example to show those new to error handling in ColdFusion how these functions can be used.

NOTE: If your site uses an application.cfm file, it would need to be converted to an application.cfc file(requires code changes) in order to utilize these functions. If both an application.cfc and an application.cfm are present in the same folder of your ColdFusion application, only the application.cfc will run and the application.cfm will be ignored.