Skip to content

Commit d19d78c

Browse files
committed
replaced generic HttpExceptions with more specific subclasses
1 parent 6eccc53 commit d19d78c

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

EventListener/FormatListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1515
use Symfony\Component\HttpKernel\Exception\HttpException;
16+
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
1617
use Symfony\Component\HttpKernel\HttpKernelInterface;
1718

1819
use FOS\RestBundle\Util\Codes;
@@ -65,7 +66,7 @@ public function onKernelRequest(GetResponseEvent $event)
6566

6667
if (null === $format) {
6768
if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
68-
throw new HttpException(Codes::HTTP_NOT_ACCEPTABLE, "No matching accepted Response format could be determined");
69+
throw new NotAcceptableHttpException("No matching accepted Response format could be determined");
6970
}
7071

7172
return;

Request/ParamFetcher.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use FOS\RestBundle\Controller\Annotations\RequestParam;
1818
use Doctrine\Common\Util\ClassUtils;
1919
use Symfony\Component\HttpFoundation\Request;
20+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
2021
use Symfony\Component\HttpKernel\Exception\HttpException;
2122

2223
/**
@@ -113,7 +114,7 @@ public function get($name, $strict = null)
113114

114115
if (null !== $failMessage) {
115116
if ($strict) {
116-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $failMessage);
117+
throw new BadRequestHttpException($failMessage);
117118
}
118119

119120
return $default;
@@ -133,8 +134,9 @@ public function get($name, $strict = null)
133134
$paramType = $config instanceof QueryParam ? 'Query' : 'Request';
134135
$problem = empty($param) ? 'empty' : 'not a scalar';
135136

136-
throw new HttpException(Codes::HTTP_BAD_REQUEST,
137-
sprintf('%s parameter "%s" is %s', $paramType, $name, $problem));
137+
throw new BadRequestHttpException(
138+
sprintf('%s parameter "%s" is %s', $paramType, $name, $problem)
139+
);
138140
}
139141

140142
return $this->cleanParamWithRequirements($config, $param, $strict);
@@ -165,7 +167,7 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
165167
if ($strict) {
166168
$paramType = $config instanceof QueryParam ? 'Query' : 'Request';
167169

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

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

Request/RequestBodyParamConverter.php

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

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1516
use Symfony\Component\HttpKernel\Exception\HttpException;
17+
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
1618
use Symfony\Component\OptionsResolver\OptionsResolver;
1719
use Symfony\Component\Serializer\Exception\Exception as SymfonySerializerException;
1820
use Symfony\Component\Serializer\SerializerInterface as SymfonySerializerInterface;
@@ -108,11 +110,11 @@ public function apply(Request $request, ConfigurationInterface $configuration)
108110
$context
109111
);
110112
} catch (UnsupportedFormatException $e) {
111-
throw new HttpException(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, $e->getMessage());
113+
throw new UnsupportedMediaTypeHttpException($e->getMessage());
112114
} catch (JMSSerializerException $e) {
113-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $e->getMessage());
115+
throw new BadRequestHttpException($e->getMessage());
114116
} catch (SymfonySerializerException $e) {
115-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $e->getMessage());
117+
throw new BadRequestHttpException($e->getMessage());
116118
}
117119

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

View/JsonpHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\Response;
1515
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1617
use Symfony\Component\HttpKernel\Exception\HttpException;
1718

1819
use FOS\RestBundle\Util\Codes;
@@ -39,7 +40,7 @@ protected function getCallback(Request $request)
3940

4041
if ($this->callbackFilter && !preg_match($this->callbackFilter, $callback)) {
4142
$msg = "Callback '$callback' does not match '{$this->callbackFilter}'";
42-
throw new HttpException(Codes::HTTP_BAD_REQUEST, $msg);
43+
throw new BadRequestHttpException($msg);
4344
}
4445

4546
return $callback;

View/ViewHandler.php

Lines changed: 2 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;
@@ -289,7 +289,7 @@ public function handle(View $view, Request $request = null)
289289

290290
if (!$this->supports($format)) {
291291
$msg = "Format '$format' not supported, handler must be implemented";
292-
throw new HttpException(Codes::HTTP_UNSUPPORTED_MEDIA_TYPE, $msg);
292+
throw new UnsupportedMediaTypeHttpException($msg);
293293
}
294294

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

0 commit comments

Comments
 (0)