Skip to content

Commit dd965b6

Browse files
committed
Merge pull request #599 from mvdbos/feature/specific-http-exceptions
Replace generic HttpExceptions with more specific subclasses
2 parents 5838b63 + 912ac16 commit dd965b6

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

EventListener/FormatListener.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
namespace FOS\RestBundle\EventListener;
1313

1414
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
15-
use Symfony\Component\HttpKernel\Exception\HttpException;
15+
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
1616
use Symfony\Component\HttpKernel\HttpKernelInterface;
1717

18-
use FOS\RestBundle\Util\Codes;
1918
use FOS\RestBundle\Util\FormatNegotiatorInterface;
2019
use FOS\RestBundle\Util\MediaTypeNegotiatorInterface;
2120

@@ -46,7 +45,7 @@ public function __construct(FormatNegotiatorInterface $formatNegotiator)
4645
*
4746
* @param GetResponseEvent $event The event
4847
*
49-
* @throws HttpException
48+
* @throws NotAcceptableHttpException
5049
*/
5150
public function onKernelRequest(GetResponseEvent $event)
5251
{
@@ -65,7 +64,7 @@ public function onKernelRequest(GetResponseEvent $event)
6564

6665
if (null === $format) {
6766
if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
68-
throw new HttpException(Codes::HTTP_NOT_ACCEPTABLE, "No matching accepted Response format could be determined");
67+
throw new NotAcceptableHttpException("No matching accepted Response format could be determined");
6968
}
7069

7170
return;

Request/ParamFetcher.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111

1212
namespace FOS\RestBundle\Request;
1313

14-
use FOS\RestBundle\Util\Codes;
1514
use FOS\RestBundle\Controller\Annotations\QueryParam;
1615
use FOS\RestBundle\Controller\Annotations\Param;
1716
use FOS\RestBundle\Controller\Annotations\RequestParam;
1817
use Doctrine\Common\Util\ClassUtils;
1918
use Symfony\Component\HttpFoundation\Request;
20-
use Symfony\Component\HttpKernel\Exception\HttpException;
19+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2120

2221
/**
2322
* Helper to validate parameters of the active request.
@@ -113,7 +112,7 @@ public function get($name, $strict = null)
113112

114113
if (null !== $failMessage) {
115114
if ($strict) {
116-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $failMessage);
115+
throw new BadRequestHttpException($failMessage);
117116
}
118117

119118
return $default;
@@ -133,8 +132,9 @@ public function get($name, $strict = null)
133132
$paramType = $config instanceof QueryParam ? 'Query' : 'Request';
134133
$problem = empty($param) ? 'empty' : 'not a scalar';
135134

136-
throw new HttpException(Codes::HTTP_BAD_REQUEST,
137-
sprintf('%s parameter "%s" is %s', $paramType, $name, $problem));
135+
throw new BadRequestHttpException(
136+
sprintf('%s parameter "%s" is %s', $paramType, $name, $problem)
137+
);
138138
}
139139

140140
return $this->cleanParamWithRequirements($config, $param, $strict);
@@ -151,7 +151,7 @@ public function get($name, $strict = null)
151151
* @param string $param param to clean
152152
* @param boolean $strict is strict
153153
*
154-
* @throws \RuntimeException
154+
* @throws BadRequestHttpException
155155
* @return string
156156
*/
157157
public function cleanParamWithRequirements(Param $config, $param, $strict)
@@ -165,7 +165,9 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
165165
if ($strict) {
166166
$paramType = $config instanceof QueryParam ? 'Query' : 'Request';
167167

168-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $paramType . " parameter value '$param', does not match requirements '{$config->requirements}'");
168+
throw new BadRequestHttpException(
169+
$paramType . " parameter value '$param', does not match requirements '{$config->requirements}'"
170+
);
169171
}
170172

171173
return null === $default ? '' : $default;

Request/RequestBodyParamConverter.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace FOS\RestBundle\Request;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15-
use Symfony\Component\HttpKernel\Exception\HttpException;
15+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
1617
use Symfony\Component\OptionsResolver\OptionsResolver;
1718
use Symfony\Component\Serializer\Exception\Exception as SymfonySerializerException;
1819
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializerInterface;
@@ -23,7 +24,6 @@
2324
use JMS\Serializer\Exception\Exception as JMSSerializerException;
2425
use JMS\Serializer\DeserializationContext;
2526
use JMS\Serializer\SerializerInterface;
26-
use FOS\RestBundle\Util\Codes;
2727

2828
/**
2929
* @author Tyler Stroud <[email protected]>
@@ -108,11 +108,11 @@ public function apply(Request $request, ConfigurationInterface $configuration)
108108
$context
109109
);
110110
} catch (UnsupportedFormatException $e) {
111-
throw new HttpException(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, $e->getMessage());
111+
throw new UnsupportedMediaTypeHttpException($e->getMessage());
112112
} catch (JMSSerializerException $e) {
113-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $e->getMessage());
113+
throw new BadRequestHttpException($e->getMessage());
114114
} catch (SymfonySerializerException $e) {
115-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $e->getMessage());
115+
throw new BadRequestHttpException($e->getMessage());
116116
}
117117

118118
$request->attributes->set($configuration->getName(), $object);

View/JsonpHandler.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\HttpFoundation\Request;
16-
use Symfony\Component\HttpKernel\Exception\HttpException;
17-
18-
use FOS\RestBundle\Util\Codes;
16+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1917

2018
/**
2119
* Implements a custom handler for JSONP leveraging the ViewHandler
@@ -39,7 +37,7 @@ protected function getCallback(Request $request)
3937

4038
if ($this->callbackFilter && !preg_match($this->callbackFilter, $callback)) {
4139
$msg = "Callback '$callback' does not match '{$this->callbackFilter}'";
42-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $msg);
40+
throw new BadRequestHttpException($msg);
4341
}
4442

4543
return $callback;

View/ViewHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Component\HttpFoundation\RedirectResponse;
1818
use Symfony\Component\HttpFoundation\Response;
1919
use Symfony\Component\HttpFoundation\Request;
20-
use Symfony\Component\HttpKernel\Exception\HttpException;
20+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
2121
use Symfony\Component\DependencyInjection\ContainerAware;
2222
use Symfony\Component\Form\FormInterface;
2323
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
@@ -278,6 +278,8 @@ protected function getTemplating()
278278
* @param Request $request Request object
279279
*
280280
* @return Response
281+
*
282+
* @throws UnsupportedMediaTypeHttpException
281283
*/
282284
public function handle(View $view, Request $request = null)
283285
{
@@ -289,7 +291,7 @@ public function handle(View $view, Request $request = null)
289291

290292
if (!$this->supports($format)) {
291293
$msg = "Format '$format' not supported, handler must be implemented";
292-
throw new HttpException(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, $msg);
294+
throw new UnsupportedMediaTypeHttpException($msg);
293295
}
294296

295297
if (isset($this->customHandlers[$format])) {

0 commit comments

Comments
 (0)