From f33eee70fcdd9e0a41da01c1a6742ffde8b68d38 Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Fri, 10 Jun 2022 23:17:15 +0200 Subject: [PATCH] Catch a more precise exception in Swagger controller (#2005) --- Controller/SwaggerUiController.php | 4 ++-- Exception/RenderInvalidArgumentException.php | 16 ++++++++++++++++ Render/RenderOpenApi.php | 6 +++--- 3 files changed, 21 insertions(+), 5 deletions(-) create mode 100644 Exception/RenderInvalidArgumentException.php diff --git a/Controller/SwaggerUiController.php b/Controller/SwaggerUiController.php index 5bacbbd..fc2ca27 100644 --- a/Controller/SwaggerUiController.php +++ b/Controller/SwaggerUiController.php @@ -11,7 +11,7 @@ namespace Nelmio\ApiDocBundle\Controller; -use InvalidArgumentException; +use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException; use Nelmio\ApiDocBundle\Render\Html\AssetsMode; use Nelmio\ApiDocBundle\Render\RenderOpenApi; use Symfony\Component\HttpFoundation\Request; @@ -42,7 +42,7 @@ final class SwaggerUiController ); return $response->setCharset('UTF-8'); - } catch (InvalidArgumentException $e) { + } catch (RenderInvalidArgumentException $e) { $advice = ''; if (false !== strpos($area, '.json')) { $advice = ' Since the area provided contains `.json`, the issue is likely caused by route priorities. Try switching the Swagger UI / the json documentation routes order.'; diff --git a/Exception/RenderInvalidArgumentException.php b/Exception/RenderInvalidArgumentException.php new file mode 100644 index 0000000..ffb0b00 --- /dev/null +++ b/Exception/RenderInvalidArgumentException.php @@ -0,0 +1,16 @@ +generatorLocator->has($area)) { - throw new InvalidArgumentException(sprintf('Area "%s" is not supported.', $area)); + throw new RenderInvalidArgumentException(sprintf('Area "%s" is not supported.', $area)); } elseif (!array_key_exists($format, $this->openApiRenderers)) { - throw new InvalidArgumentException(sprintf('Format "%s" is not supported.', $format)); + throw new RenderInvalidArgumentException(sprintf('Format "%s" is not supported.', $format)); } /** @var OpenApi $spec */