ISN (International Social Networking Services - Interactive Apps and Websites)



Errors and Logging sample source codes - PHP Scripts


 

Building a basic error handler with custom error types
 
<?php 
/** 
* Error Handling Class 
*/ 
 
/** 
* Error Handling Class 
* Code for: Setting vars, handling errors, mailing an error report and 
* writing to an error log. 
*/ 
class error_handle 
 
     
/** 
* The constructor function for error_handle. 
* sets the error handler to the custom one below 
*/ 
function error_handle() 
    { 
     
    // Sets the error handler to this one rather than default 
    // Parameters are the object containing the function and the 
    // function itsself. This is because the function is inside a class. 
    set_error_handler( array( &$this, 'error_handler' ) ); 
 
    } 
 
 
 
  /** 
  * The error handler set by set_error_handler() 
  * 
  * Checks to see what sort of error is being triggered. It can catch any of the E_WARNING, E_ERROR and E_NOTICE as well as the 
  * E_USER_* errors. You can also add your own custom types by adding them like I have done with BIG_SCARY_ERROR. Where it says to 
  * add your own code, that is where you put the code to be executed when the error of that type occurs. 
  * 
  * @param string $error_type The type of error being handled. 
  * @param string $error_message The error message being handled. 
  * @param string $error_file The file where the error occurred. 
  * @param integer $error_line The line where the error occurred. 
  * @param string $error_context The context in which the error occurred. 
  * @return boolean 
  */ 
  function error_handler($error_type, $error_message, $error_file, $error_line, $error_context) 
  { 
   switch($error_type) 
    { 
    case E_ERROR: 
    case E_USER_ERROR: 
    // Add your own code here! 
    echo '<p class="error">'.$error_message,' (An error type ',$error_type,' occurred). The administrator has been notified. This page might not function correctly.<br /></p>';   
    break; 
      
      
    case E_WARNING: 
    case E_USER_WARNING: 
    // Add your own code here! 
    echo '<p class="error">'.$error_message,' (An error type ',$error_type,' occurred). The administrator has been notified. This page might not function correctly.<br /></p>'; 
    break; 
 
    case E_NOTICE: 
    case E_USER_NOTICE: 
    // Add your own code here! 
    echo '<p class="error">'.$error_message,' (An error type ',$error_type,' occurred). The administrator has been notified. This page might not function correctly.<br /></p>'; 
    break; 
     
    case BIG_SCARY_ERROR: 
    echo 'A *huge* scary error has been seen skulking around on line '.$error_line.' of '.$error_file.'! It\'s shouting "'.$error_message.'"'; 
    break; 
    } 
return true; 
  } 
 
 
?> 
 
To use this error handler, put in in a file, and include it into your script. Then, instantiate it (eg $error_handler = new error_handler() ). The whenever an error occurs, the class will catch it. To trigger your own errors do: 
 
trigger_error('error message typed here', E_USER_ERROR); 
(where the bit in capitals is the type you want to trigger). 
So you can do this to trigger a custom error type: 
 
trigger_error('I'm going to eat your PHP code!!! Bwahahahahaaa', BIG_SCARY_ERROR);
By continuing to use this site, you agree to the use of cookies to personalize content and advertisements, to provide social media functionality, to analyze our traffic using Google services like Analytics and Adsense.

Google Adsense and its partners may use your data for advertising personalization and cookies may be used for personalized and non-personalized advertising. How does Google use my data?
Please use the following button to see the list of Google partners as well as all the details regarding cookies.
See detailsI Accept
These cookies are mandatory for the operation of isn-services.com, if you do not accept them please quit this site.
You have the right to refuse cookies and leave the site or to change the parameters.