SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Custom Error Messages With Own Error Handler ?

    Hello Folks, just something....

    I have the following script to control PHP error reporting - output in this case, though the error message is generated by PHP; Is it possible to change what I have to send my own message instead ?

    For example, I could get this error below, which isn't ideal since it gives a directory path:

    PHP Code:
    Failed opening \'templates/config/config.php\' for inclusion (include_path=\'.;c:\\php4\\pear\') 
    Here is my script I use:

    PHP Code:
    # begin
     # set the error reporting level for script
     
    error_reporting(E_USER_ERROR E_USER_WARNING E_USER_NOTICE);
     
    # set error handler to generate error reporting and logging
     
    $error_handler set_error_handler("errorHandler");
     
     
    function 
    errorHandler($errno$errmsg) {
      
    # found an error so alert user to error message and log error
      
    header('location:errors.php?msg='$errmsg);
      
    # prevent further script execution
      
    exit();
     } 
    The error message simply gets displayed in error.php; nothing too grand about this as it's all HTML

    Any idea's and suggestions are welcome as always.

  2. #2
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd do some logging along with that, and perhaps have it e-mail you if there are too many errors in a certain period of time.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yer I know Took care of the logging just now actually, though I cannot display/log a message of the error since IMO it is plain ugly

    I'd like to pass along my own message based on the error level, such as 'Warning', 'Fatal', etc etc

    And maybe then offer a number of suggestions based on the level as to what went wrong, since I have the filename and line number of the script if an error happens.

    Looking over the manual, there is no idea of how you can pass a long your own message; your stuck with what PHP gives you....

    ... which IMO is a bit silly really; can't wait until PHP5 with it's try... catch... exceptions...


  4. #4
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The first argument to your error handling function contains the error level. You can do a switch statement based on that to get what you want. It's in the example on http://www.php.net/set_error_handler.

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Done that as well;

    Now everthing that I need gets passed bar my own message; and there isn't a way of doing this that I've yet to find either;

    Most annoying, really it is

  6. #6
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking that you could build the 'displayed' error message on the error page itself. You could select which message to display based on the file and/or line that generated the error and the level of the error message. If you don't want the error page to get too bloated, there isn't any reson that you couldn't have a different error page for each 'operation group' of your web site (one for stuff dealing with user accounts, one for stuff dealing with record type X, etc.)

  7. #7
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm....

    Yer, something like that might actually work although it'd be a nightmare to maintain and as soon as you've changed your script, the line numbers change also....

    What I was thinking, is that I could somehow pass along an integer to the error handler and from this, based on the type of error, select the required message from an array - from configuration - based on the integer, so if a file wasn't included, instead of getting the message I posted in the first post, I could have for example:

    PHP Code:
    Unable to open application due to missing or corrupt fileconfig.cfg from directory: /templates
    This way, the actual messages are configurable as well from the point of view of the administrator for example ?

    But at the moment I am stuck with just displaying the file the error took place in and the line number so someone has to go into the script, see the line and then deduce that a file is missing; if they can understand the basics of PHP that is

  8. #8
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    function errorHandler($errno, $errmsg, $filename, $linenum) {
    # create array to hold type of error
    $errorType = array (
    1 => "Error",
    2 => "Warning",
    4 => "Parsing Error",
    8 => "Notice",
    16 => "Core Error",
    32 => "Core Warning",
    64 => "Compile Error",
    128 => "Compile Warning",
    256 => "User Error",
    512 => "User Warning",
    1024 => "User Notice" );

    # found an error so alert user to error message and log error
    header('location:errors.php?name=' . basename($filename) . '&num=' . $linenum . '&type=' . $errorType[$errno]);
    # prevent further script execution
    exit();
    }


    # and the HTML file

    <head>
    <title> Error Report </title>
    <link rel="stylesheet" type="text/css" href="templates/style.css" />
    </head>
    <body leftmargin="0" topmargin="12" marginwidth="0" marginheight="12">
    <table border="0" width="992" height="100%" align="center" valign="center" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
    <td height="50%">&nbsp;</td></tr>
    <tr>
    <td align="center" valign="center" width="100%" height="64">

    <table style="border:solid 1px rgb(144, 184, 175);background-color:rgb(223, 239, 208);" border="0" width="75%" align="center" height="192" valign="center" cellspacing="0" cellpadding="0">
    <tbody>
    <tr>
    <td align="center" valign="top" width="100%" height="16">
    <b>ERROR REPORT - <?php echo(strtoupper($_GET['type'])); ?></b>
    </td></tr>
    <td align="center" valign="center" width="100%" height="100%">
    <p>An error has occured with this application - please see below for more about this error.</p>
    <p>The error originated from file <b><?php echo($_GET['name']); ?></b> at line number <b><?php echo($_GET['num']); ?></b></p>
    <p>This error has now been logged - if it persists, please report this error to your administrator.</p>
    <p><button onClick='window.location="index.php";'>Go Back</button></p>
    </td></tr>
    </tbody></table>

    </td></tr>
    <tr>
    <td height="50%">&nbsp;</td></tr>
    </tbody></table>
    <?php 
    # log error to log
    $date date('Y-m-d H:i:s');
    $mess $date " [File: " $_GET['name'] . " - on line " $_GET['num'] . " - Level: " $_GET['type'] . "]\r\n";
    # logging...
    if($fp fopen('logs/error-log.txt'"a+" )) {
    fputs($fp$mess);
    fclose($fp);
    }
    ?>
    </body></html>
    ...

  9. #9
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    trigger_error('...'E_USER_NOTICE); 
    Ummm.... Isn't always that obvious I suppose ?


  10. #10
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just remember:
    Note: The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR and E_COMPILE_WARNING.
    Failed calls to 'require' generate an E_ERROR level error, will therefore not use your error handling function, and will reveal the path of the attempted include if you have errors being displayed on the page.

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers - although can't see myself using require() - I use include_once() near all the time.

    Now all I need to do is put the above to OOP and I'll be even happier....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •