From bb29b56448f3ddf7089ab99e8274635048a03525 Mon Sep 17 00:00:00 2001 From: Woody Gilk Date: Sat, 28 Feb 2009 11:34:32 +0000 Subject: [PATCH] Added Kohana_Exception class and error view and updated index.php to use exception handling --- index.php | 4 +- system/classes/kohana/exception.php | 105 ++++++++++++++++++++++++++++ system/views/kohana/error.php | 24 +++++++ 3 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 system/classes/kohana/exception.php create mode 100644 system/views/kohana/error.php diff --git a/index.php b/index.php index 5dfe38a..d5429d2 100644 --- a/index.php +++ b/index.php @@ -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; \ No newline at end of file diff --git a/system/classes/kohana/exception.php b/system/classes/kohana/exception.php new file mode 100644 index 0000000..ad105cb --- /dev/null +++ b/system/classes/kohana/exception.php @@ -0,0 +1,105 @@ +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 diff --git a/system/views/kohana/error.php b/system/views/kohana/error.php new file mode 100644 index 0000000..bc89c26 --- /dev/null +++ b/system/views/kohana/error.php @@ -0,0 +1,24 @@ + +
+
+

:

+
+ +
\ No newline at end of file