From 874cfaf6bbbadf734507f9eb4194c1d4b8b3ce70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Mon, 9 Oct 2017 15:49:55 +0200 Subject: [PATCH] Provide a better exception message and type Getting "Not found" on a route where you are getting an object can get really confusing. --- features/security/validate_response_types.feature | 4 ++-- src/EventListener/AddFormatListener.php | 2 +- tests/EventListener/AddFormatListenerTest.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/features/security/validate_response_types.feature b/features/security/validate_response_types.feature index 9f5573baa6b..793b3137927 100644 --- a/features/security/validate_response_types.feature +++ b/features/security/validate_response_types.feature @@ -28,11 +28,11 @@ Feature: Validate response types And I send a "GET" request to "/dummies/1.invalid" Then the response status code should be 404 And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8" - And the JSON node "detail" should be equal to "Not Found" + And the JSON node "detail" should be equal to 'Format "invalid" is not supported' Scenario: Requesting an invalid format in the Accept header and in the URL should throw an error When I add "Accept" header equal to "text/invalid" And I send a "GET" request to "/dummies/1.invalid" Then the response status code should be 404 And the header "Content-Type" should be equal to "application/problem+json; charset=utf-8" - And the JSON node "detail" should be equal to "Not Found" + And the JSON node "detail" should be equal to 'Format "invalid" is not supported' diff --git a/src/EventListener/AddFormatListener.php b/src/EventListener/AddFormatListener.php index ee4482ecd8b..24e3683665f 100644 --- a/src/EventListener/AddFormatListener.php +++ b/src/EventListener/AddFormatListener.php @@ -58,7 +58,7 @@ public function onKernelRequest(GetResponseEvent $event) if (null === $routeFormat = $request->attributes->get('_format') ?: null) { $mimeTypes = array_keys($this->mimeTypes); } elseif (!isset($this->formats[$routeFormat])) { - throw new NotFoundHttpException('Not Found'); + throw new NotFoundHttpException(sprintf('Format "%s" is not supported', $routeFormat)); } else { $mimeTypes = Request::getMimeTypes($routeFormat); } diff --git a/tests/EventListener/AddFormatListenerTest.php b/tests/EventListener/AddFormatListenerTest.php index 8f2c1155c03..08fa5d1a19a 100644 --- a/tests/EventListener/AddFormatListenerTest.php +++ b/tests/EventListener/AddFormatListenerTest.php @@ -171,7 +171,7 @@ public function testAcceptHeaderTakePrecedenceOverRequestFormat() /** * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException - * @expectedExceptionMessage Not Found + * @expectedExceptionMessage Format "invalid" is not supported */ public function testInvalidRouteFormat() {