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)
Parameter | Description |
---|---|
error_level | Required. Specifies the error report level for the user-defined error. Must be a value number. See table below for possible error report levels |
error_message | Required. Specifies the error message for the user-defined error |
error_file | Optional. Specifies the filename in which the error occurred |
error_line | Optional. Specifies the line number in which the error occurred |
error_context | Optional. 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:
Value | Constant | Description |
---|---|---|
2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted |
8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also happen when running a script normally |
256 | E_USER_ERROR | Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() |
512 | E_USER_WARNING | Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() |
1024 | E_USER_NOTICE | User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() |
4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()) |
8191 | E_ALL | All 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