CodeIgniter builds error reporting into your system through Exceptions, both the
SPL collection, as well as a few custom exceptions that are provided by the framework. Depending on your environment’s setup, the default action when an error or exception is thrown is to display a detailed error report unless the application is running under the This section is a quick overview for newer programmers, or for developers who are not experienced with using exceptions. Exceptions are simply events that happen when the exception is “thrown”. This halts the current flow of the script, and
execution is then sent to the error handler which displays the appropriate error page: If you are calling a method that might throw an exception, you can catch that exception using a <?php try { $user = $userModel->find($id); } catch (\Exception $e) { exit($e->getMessage()); } If the In the example above, we catch any type of Exception. If we
only want to watch for specific types of exceptions, like a <?php try { $user = $userModel->find($id); } catch (\CodeIgniter\UnknownFileException $e) { // do something here... } This can be handy for handling the error yourself, or for performing cleanup before the script ends. If you want the error handler to function as normal, you can throw a new exception within the catch block: <?php try { $user = $userModel->find($id); } catch (\CodeIgniter\UnknownFileException $e) { // do something here... throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } ConfigurationBy default, CodeIgniter will display all errors in the Important Disabling error reporting DOES NOT stop logs from being written if there are errors. Logging ExceptionsBy default, all Exceptions other than 404 - Page Not Found
exceptions are logged. This can be turned on and off by setting the <?php namespace Config; use CodeIgniter\Config\BaseConfig; class Exceptions extends BaseConfig { public $log = true; } To ignore logging on other status codes, you can set the status code to ignore in the same file: <?php namespace Config; use CodeIgniter\Config\BaseConfig; class Exceptions extends BaseConfig { public $ignoredCodes = [404]; } Note It is possible that logging still will not happen for exceptions if your current Log settings are not set up to log critical errors, which all exceptions are logged as. Custom ExceptionsThe following custom exceptions are available: PageNotFoundExceptionThis is used to signal a 404, Page Not Found error. When thrown, the system will show the view found at app/Views/errors/html/error_404.php. You should customize all of the error views for your site. If, in app/Config/Routes.php, you have specified a 404 Override, that will be called instead of the standard 404 page: <?php if (! $page = $pageModel->find($id)) { throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound(); } You can pass a message into the exception that will be displayed in place of the default message on the 404 page. ConfigExceptionThis exception should be used when the values from the configuration class are invalid, or when the config class is not the right type, etc: <?php throw new \CodeIgniter\Exceptions\ConfigException(); This provides an HTTP status code of 500 and an exit code of 3. DatabaseExceptionThis exception is thrown for database errors, such as when the database connection cannot be created, or when it is temporarily lost: <?php throw new \CodeIgniter\Database\Exceptions\DatabaseException(); This provides an HTTP status code of 500 and an exit code of 8. RedirectExceptionThis exception is a special case allowing for overriding of all other response routing and forcing a redirect to a specific route or URL: <?php throw new \CodeIgniter\Router\Exceptions\RedirectException($route);
<?php throw new \CodeIgniter\Router\Exceptions\RedirectException($route, 301); |