Skip to content

Instantly share code, notes, and snippets.

@isaiahdw
Created September 8, 2011 18:48
Show Gist options
  • Select an option

  • Save isaiahdw/1204271 to your computer and use it in GitHub Desktop.

Select an option

Save isaiahdw/1204271 to your computer and use it in GitHub Desktop.
<?php defined('SYSPATH') or die('No direct script access.');
class Kohana_Exception extends Kohana_Kohana_Exception
{
public static function handler(Exception $e)
{
$show_exceptions = array(
Kohana::DEVELOPMENT,
Kohana::QA,
);
if (in_array(Kohana::$environment, $show_exceptions))
{
parent::handler($e);
}
else
{
try
{
Kohana::$log->add(Log::ERROR, self::text($e));
if ($e instanceof HTTP_Exception)
{
$attributes['action'] = $e->getCode();
}
else
{
$attributes['action'] = 500;
}
// Error sub-request.
echo Request::factory(Route::get('error')->uri($attributes))
->execute()
->send_headers()
->body();
}
catch (Exception $e)
{
// If there is a problem showing the error just send plain text instead
ob_get_level() and ob_clean();
// Display the exception text
echo 'Unhandled Exception';
}
}
// Exit with an error status
exit(1);
}
/**
* Get a text respentation of the exception message including a brief backtrace
*
* Error [ Code ]: Message ~ File [ Line ]
*
* @param object Exception
* @return string
*/
public static function text(Exception $e)
{
try
{
$trace = '';
$backtrace = $e->getTrace();
foreach ($backtrace as $item)
{
$file = isset($item['file']) ? $item['file'] : '{PHP internal call}';
$line = isset($item['line']) ? $item['line'] : '';
$class = isset($item['class']) ? $item['class'] : '';
$function = isset($item['function']) ? $item['function'] : '';
$trace .= sprintf("\n\t%s [%d] - %s::%s", $file, $line, $class, $function);
}
return sprintf('%s [ %s ]: %s ~ %s [ %d ] %s',
get_class($e), $e->getCode(), strip_tags($e->getMessage()), Debug::path($e->getFile()), $e->getLine(), $trace);
}
catch (Exception $e)
{
return $e->getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment