Reputation: 7318
Here is the code I use to catch at the global level missed exceptions and errors:
set_exception_handler( function( Exception $e ) {
$exceptionCode = $e->getCode();
switch ( $exceptionCode ) {
case '42000': # Database
FlashMessages::flashIt( 'message', 'There is a syntax error in the db query' );
include( Settings::ABSPATH . '/src/views/message.php' );
var_dump( $e );
exit;
break;
default:
FlashMessages::flashIt( 'message', 'Something unpredicted happened.' );
include( Settings::ABSPATH . '/src/views/message.php' );
var_dump( $e );
exit;
break;
}
} );
set_error_handler( function( $errno, $errstr, $errfile, $errline ) {
FlashMessages::flashIt( 'message', 'An error happened.' );
include( Settings::ABSPATH . '/src/views/message.php' );
var_dump( $errstr );
exit;
} );
What I expected: All exceptions caught by set_exception_handler. All errors caught by set_error_handler.
What I have: set_exception_handler tries to catch errors and generate problems: Fatal error: Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of Exception, instance of Error given
set_error_handler doesn't catch this kind of error even if I completely remove the set_exception_handler.
Question: how can I catch from those global functions the kind of error that is missed by both of these function ?
Upvotes: 0
Views: 1163
Reputation: 8060
Change exception handler param type from Exception
to Throwable
.
http://php.net/manual/en/function.set-exception-handler.php#refsect1-function.set-exception-handler-changelog
Upvotes: 2