Added Kohana_Exception class and error view and updated index.php to use exception handling
This commit is contained in:
parent
fddf36e543
commit
bb29b56448
|
@ -75,10 +75,10 @@ require SYSPATH.'classes/kohana'.EXT;
|
|||
spl_autoload_register(array('Kohana', 'auto_load'));
|
||||
|
||||
// Enable the exception handler
|
||||
// set_exception_handler(array('Kohana', 'exception_handler'));
|
||||
set_exception_handler(array('Kohana_Exception', 'handle'));
|
||||
|
||||
// Enable the error-to-exception handler
|
||||
// set_error_handler(array('Kohana', 'error_handler'));
|
||||
set_error_handler(array('Kohana', 'error_handler'));
|
||||
|
||||
// Bootstrap the application
|
||||
require APPPATH.'bootstrap'.EXT;
|
105
system/classes/kohana/exception.php
Normal file
105
system/classes/kohana/exception.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php defined('SYSPATH') or die('No direct access');
|
||||
/**
|
||||
* Kohana exception class. Converts exceptions into HTML messages.
|
||||
*
|
||||
* @package Core
|
||||
* @author Kohana Team
|
||||
* @copyright (c) 2008-2009 Kohana Team
|
||||
* @license http://kohanaphp.com/license.html
|
||||
*/
|
||||
class Kohana_Exception_Core extends Exception {
|
||||
|
||||
/**
|
||||
* Inline exception handler, displays the error message, source of the
|
||||
* exception, and the stack trace of the error.
|
||||
*
|
||||
* @param object exception object
|
||||
* @return void
|
||||
*/
|
||||
public static function handle(Exception $e)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get the exception information
|
||||
$type = get_class($e);
|
||||
$code = $e->getCode();
|
||||
$message = $e->getMessage();
|
||||
$file = $e->getFile();
|
||||
$line = $e->getLine();
|
||||
|
||||
if (Kohana::$is_cli)
|
||||
{
|
||||
// Just display the text of the exception
|
||||
echo "\n", $type, ' [ ', $code ,' ]: ', $message, ' ', $file, ' [ ', $line, ' ] ', "\n";
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Get the exception backtrace
|
||||
$trace = $e->getTrace();
|
||||
|
||||
if ($e instanceof ErrorException AND version_compare(PHP_VERSION, '5.3', '<'))
|
||||
{
|
||||
// Work around for a bug in ErrorException::getTrace() that exists in
|
||||
// all PHP 5.2 versions. @see http://bugs.php.net/bug.php?id=45895
|
||||
for ($i = count($trace) - 1; $i > 0; --$i)
|
||||
{
|
||||
if (isset($trace[$i - 1]['args']))
|
||||
{
|
||||
// Re-position the args
|
||||
$trace[$i]['args'] = $trace[$i - 1]['args'];
|
||||
|
||||
// Remove the args
|
||||
unset($trace[$i - 1]['args']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the source of the error
|
||||
$source = Kohana::debug_source($file, $line);
|
||||
|
||||
// Generate a new error id
|
||||
$error_id = uniqid();
|
||||
|
||||
// Start an output buffer
|
||||
ob_start();
|
||||
|
||||
// Include the exception HTML
|
||||
include Kohana::find_file('views', 'kohana/error');
|
||||
|
||||
// Display the contents of the output buffer
|
||||
echo ob_get_clean();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
// Clean the output buffer if one exists
|
||||
ob_get_level() and ob_clean();
|
||||
|
||||
// This can happen when the kohana error view has a PHP error
|
||||
echo $e->getMessage(), ' [ ', Kohana::debug_path($e->getFile()), ', ', $e->getLine(), ' ]';
|
||||
|
||||
// Exit with an error status
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new translated exception.
|
||||
*
|
||||
* @param string error message
|
||||
* @param array translation variables
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($message, array $variables = NULL)
|
||||
{
|
||||
// Set the message
|
||||
// $message = __($message, $variables);
|
||||
$message = strtr($message, $variables);
|
||||
|
||||
// Pass the message to the parent
|
||||
parent::__construct($message);
|
||||
}
|
||||
|
||||
} // End Kohana_Exception
|
24
system/views/kohana/error.php
Normal file
24
system/views/kohana/error.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
// JS toggling of the error details
|
||||
$toggle_details =
|
||||
|
||||
// Get the error details div for this error
|
||||
'var d=document.getElementById(\'kohana_error_'.$error_id.'_details\');'.
|
||||
|
||||
// Toggle the display value of the div
|
||||
'd.style.display=(d.style.display==\'none\'?\'\':\'none\');'.
|
||||
|
||||
// Do not activate the link
|
||||
'return false;'
|
||||
|
||||
?>
|
||||
<div id="kohana_error_<?php echo $error_id ?>" class="kohana_error" style="display:block;position:relative;z-index:1000;background:#cff292;font-size:1em;font-family:sans-serif;text-align:left">
|
||||
<div class="message" style="display:block;margin:0;padding:1em;color:#111">
|
||||
<p class="text" style="display:block;margin:0;padding:0;color:#111"><a href="#toggle_details" class="toggle_details" style="padding:0color:#39530c;text-decoration:underline;background:transparent" onclick="javascript:<?php echo $toggle_details ?>"><?php echo $type, ' [ ', $code, ' ]' ?></a>: <code style="display:inline;margin:0;padding:0;font-size:1em;background:transparent:color:#111;font-family:sans-serif"><?php echo $message ?></code> <span class="file"><?php echo Kohana::debug_path($file), ' [ ', $line, ' ]' ?></span></p>
|
||||
</div>
|
||||
<div id="kohana_error_<?php echo $error_id ?>_details" class="details" style="display:none;margin:0;padding:0 1em 1em;color:#111">
|
||||
<pre class="source" style="display:block;margin:0;padding:1em;font-family:monospace;background:#efefef;color:#111"><?php echo $source ?></pre>
|
||||
<pre class="trace" style="display:block;margin:0;padding:1em 0 0;font-family:monospace;color:#111"><?php echo implode("\n\n", Kohana::trace($trace)) ?></pre>
|
||||
</div>
|
||||
</div>
|
Loading…
Reference in a new issue