Skip to content

Instantly share code, notes, and snippets.

@hkdobrev
Created January 11, 2012 23:42
Show Gist options
  • Save hkdobrev/1597503 to your computer and use it in GitHub Desktop.
Save hkdobrev/1597503 to your computer and use it in GitHub Desktop.
Custom Kohana_Exception class for handling exceptions in production.
<?php defined('SYSPATH') OR die('No direct access allowed!');
class Controller_Error extends Controller_Layout {
public function before()
{
parent::before();
$this->response->status((int) $this->request->action());
$this->response->body(View::factory('erros/default', array(
'page' => URL::site(rawurldecode(Request::$initial->uri())),
'message' => base64_decode($this->request->param('message'))
));
}
}
<?php defined('SYSPATH') OR die('No direct script access.');
/**
* Custom exception class. If not in development environment shows a nice message with the error controller.
* Logs all of the error information in the logs.
*
* @category Exceptions
* @author Haralan Dobrev
*/
class Kohana_Exception extends Kohana_Kohana_Exception {
public static function handler(Exception $e)
{
if (Kohana::DEVELOPMENT === Kohana::$environment)
{
// Shows the default red kohana error page with trace if we are in development
parent::handler($e);
}
else
{
// Create a text version of the exception
$error = Kohana_Exception::text($e);
// Add this exception to the log
Kohana::$log->add(Log::ERROR, $error);
// Get the whole trace as a string
$strace = Kohana_Exception::text($e) . "\n--\n" . $e->getTraceAsString();
// Log that trace string with Log::STRACE level
Kohana::$log->add(Log::STRACE, $strace);
try
{
$attributes = array
(
// Every response code could have an action in the error controller
// Use the 500 HTTP response code by default
'action' => 500,
// Don't show even the message if it's an internal error
'message' => 'Something went wrong'
);
// Sets the action to the appropriate response code if it's an HTTP_Exception
if ($e instanceof HTTP_Exception)
{
// Use the response code from the exception of it's an HTTP_Exception
$attributes['action'] = $e->getCode();
// Get the message from the exception
$attributes['message'] = $e->getMessage();
}
// Encodes the message so there are no problems matching the route
$attributes['message'] = base64_encode($attributes['message']);
// Error sub-request using the error controller
echo Request::factory(Route::get('error')->uri($attributes))
->execute()
->send_headers()
->body();
}
catch (Exception $e)
{
// Clean the output buffer if one exists
ob_get_level() and ob_clean();
// Display the exception text
echo $error;
// Exit with an error status
exit(1);
}
}
}
}
@hkdobrev
Copy link
Author

Place the exception class in APPPATH/classes/kohana/exception.php

@hkdobrev
Copy link
Author

Add a route named 'error' with a message parameter. Like this:

Route::set('error', 'error/<action>(/<message>)')
    ->defaults(array(
        'controller' => 'error',
        'message' => ''
    ));

@hkdobrev
Copy link
Author

Add actions to the error controller with names of response codes like 403, 404, 500 and others.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment