Error/Exception Handling

Kohana provides both an exception handler and an error handler that transforms errors into exceptions using PHP's ErrorException class. Many details of the error and the internal state of the application is displayed by the handler:

  1. Exception class
  2. Error level
  3. Error message
  4. Source of the error, with the error line highlighted
  5. A debug backtrace of the execution flow
  6. Included files, loaded extensions, and global variables

Example

Click any of the links to toggle the display of additional information:

ErrorException [ Notice ]: Undefined variable: var_does_not_exist

MODPATH/userguide/views/userguide/examples/error.php [ 4 ]

1 <?php
2 
3 // Should trigger an ErrorException with an E_NOTICE level
4 echo $var_does_not_exist;
5 
6 ?>
  1. MODPATH/userguide/views/userguide/examples/error.php [ 4 ] » Kohana_Core::error_handler(arguments)

  2. SYSPATH/classes/Kohana/View.php [ 61 ] » include(arguments)

  3. SYSPATH/classes/Kohana/View.php [ 348 ] » Kohana_View::capture(arguments)

  4. MODPATH/userguide/classes/Kohana/Kodoc/Markdown.php [ 179 ] » Kohana_View->render()

  5. MODPATH/userguide/vendor/markdown/markdown.php [ 330 ] » Kohana_Kodoc_Markdown->doIncludeViews(arguments)

  6. MODPATH/userguide/classes/Kohana/Kodoc/Markdown.php [ 58 ] » Markdown_Parser->transform(arguments)

  7. MODPATH/userguide/classes/Kohana/Controller/Userguide.php [ 148 ] » Kohana_Kodoc_Markdown::markdown(arguments)

  8. SYSPATH/classes/Kohana/Controller.php [ 84 ] » Kohana_Controller_Userguide->action_docs()

  9. {PHP internal call} » Kohana_Controller->execute()

  10. SYSPATH/classes/Kohana/Request/Client/Internal.php [ 97 ] » ReflectionMethod->invoke(arguments)

  11. SYSPATH/classes/Kohana/Request/Client.php [ 114 ] » Kohana_Request_Client_Internal->execute_request(arguments)

  12. SYSPATH/classes/Kohana/Request.php [ 986 ] » Kohana_Request_Client->execute(arguments)

  13. DOCROOT/3.3-index.php [ 103 ] » Kohana_Request->execute()

Environment

Disabling Error/Exception Handling

If you do not want to use the internal error handling, you can disable it (highly discouraged) when calling Kohana::init:

Kohana::init(array('errors' => FALSE));

Error Reporting

By default, Kohana displays all errors, including strict mode warnings. This is set using error_reporting:

error_reporting(E_ALL | E_STRICT);

When you application is live and in production, a more conservative setting is recommended, such as ignoring notices:

error_reporting(E_ALL & ~E_NOTICE);

If you get a white screen when an error is triggered, your host probably has disabled displaying errors. You can turn it on again by adding this line just after your error_reporting call:

ini_set('display_errors', TRUE);

Errors should always be displayed, even in production, because it allows you to use exception and error handling to serve a nice error page rather than a blank white screen when an error happens.

HTTP Exception Handling

Kohana comes with a robust system for handing http errors. It includes exception classes for each http status code. To trigger a 404 in your application (the most common scenario):

throw HTTP_Exception::factory(404, 'File not found!');

To register error pages for these, using 404 as an example:

class HTTP_Exception_404 extends Kohana_HTTP_Exception_404 {

    public function get_response()
    {
        $response = Response::factory();

        $view = View::factory('errors/404');

        // We're inside an instance of Exception here, all the normal stuff is available.
        $view->message = $this->getMessage();

        $response->body($view->render());

        return $response;
    }

}