Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa30d04

Browse files
committed
[Serializer] throw more specific exceptions
1 parent 3c262ba commit aa30d04

10 files changed

+77
-31
lines changed

src/Symfony/Component/Serializer/Encoder/JsonDecode.php

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

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
14+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1515

1616
/**
1717
* Decodes JSON data.
@@ -73,7 +73,7 @@ public function __construct($associative = false, $depth = 512)
7373
*
7474
* @return mixed
7575
*
76-
* @throws UnexpectedValueException
76+
* @throws NotEncodableValueException
7777
*
7878
* @see http://php.net/json_decode json_decode
7979
*/
@@ -88,7 +88,7 @@ public function decode($data, $format, array $context = array())
8888
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
8989

9090
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
91-
throw new UnexpectedValueException(json_last_error_msg());
91+
throw new NotEncodableValueException(json_last_error_msg());
9292
}
9393

9494
return $decodedData;

src/Symfony/Component/Serializer/Encoder/JsonEncode.php

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

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
14+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1515

1616
/**
1717
* Encodes JSON data.
@@ -40,7 +40,7 @@ public function encode($data, $format, array $context = array())
4040
$encodedJson = json_encode($data, $context['json_encode_options']);
4141

4242
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) {
43-
throw new UnexpectedValueException(json_last_error_msg());
43+
throw new NotEncodableValueException(json_last_error_msg());
4444
}
4545

4646
return $encodedJson;

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

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

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
14+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1515

1616
/**
1717
* Encodes XML data.
@@ -78,7 +78,7 @@ public function encode($data, $format, array $context = array())
7878
public function decode($data, $format, array $context = array())
7979
{
8080
if ('' === trim($data)) {
81-
throw new UnexpectedValueException('Invalid XML data, it can not be empty.');
81+
throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
8282
}
8383

8484
$internalErrors = libxml_use_internal_errors(true);
@@ -94,13 +94,13 @@ public function decode($data, $format, array $context = array())
9494
if ($error = libxml_get_last_error()) {
9595
libxml_clear_errors();
9696

97-
throw new UnexpectedValueException($error->message);
97+
throw new NotEncodableValueException($error->message);
9898
}
9999

100100
$rootNode = null;
101101
foreach ($dom->childNodes as $child) {
102102
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
103-
throw new UnexpectedValueException('Document types are not allowed.');
103+
throw new NotEncodableValueException('Document types are not allowed.');
104104
}
105105
if (!$rootNode && XML_PI_NODE !== $child->nodeType) {
106106
$rootNode = $child;
@@ -380,7 +380,7 @@ private function parseXmlValue(\DOMNode $node, array $context = array())
380380
*
381381
* @return bool
382382
*
383-
* @throws UnexpectedValueException
383+
* @throws NotEncodableValueException
384384
*/
385385
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
386386
{
@@ -437,7 +437,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
437437
return $this->appendNode($parentNode, $data, 'data');
438438
}
439439

440-
throw new UnexpectedValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
440+
throw new NotEncodableValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
441441
}
442442

443443
/**
@@ -485,7 +485,7 @@ private function needsCdataWrapping($val)
485485
*
486486
* @return bool
487487
*
488-
* @throws UnexpectedValueException
488+
* @throws NotEncodableValueException
489489
*/
490490
private function selectNodeType(\DOMNode $node, $val)
491491
{
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Serializer\Exception;
13+
14+
/**
15+
* @author Christian Flothmann <[email protected]>
16+
*/
17+
class NotEncodableValueException extends UnexpectedValueException
18+
{
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 Symfony\Component\Serializer\Exception;
13+
14+
/**
15+
* @author Christian Flothmann <[email protected]>
16+
*/
17+
class NotNormalizableValueException extends UnexpectedValueException
18+
{
19+
}

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Serializer\Encoder\JsonEncoder;
1616
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
1717
use Symfony\Component\Serializer\Exception\LogicException;
18-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
18+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1919
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
2020
use Symfony\Component\PropertyInfo\Type;
2121
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
@@ -200,7 +200,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
200200
try {
201201
$this->setAttributeValue($object, $attribute, $value, $format, $context);
202202
} catch (InvalidArgumentException $e) {
203-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
203+
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
204204
}
205205
}
206206

@@ -233,7 +233,7 @@ abstract protected function setAttributeValue($object, $attribute, $value, $form
233233
*
234234
* @return mixed
235235
*
236-
* @throws UnexpectedValueException
236+
* @throws NotNormalizableValueException
237237
* @throws LogicException
238238
*/
239239
private function validateAndDenormalize($currentClass, $attribute, $data, $format, array $context)
@@ -292,7 +292,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
292292
return $data;
293293
}
294294

295-
throw new UnexpectedValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data)));
295+
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), gettype($data)));
296296
}
297297

298298
/**

src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

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

1414
use Symfony\Component\Serializer\Exception\BadMethodCallException;
1515
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
16+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1717
use Symfony\Component\Serializer\SerializerAwareInterface;
1818
use Symfony\Component\Serializer\SerializerInterface;
1919

@@ -33,6 +33,8 @@ class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterfa
3333

3434
/**
3535
* {@inheritdoc}
36+
*
37+
* @throws NotNormalizableValueException
3638
*/
3739
public function denormalize($data, $class, $format = null, array $context = array())
3840
{
@@ -52,7 +54,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
5254
$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
5355
foreach ($data as $key => $value) {
5456
if (null !== $builtinType && !call_user_func('is_'.$builtinType, $key)) {
55-
throw new UnexpectedValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, gettype($key)));
57+
throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, gettype($key)));
5658
}
5759

5860
$data[$key] = $serializer->denormalize($value, $class, $format, $context);

src/Symfony/Component/Serializer/Normalizer/DataUriNormalizer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
1616
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
1717
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
18-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
18+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1919

2020
/**
2121
* Normalizes an {@see \SplFileInfo} object to a data URI.
@@ -85,11 +85,14 @@ public function supportsNormalization($data, $format = null)
8585
* Regex adapted from Brian Grinstead code.
8686
*
8787
* @see https://gist.github.com/bgrins/6194623
88+
*
89+
* @throws InvalidArgumentException
90+
* @throws NotNormalizableValueException
8891
*/
8992
public function denormalize($data, $class, $format = null, array $context = array())
9093
{
9194
if (!preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i', $data)) {
92-
throw new UnexpectedValueException('The provided "data:" URI is not valid.');
95+
throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
9396
}
9497

9598
try {
@@ -102,7 +105,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
102105
return new \SplFileObject($data);
103106
}
104107
} catch (\RuntimeException $exception) {
105-
throw new UnexpectedValueException($exception->getMessage(), $exception->getCode(), $exception);
108+
throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
106109
}
107110

108111
throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $class));

src/Symfony/Component/Serializer/Normalizer/DateTimeNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
15+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1616

1717
/**
1818
* Normalizes an object implementing the {@see \DateTimeInterface} to a date string.
@@ -79,7 +79,7 @@ public function supportsNormalization($data, $format = null)
7979
/**
8080
* {@inheritdoc}
8181
*
82-
* @throws UnexpectedValueException
82+
* @throws NotNormalizableValueException
8383
*/
8484
public function denormalize($data, $class, $format = null, array $context = array())
8585
{
@@ -100,7 +100,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
100100

101101
$dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();
102102

103-
throw new UnexpectedValueException(sprintf(
103+
throw new NotNormalizableValueException(sprintf(
104104
'Parsing datetime string "%s" using format "%s" resulted in %d errors:'."\n".'%s',
105105
$data,
106106
$dateTimeFormat,
@@ -112,7 +112,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
112112
try {
113113
return \DateTime::class === $class ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
114114
} catch (\Exception $e) {
115-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
115+
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
116116
}
117117
}
118118

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
use Symfony\Component\Serializer\Encoder\ChainEncoder;
1616
use Symfony\Component\Serializer\Encoder\EncoderInterface;
1717
use Symfony\Component\Serializer\Encoder\DecoderInterface;
18+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
19+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1820
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
1921
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
2022
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
2123
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2224
use Symfony\Component\Serializer\Exception\LogicException;
23-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
2425

2526
/**
2627
* Serializer serializes and deserializes data.
@@ -108,7 +109,7 @@ public function __construct(array $normalizers = array(), array $encoders = arra
108109
final public function serialize($data, $format, array $context = array())
109110
{
110111
if (!$this->supportsEncoding($format)) {
111-
throw new UnexpectedValueException(sprintf('Serialization for the format %s is not supported', $format));
112+
throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported', $format));
112113
}
113114

114115
if ($this->encoder->needsNormalization($format)) {
@@ -124,7 +125,7 @@ final public function serialize($data, $format, array $context = array())
124125
final public function deserialize($data, $type, $format, array $context = array())
125126
{
126127
if (!$this->supportsDecoding($format)) {
127-
throw new UnexpectedValueException(sprintf('Deserialization for the format %s is not supported', $format));
128+
throw new NotEncodableValueException(sprintf('Deserialization for the format %s is not supported', $format));
128129
}
129130

130131
$data = $this->decode($data, $format, $context);
@@ -160,14 +161,16 @@ public function normalize($data, $format = null, array $context = array())
160161
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
161162
}
162163

163-
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
164+
throw new NotNormalizableValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
164165
}
165166

166-
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
167+
throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
167168
}
168169

169170
/**
170171
* {@inheritdoc}
172+
*
173+
* @throws NotNormalizableValueException
171174
*/
172175
public function denormalize($data, $type, $format = null, array $context = array())
173176
{
@@ -179,7 +182,7 @@ public function denormalize($data, $type, $format = null, array $context = array
179182
return $normalizer->denormalize($data, $type, $format, $context);
180183
}
181184

182-
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $type));
185+
throw new NotNormalizableValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $type));
183186
}
184187

185188
/**

0 commit comments

Comments
 (0)