Skip to content

Commit

Permalink
Merge pull request #12836 from cakephp/issue-12834
Browse files Browse the repository at this point in the history
Fix .json routes not rendering error pages properly
  • Loading branch information
markstory committed Dec 23, 2018
2 parents 0be30d0 + ac2ad08 commit dc63c2f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/Error/ExceptionRenderer.php
Expand Up @@ -125,9 +125,18 @@ protected function _unwrap($exception)
*/
protected function _getController()
{
$request = $this->request ?: Router::getRequest(true);
$request = $this->request;
$routerRequest = Router::getRequest(true);
// Fallback to the request in the router or make a new one from
// $_SERVER
if ($request === null) {
$request = ServerRequestFactory::fromGlobals();
$request = $routerRequest ?: ServerRequestFactory::fromGlobals();
}

// If the current request doesn't have routing data, but we
// found a request in the router context copy the params over
if ($request->getParam('controller') === false && $routerRequest !== null) {
$request = $request->withAttribute('params', $routerRequest->getAttribute('params'));
}

$response = new Response();
Expand Down
36 changes: 35 additions & 1 deletion tests/TestCase/Error/ExceptionRendererTest.php
Expand Up @@ -196,7 +196,9 @@ public function testControllerInstanceForPrefixedRequest()

$exception = new NotFoundException('Page not found');
$request = new ServerRequest();
$request = $request->withParam('prefix', 'admin');
$request = $request
->withParam('controller', 'Articles')
->withParam('prefix', 'admin');

$ExceptionRenderer = new MyCustomExceptionRenderer($exception, $request);

Expand Down Expand Up @@ -905,6 +907,38 @@ public function testRenderWithNoRequest()
$this->assertEquals(500, $result->getStatusCode());
}

/**
* Test that router request parameters are applied when the passed
* request has no params.
*
* @return void
*/
public function testRenderInheritRoutingParams()
{
$routerRequest = new ServerRequest([
'params' => [
'controller' => 'Articles',
'action' => 'index',
'plugin' => null,
'pass' => [],
'_ext' => 'json',
]
]);
// Simulate a request having routing applied and stored in router
Router::pushRequest($routerRequest);

$exceptionRenderer = new ExceptionRenderer(new Exception('Terrible'), new ServerRequest());
$exceptionRenderer->render();
$properties = $exceptionRenderer->__debugInfo();

foreach (['controller', 'action', '_ext'] as $key) {
$this->assertSame(
$routerRequest->getParam($key),
$properties['controller']->request->getParam($key)
);
}
}

/**
* Test that rendering exceptions triggers shutdown events.
*
Expand Down

0 comments on commit dc63c2f

Please sign in to comment.