From 8884656ea7ebfec5a7c5b293e1996e88d6954c95 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 7 Jun 2010 21:47:51 +0200 Subject: [PATCH] improved exception & error handling --- backend/index.php | 11 ++++------ backend/init.php | 12 +++-------- backend/lib/model/database.php | 2 +- backend/lib/view/jsonview.php | 18 ++++++++++++++++- backend/lib/view/view.php | 20 ++++++++++++++++--- .../{util/exceptions.php => view/xmlview.php} | 19 ++++-------------- 6 files changed, 46 insertions(+), 36 deletions(-) rename backend/lib/{util/exceptions.php => view/xmlview.php} (76%) diff --git a/backend/index.php b/backend/index.php index 5b9b5d9..1414789 100644 --- a/backend/index.php +++ b/backend/index.php @@ -25,10 +25,7 @@ include 'init.php'; // initialize environment (error handling, configuration, class autoloading) -try { - $fc = new FrontController(); // spawn frontcontroller - $fc->run(); // execute controller and sends output -} catch (Exception $e) { // catch all exceptions - echo $e; -} -?> \ No newline at end of file +$fc = new FrontController(); // spawn frontcontroller +$fc->run(); // execute controller and sends output + +?> diff --git a/backend/init.php b/backend/init.php index d42adc5..c0723fc 100644 --- a/backend/init.php +++ b/backend/init.php @@ -27,12 +27,9 @@ define('VZ_VERSION', '0.1'); function __autoload($className) { $libs = __DIR__ . '/lib/'; - require_once $libs . 'util/exceptions.php'; - // preg_replace pattern class name => inclulde path $mapping = array( // util classes - '/^.*Exception$/' => 'util/exceptions', '/^Registry$/' => 'util/registry', // model classes @@ -52,11 +49,11 @@ function __autoload($className) { $include = $libs . strtolower(preg_replace(array_keys($mapping), array_values($mapping), $className)) . '.php'; if (empty($include)) { - throw new CustomException('Cannot load class ' . $className . '! Name not mapped.'); + throw new Exception('Cannot load class ' . $className . '! Name not mapped.'); } if (!file_exists($include)) { - throw new CustomException('Cannot load class ' . $className . '! File does not exist: ' . $include); + throw new Exception('Cannot load class ' . $className . '! File does not exist: ' . $include); } require_once $include; @@ -65,12 +62,9 @@ function __autoload($className) { // enable strict error reporting error_reporting(E_ALL); -// lets handle all php errors as exceptions -set_error_handler(array('CustomErrorException', 'errorHandler')); - // load configuration into registry if (!file_exists(__DIR__ . '/volkszaehler.conf.php')) { - throw new CustomException('No configuration available! Use volkszaehler.conf.default.php as an template'); + throw new Exception('No configuration available! Use volkszaehler.conf.default.php as an template'); } include __DIR__ . '/volkszaehler.conf.php'; diff --git a/backend/lib/model/database.php b/backend/lib/model/database.php index b234a90..4ca0b16 100644 --- a/backend/lib/model/database.php +++ b/backend/lib/model/database.php @@ -22,7 +22,7 @@ /** * @brief base exception for database queries */ -class DatabaseException extends CustomException {} +class DatabaseException extends Exception {} /** * @brief abstract resultset definition diff --git a/backend/lib/view/jsonview.php b/backend/lib/view/jsonview.php index 7cf9383..d740680 100644 --- a/backend/lib/view/jsonview.php +++ b/backend/lib/view/jsonview.php @@ -30,7 +30,11 @@ class JsonView extends View { $this->source = 'volkszaehler.org'; $this->version = VZ_VERSION; $this->storage = $config['db']['backend']; - //$this->response->headers['Content-type'] = 'application/json'; // TODO uncomment in production use (just for debugging) + $this->controller = $request->get['controller']; + $this->action = $request->get['action']; + + + $this->response->headers['Content-type'] = 'application/json'; } public function __set($key, $value) { @@ -49,6 +53,18 @@ class JsonView extends View { $this->time = round(microtime(true) - $this->created, 4); echo json_encode($this->data); } + + public function exceptionHandler(Exception $exception) { + $this->exception = array('message' => $exception->getMessage(), + 'code' => $exception->getCode(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'trace' => $exception->getTrace() + ); + + echo 'bla'; + $this->render(); + } } ?> \ No newline at end of file diff --git a/backend/lib/view/view.php b/backend/lib/view/view.php index d89f598..4798bfc 100644 --- a/backend/lib/view/view.php +++ b/backend/lib/view/view.php @@ -19,16 +19,30 @@ * http://www.gnu.org/copyleft/gpl.html */ -abstract class View { +interface ViewInterface { + public function __construct(HttpRequest $request, HttpResponse $response); + public function render(); + public function exceptionHandler(Exception $exception); + public function errorHandler($errno, $errstr, $errfile, $errline); +} + +abstract class View implements ViewInterface { public $request; protected $response; - protected $created; // holds timestamp of creation, used later to show time of execution + protected $created; // holds timestamp of creation, used later to return time of execution public function __construct(HttpRequest $request, HttpResponse $response) { $this->request = $request; $this->response = $response; $this->created = microtime(true); + + // error & exception handling by view + set_exception_handler(array($this, 'exceptionHandler')); + set_error_handler(array($this, 'errorHandler'), E_ALL); } - abstract public function render(); + final public function errorHandler($errno, $errstr, $errfile, $errline) { + $this->exceptionHandler(new ErrorException($errstr, 0, $errno, $errfile, $errline)); + die(); + } } \ No newline at end of file diff --git a/backend/lib/util/exceptions.php b/backend/lib/view/xmlview.php similarity index 76% rename from backend/lib/util/exceptions.php rename to backend/lib/view/xmlview.php index 6d816fe..899d314 100644 --- a/backend/lib/util/exceptions.php +++ b/backend/lib/view/xmlview.php @@ -19,7 +19,10 @@ * http://www.gnu.org/copyleft/gpl.html */ -class CustomException extends Exception { +class XmlView extends View { + + // just moved from exception class + // TODO implement exception handling public function toXml(DOMDocument $doc) { $xmlRecord = $doc->createElement('exception'); $xmlRecord->setAttribute('code', $this->code); @@ -32,20 +35,6 @@ class CustomException extends Exception { return $xmlRecord; } - - public function toHtml() { - return $this->message . ' in ' . $this->file . ':' . $this->line; - } - - public function toJson() { // TODO implement - - } -} - -class CustomErrorException extends ErrorException { - static public function errorHandler($errno, $errstr, $errfile, $errline ) { - throw new self($errstr, 0, $errno, $errfile, $errline); - } } ?> \ No newline at end of file