Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Ensure routing failure leads to early request completion
Browse files Browse the repository at this point in the history
- Do not clone the event when triggering dispatch.error after routing failure
  • Loading branch information
weierophinney committed Feb 14, 2012
1 parent e0176af commit d211e5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 4 additions & 6 deletions library/Zend/Mvc/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,9 @@ public function route(MvcEvent $e)
$routeMatch = $router->match($request);

if (!$routeMatch instanceof Router\RouteMatch) {
$error = clone $e;
$error->setError(static::ERROR_CONTROLLER_NOT_FOUND);
$e->setError(static::ERROR_CONTROLLER_NOT_FOUND);

$results = $this->events()->trigger('dispatch.error', $error);
$results = $this->events()->trigger('dispatch.error', $e);
if (count($results)) {
$return = $results->last();
} else {
Expand Down Expand Up @@ -319,10 +318,9 @@ public function dispatch(MvcEvent $e)
);
}

$events = $this->events();
$routeMatch = $e->getRouteMatch();

$routeMatch = $e->getRouteMatch();
$controllerName = $routeMatch->getParam('controller', 'not-found');
$events = $this->events();

try {
$controller = $locator->get($controllerName);
Expand Down
22 changes: 22 additions & 0 deletions tests/Zend/Mvc/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,26 @@ public function testLocatorExceptionShouldTriggerDispatchError()
$result = $app->run();
$this->assertSame($response, $result);
}

/**
* @group error-handling
*/
public function testFailureForRouteToReturnRouteMatchShouldPopulateEventError()
{
$app = $this->setupBadController();
$router = new Router\SimpleRouteStack();
$app->setRouter($router);

$response = $app->getResponse();
$events = $app->events();
$events->attach('dispatch.error', function ($e) use ($response) {
$error = $e->getError();
$response->setContent("Code: " . $error);
return $response;
});

$app->run();
$event = $app->getMvcEvent();
$this->assertEquals(Application::ERROR_CONTROLLER_NOT_FOUND, $event->getError());
}
}

0 comments on commit d211e5e

Please sign in to comment.