Skip to content

Commit

Permalink
Get rid of the exception stack in route handling. First exception wil…
Browse files Browse the repository at this point in the history
…l kill the request
  • Loading branch information
bergie committed Oct 8, 2010
1 parent 6b3f96e commit 2e3b7a3
Showing 1 changed file with 3 additions and 52 deletions.
55 changes: 3 additions & 52 deletions services/dispatcher/midgard3.php
Expand Up @@ -152,67 +152,18 @@ public function dispatch(midgardmvc_core_request $request)
$matched_routes[$route->id] = $matches;
}
}
//$matched_routes = $this->get_route_matches($request, $route_definitions);

if (!$matched_routes)
{
// TODO: Check message
throw new midgardmvc_exception_notfound('No route matches current URL ' . $request->get_path());
}
//unset($route_id_map);

$matched_routes = array_reverse($matched_routes);
$success_flag = true; // Flag to tell if route ran successfully
foreach ($matched_routes as $route_id => $arguments)
{
try
{
$success_flag = true; // before trying route it's marked success
$request->set_route($routes[$route_id]);
$this->dispatch_route($request, $arguments);
}
catch (Exception $e)
{
if ( $e instanceof midgardmvc_exception_unauthorized
|| $e instanceof StartNewRequestException)
{
// ACL and App Server exceptions override anything else
throw $e;
}
$this->exceptions_stack[] = $e; // Adding exception to exceptions stack
$success_flag = false; // route failed
}
if ($success_flag) // Checking for success
{
break; // if we get here, controller run succesfully so bailing out from the loop
}
} // ending foreach

if (!$success_flag)
{
// if foreach is over and success flag is false throwing exeption
$messages = '';
foreach ($this->exceptions_stack as $exception)
{
switch (get_class($exception))
{
case 'midgardmvc_exception_unauthorized':
throw $exception;
// This will exit
case 'midgardmvc_exception_httperror':
if ( $exception->getCode() != 405
|| count($this->exceptions_stack) == 1)
{
// Throw the HTTP error as-is, except if it is a "Method not allowed" that isn't the only error
throw $exception;
// This will exit
}
default:
$messages .= $exception->getMessage() . "\n";
break;
}
}
// 404 MultiFail
throw new midgardmvc_exception_notfound($messages);
$request->set_route($routes[$route_id]);
$this->dispatch_route($request, $arguments);
}
}

Expand Down

0 comments on commit 2e3b7a3

Please sign in to comment.