Skip to content

Commit 55fafa4

Browse files
GuilhemNxabbuh
authored andcommitted
Replace the ViolationFormatter by a new exception
1 parent fc4f688 commit 55fafa4

File tree

3 files changed

+57
-14
lines changed

3 files changed

+57
-14
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSRestBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\RestBundle\Exception;
13+
14+
use FOS\RestBundle\Controller\Annotations\Param;
15+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16+
use Symfony\Component\Validator\ConstraintViolationListInterface;
17+
18+
class InvalidParameterException extends BadRequestHttpException
19+
{
20+
private $parameter;
21+
private $violations;
22+
23+
public function __construct(Param $parameter, ConstraintViolationListInterface $violations)
24+
{
25+
$this->parameter = $parameter;
26+
$this->violations = $violations;
27+
28+
$message = '';
29+
foreach ($violations as $key => $violation) {
30+
if ($key > 0) {
31+
$message .= "\n";
32+
}
33+
$message .= sprintf(
34+
'Parameter "%s" of value "%s" violated a constraint "%s"',
35+
$parameter->name,
36+
$violation->getInvalidValue(),
37+
$violation->getMessage()
38+
);
39+
}
40+
parent::__construct($message);
41+
}
42+
43+
public function getParameter()
44+
{
45+
return $this->parameter;
46+
}
47+
48+
public function getViolations()
49+
{
50+
return $this->violations;
51+
}
52+
}

Request/ParamFetcher.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use FOS\RestBundle\Controller\Annotations\Param;
1616
use FOS\RestBundle\Controller\Annotations\RequestParam;
1717
use FOS\RestBundle\Util\ViolationFormatterInterface;
18+
use FOS\RestBundle\Exception\InvalidParameterException;
1819
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1920
use Symfony\Component\DependencyInjection\ContainerInterface;
2021
use Symfony\Component\HttpFoundation\Request;
@@ -38,11 +39,7 @@ class ParamFetcher implements ParamFetcherInterface, ContainerAwareInterface
3839
private $parameterBag;
3940
private $requestStack;
4041
private $validator;
41-
private $violationFormatter;
42-
/**
43-
* @var callable
44-
*/
45-
private $controller;
42+
4643
/**
4744
* @var ContainerInterface
4845
*
@@ -67,7 +64,6 @@ public function __construct(ParamReader $paramReader, $requestStack, ViolationFo
6764
}
6865

6966
$this->requestStack = $requestStack;
70-
$this->violationFormatter = $violationFormatter;
7167
$this->validator = $validator;
7268

7369
$this->parameterBag = new ParameterBag($paramReader);
@@ -260,12 +256,7 @@ public function cleanParamWithRequirements(Param $config, $param, $strict)
260256

261257
if (0 !== count($errors)) {
262258
if ($strict) {
263-
if (is_array($config->requirements) && isset($config->requirements['error_message'])) {
264-
$errorMessage = $config->requirements['error_message'];
265-
} else {
266-
$errorMessage = $this->violationFormatter->formatList($config, $errors);
267-
}
268-
throw new BadRequestHttpException($errorMessage);
259+
throw new InvalidParameterException($config, $errors);
269260
}
270261

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

Tests/Request/ParamFetcherTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ public function testConstraintThrowExceptionInStrictMode()
592592

593593
$this->setExpectedException(
594594
'\\Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException',
595-
'foobar'
595+
"Parameter \"bizoo\" of value \"\" violated a constraint \"expected message 1\"\nParameter \"bizoo\" of value \"\" violated a constraint \"expected message 2\""
596596
);
597597

598598
$queryFetcher = new ParamFetcher($reader, $requestStack, $this->violationFormatter, $this->validator);
@@ -812,7 +812,7 @@ public function testCustomErrorMessage()
812812
$queryFetcher->get('fero');
813813
$this->fail('Fetching get() in strict mode with no default value did not throw an exception');
814814
} catch (HttpException $httpException) {
815-
$this->assertEquals($errorMessage, $httpException->getMessage());
815+
$this->assertEquals('Parameter "fero" of value "" violated a constraint "variable must be an integer"', $httpException->getMessage());
816816
}
817817
}
818818
}

0 commit comments

Comments
 (0)