set_error_handler & set_exception_handler
I use the following functions to set error control:
set_error_handler
set_exception_hand开发者_运维知识库ler
When an error occurs, it calls this function which captures the errors and stores it to the database before redirect the user to a generic page
function log_error($exception)
{
if(is_object($exception))
log_action(ERROR, "Exception with message " . $exception->getMessage() . " thrown in " . $exception->getFile() . " on line " . $exception->getLine(), base_url() . $_SERVER["REQUEST_URI"]);
else
log_action(ERROR, "Unable to catch exception. print out: " . print_r($exception, true), base_url() . $_SERVER["REQUEST_URI"]);
header("Location: " . base_url() . "public_error_notification");
}
This usually works except a lot of times instead of getting an error object, I just get a number. Two numbers I get all the time are 8 and 2. You can see I account for those numbers in my error log. My question is, what do these numbers mean? When I looked it up, it made sense because there were error codes for files, but the errors I am getting now have nothing to do with files. I assume that they are error code, so is there any way that I can get a list of error codes that can be returned?
It seems you're using the same callback for both exception handler and error handler, which do not pass the same type of arguments.
Take a look at the set_error_handler doc, the first argument is the error code, second is the error string. No object is passed
http://php.net/set_error_handler
For the error codes check this:
http://php.net/manual/en/errorfunc.constants.php
The error_handler
callback takes a function with this signature:
handler ( int $errno , string $errstr [, string $errfile [, int $errline [, array $errcontext ]]] )
The first argument is the error number, the second is a string describing the error.
Meanwhile, the exception_handler
has a function with this signature:
handler ( object $exception )
Where the first and only argument is an object containing details of the exception thrown.
The problem you're having is that exceptions and errors don't have the same callback signature. While you're dealing appropriately with exceptions, you're using an inappropriate callback to handle errors.
When you see numbers like 8
and 2
you're catching errors, when you see the exception object, you're catching exceptions.
I'll just add that the number you get is the level of the error raised, there are contants defined for them in php, such as E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE.
精彩评论