Commit 4c643ffc authored by catch's avatar catch

Issue #2594777 by dawehner, Wim Leers, damiankloip: Accessing a non existing...

Issue #2594777 by dawehner, Wim Leers, damiankloip: Accessing a non existing format results in a 500
parent ea6f67e2
......@@ -165,6 +165,20 @@ protected function onJson(GetResponseForExceptionEvent $event) {
$event->setResponse($response);
}
/**
* Handles an HttpExceptionInterface exception for unknown formats.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
protected function onFormatUnknown(GetResponseForExceptionEvent $event) {
/** @var \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface|\Exception $exception */
$exception = $event->getException();
$response = new Response($exception->getMessage(), $exception->getStatusCode(), $exception->getHeaders());
$event->setResponse($response);
}
/**
* Handles errors for this subscriber.
*
......@@ -173,11 +187,17 @@ protected function onJson(GetResponseForExceptionEvent $event) {
*/
public function onException(GetResponseForExceptionEvent $event) {
$format = $this->getFormat($event->getRequest());
$exception = $event->getException();
// If it's an unrecognized format, assume HTML.
$method = 'on' . $format;
if (!method_exists($this, $method)) {
$method = 'onHtml';
if ($exception instanceof HttpExceptionInterface) {
$this->onFormatUnknown($event);
}
else {
$this->onHtml($event);
}
return;
}
$this->$method($event);
}
......
<?php
/**
* @file
* Contains \Drupal\Tests\Core\EventSubscriber\DefaultExceptionSubscriberTest.
*/
namespace Drupal\Tests\Core\EventSubscriber;
use Drupal\Core\EventSubscriber\DefaultExceptionSubscriber;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* @coversDefaultClass \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber
* @group EventSubscriber
*/
class DefaultExceptionSubscriberTest extends UnitTestCase {
/**
* @covers ::onException
* @covers ::onFormatUnknown
*/
public function testOnExceptionWithUnknownFormat() {
$config_factory = $this->getConfigFactoryStub();
// Format 'bananas' requested, yet only 'json' allowed.
$kernel = $this->prophesize(HttpKernelInterface::class);
$request = Request::create('/test?_format=bananas');
$e = new MethodNotAllowedHttpException(['json'], 'test message');
$event = new GetResponseForExceptionEvent($kernel->reveal(), $request, 'GET', $e);
$subscriber = new DefaultExceptionSubscriber($config_factory);
$subscriber->onException($event);
$response = $event->getResponse();
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals('test message', $response->getContent());
$this->assertEquals(405, $response->getStatusCode());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment