Jump to content

Custom error handler not catching custom exception?


PatRoy

Recommended Posts

I'm sure it's not much, but I'm not understanding something with custom error handlers:

I've created a custom error handler, which I initially set when my page loads :

set_error_handler(array ( new ErrorHandler(), 'handleError' ));

 

It seems to catch all internal PHP errors, such as if I: var_dump($non_existing_var); right after the set_error_handler....

Now, I have an object that throws an exception after:

set_error_handler(array ( new ErrorHandler(), 'handleError' )); 
$locale = new \CorbeauPerdu\i18n\Locale(...); // this should throw an exception 
...

I thought that with an error handler set, I could 'skip' the try/catch for it, but doing so, PHP spits out in its internal log:

PHP Fatal error: Uncaught CorbeauPerdu\i18n\LocaleException: ....

 

My error handler doesn't catch it at all before, thus my page breaks!!

If I want it to go through the error handler, I have to init my Locale with a try/catch and use trigger_error() like so:

set_error_handler(array ( new ErrorHandler(), 'handleError' ));

try { 
  $locale = new \CorbeauPerdu\i18n\Locale(...); // this should throw an exception 
}
catch(Exception $e) { 
  trigger_error($e->getMessage(), E_USER_ERROR); 
} 
...

Is this normal ? I thought one of the goals of the error_handler was to catch anything that wasn't dealt with?

Thanks for your answers!

Link to comment
Share on other sites

Exceptions are different than errors, and there's a separate function to install a handler for them: set_exception_handler.

That kind of handling is only for doing something like logging the error or whatever however, you can't use it to ignore the exception and continue.  If you want your script to continue executing despite the exception, then you need to catch it with a try/catch block and handle it appropriately there.

 

  • Like 1
Link to comment
Share on other sites

1 hour ago, kicken said:

Exceptions are different than errors, and there's a separate function to install a handler for them: set_exception_handler.

That kind of handling is only for doing something like logging the error or whatever however, you can't use it to ignore the exception and continue.  If you want your script to continue executing despite the exception, then you need to catch it with a try/catch block and handle it appropriately there.

 

The idea is to stop the script whenever an error (or exception) occurs. My handler essentially logs the error with a uniqid() to a log file, sends an email to admins with the full error message + ID, and finally, outputs an html page with a generic message "Oh crap! Something went wrong! Contact admins with ID: <error id>"

So, from my understanding, I'd need 2 Error Handler classes: one for errors, and one for exceptions.... OR, I guess I could have in my ErrorHandler class 2 functions :

/** 
 * Handle Errors: set from set_error_handler()
 */
public function handleError($errno, $errstr, $errfile, $errline) {
  ...
  $this->sendToEmail();   // send email to admins
  $this->saveToLog();     // save to log file
  $this->loadErrorPage(); // print error page and exit()!
}

/** 
 * Handle Exceptions: set from set_exception_handler()
 */
public function handleException($ex) {
  ...  
  $this->sendToEmail();   // send email to admins
  $this->saveToLog();     // save to log file
  $this->loadErrorPage(); // print error page and exit()!
}

 

Would this be a proper way of doing this then ? I do not like having to trigger_error()  in my try/catch... For one thing, I think the script should catch any such mistakes, and for another, with trigger_error(), I do not have access to the Exception object (to stacktrace it, etc.).

Thanks for your inputs!

Pat

 

Link to comment
Share on other sites

Take a closer look at the documentation for set_error_handler() and you'll notice it says you cannot handle fatal (E_ERROR) errors.

The only thing you can do in that case is react to the fatal error after the fact: use register_shutdown_function() and error_get_last() to detect whether the script is ending because of a fatal error.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.