Catch a more precise exception in Swagger controller (#2005)

This commit is contained in:
Guilhem Niot 2022-06-10 23:17:15 +02:00 committed by GitHub
parent 235963df41
commit f33eee70fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions

View File

@ -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.';

View File

@ -0,0 +1,16 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Exception;
class RenderInvalidArgumentException extends \InvalidArgumentException
{
}

View File

@ -11,7 +11,7 @@
namespace Nelmio\ApiDocBundle\Render;
use InvalidArgumentException;
use Nelmio\ApiDocBundle\Exception\RenderInvalidArgumentException;
use OpenApi\Annotations\OpenApi;
use OpenApi\Annotations\Server;
use Psr\Container\ContainerInterface;
@ -65,9 +65,9 @@ class RenderOpenApi
public function render(string $format, string $area, array $options = []): string
{
if (!$this->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 */