PatRoy Posted May 13, 2020 Share Posted May 13, 2020 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! Quote Link to comment https://forums.phpfreaks.com/topic/310763-custom-error-handler-not-catching-custom-exception/ Share on other sites More sharing options...
kicken Posted May 13, 2020 Share Posted May 13, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310763-custom-error-handler-not-catching-custom-exception/#findComment-1577949 Share on other sites More sharing options...
PatRoy Posted May 13, 2020 Author Share Posted May 13, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310763-custom-error-handler-not-catching-custom-exception/#findComment-1577959 Share on other sites More sharing options...
requinix Posted May 13, 2020 Share Posted May 13, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310763-custom-error-handler-not-catching-custom-exception/#findComment-1577962 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.