Now this is a pretty big discussion: Should you catch fatal errors or not. Some say they are FATAL, so you donโt know what state the system is in, but Iโll go with โtry to clean up if an error occursโ. To catch ALL fatal errors, you need to configure the pre_system binding. go to application / config / hooks.php and type
$hook['pre_system'][] = array( 'class' => 'PHPFatalError', 'function' => 'setHandler', 'filename' => 'PHPFatalError.php', 'filepath' => 'hooks' );
after that go to the hooks directory and add the handling of this error:
<?php class PHPFatalError { public function setHandler() { register_shutdown_function('handleShutdown'); } } function handleShutdown() { if (($error = error_get_last())) { ob_start(); echo "<pre>"; var_dump($error); echo "</pre>"; $message = ob_get_clean(); sendEmail($message); ob_start(); echo '{"status":"error","message":"Internal application error!"}'; ob_flush(); exit(); } }
as you can see, we use the register_shutdown_function function to run a function that checks if an error has occurred and if it emailed it to the developer. This setup has been working flawlessly for over two years in several CI projects I have worked with.
tix3
source share