Saturday, September 21, 2013

PHP Custom Error Handling

Creating a Custom Error Handler

Creating a custom error handler is quite simple. We simply create a special function that can be called when an error occurs in PHP.
This function must be able to handle a minimum of two parameters (error level and error message) but can accept up to five parameters (optionally: file, line-number, and the error context):

 Syntax

error_function(error_level,error_message,error_file,error_line,error_context)

ParameterDescription
error_levelRequired. Specifies the error report level for the user-defined error. Must be a value number. See table below for possible error report levels
error_messageRequired. Specifies the error message for the user-defined error
error_fileOptional. Specifies the filename in which the error occurred
error_lineOptional. Specifies the line number in which the error occurred
error_contextOptional. Specifies an array containing every variable, and their values, in use when the error occurred

Error Report levels

These error report levels are the different types of error the user-defined error handler can be used for:
ValueConstantDescription
2E_WARNINGNon-fatal run-time errors. Execution of the script is not halted
8E_NOTICERun-time notices. The script found something that might be an error, but could also happen when running a script normally
256E_USER_ERRORFatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512E_USER_WARNINGNon-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024E_USER_NOTICEUser-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
4096E_RECOVERABLE_ERRORCatchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8191E_ALLAll errors and warnings (E_STRICT became a part of E_ALL in PHP 5.4)

Now lets create a function to handle errors:



<?php
error_reporting(E_ALL);

function myErrorHandler($errorNo, $errorStr, $errFile, $errLine) {
    switch ($errorNo) {
        case E_USER_ERROR:
            echo '<BR>E_USER_ERROR';
            echo "<BR>Error no: ".$errorNo;
            echo "<BR>Error str: ".$errorStr;
            echo "<BR>Error file: ".$errFile;
            echo "<BR>Error line: ".$errLine;
            break;
        case E_WARNING:
        case E_USER_WARNING:
            echo '<BR>E_WARNING, E_USER_WARNING';
            echo "<BR>Error no: ".$errorNo;
            echo "<BR>Error str: ".$errorStr;
            echo "<BR>Error file: ".$errFile;
            echo "<BR>Error line: ".$errLine;
            break;
        case E_NOTICE:
        case E_USER_NOTICE:
            echo '<BR>E_NOTICE, E_USER_NOTICE';
            echo "<BR>Error no: ".$errorNo;
            echo "<BR>Error str: ".$errorStr;
            echo "<BR>Error file: ".$errFile;
            echo "<BR>Error line: ".$errLine;
            break;
        default :
            echo '<BR>default';
            echo "<BR>Error no: ".$errorNo;
            echo "<BR>Error str: ".$errorStr;
            echo "<BR>Error file: ".$errFile;
            echo "<BR>Error line: ".$errLine;
            break;
    }
    echo "<BR>";
    /* Don't execute PHP internal error handler */
    return true;
}
set_error_handler("myErrorHandler");

$x = $y + 3;

$f = fopen("a.txt", "r");
?>

And output would be like:

E_NOTICE, E_USER_NOTICE
Error no: 8
Error str: Undefined variable: y
Error file: C:\xampp\htdocs\index.php
Error line: 42

E_WARNING, E_USER_WARNING
Error no: 2
Error str: fopen(a.txt) [function.fopen]: failed to open stream: No such file or directory
Error file: C:\xampp\htdocs\index.php
Error line: 44

No comments:

Post a Comment