From 3296a22ff6ab9b47a0d2a2008bca6b62a0d7fa88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simo=20Niemel=C3=A4?= Date: Tue, 10 Nov 2009 20:45:10 +0200 Subject: [PATCH] Wrap apps inside a ShowException middleware --- lib/core/Rack.php | 19 ++++++------------- lib/core/ShowExceptions.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 lib/core/ShowExceptions.php diff --git a/lib/core/Rack.php b/lib/core/Rack.php index 43abfed..5c5e5d9 100644 --- a/lib/core/Rack.php +++ b/lib/core/Rack.php @@ -78,11 +78,9 @@ protected static function get_env() public static function run($app) { - array_push(self::$middleware, $app); $env =& static::get_env(); - ob_start(); - $result = self::runMiddleware($env); + $result = self::runMiddleware($app, $env); $output = ob_get_clean(); if ($output) @@ -110,18 +108,13 @@ private static function execute($result, $env) exit; } - public static function runMiddleware($env) + public static function runMiddleware($inner_app, $env) { - if (empty(self::$middleware)) { - return null; - } - - $middleware = self::$middleware; - $inner_app = array_pop($middleware); - $middleware =& array_reverse($middleware); + self::$middleware = array_reverse(self::$middleware); + self::useMiddleware('core\ShowExceptions'); - if (!empty($middleware)) { - foreach ($middleware as $app) { + if (!empty(self::$middleware)) { + foreach (self::$middleware as $app) { $inner_app = $app($inner_app); } } diff --git a/lib/core/ShowExceptions.php b/lib/core/ShowExceptions.php new file mode 100644 index 0000000..ba63171 --- /dev/null +++ b/lib/core/ShowExceptions.php @@ -0,0 +1,28 @@ +app = $app; + } + + public function __invoke($env) + { + try { + return $this->call($this->app, $env); + } catch (\Exception $ex) { + $body = $this->handleException($env, $ex); + return array(500, array('Content-Type' => 'text/html'), $body); + } + } + + private function handleException($env, $ex) + { + fwrite($env['rack.errors'], $ex->__toString()); + return array($ex->__toString()); + } +}