Skip to content

Commit

Permalink
Wrap apps inside a ShowException middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sniemela committed Nov 10, 2009
1 parent 3ee0de7 commit 3296a22
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
19 changes: 6 additions & 13 deletions lib/core/Rack.php
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
}
Expand Down
28 changes: 28 additions & 0 deletions lib/core/ShowExceptions.php
@@ -0,0 +1,28 @@
<?php
namespace core;

class ShowExceptions extends App
{
protected $app;

public function __construct($app)
{
$this->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());
}
}

0 comments on commit 3296a22

Please sign in to comment.