improved exception & error handling

This commit is contained in:
Steffen Vogel 2010-06-07 21:47:51 +02:00
parent b2331e7d6c
commit 8884656ea7
6 changed files with 46 additions and 36 deletions

View file

@ -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;
}
?>
$fc = new FrontController(); // spawn frontcontroller
$fc->run(); // execute controller and sends output
?>

View file

@ -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';

View file

@ -22,7 +22,7 @@
/**
* @brief base exception for database queries
*/
class DatabaseException extends CustomException {}
class DatabaseException extends Exception {}
/**
* @brief abstract resultset definition

View file

@ -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();
}
}
?>

View file

@ -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();
}
}

View file

@ -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);
}
}
?>