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

Skip to content

Commit 991c02e

Browse files
committed
[Serializer] throw more specific exceptions
1 parent 10c9d19 commit 991c02e

10 files changed

+73
-34
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.
@@ -76,7 +76,7 @@ public function encode($data, $format, array $context = array())
7676
public function decode($data, $format, array $context = array())
7777
{
7878
if ('' === trim($data)) {
79-
throw new UnexpectedValueException('Invalid XML data, it can not be empty.');
79+
throw new NotEncodableValueException('Invalid XML data, it can not be empty.');
8080
}
8181

8282
$internalErrors = libxml_use_internal_errors(true);
@@ -92,12 +92,12 @@ public function decode($data, $format, array $context = array())
9292
if ($error = libxml_get_last_error()) {
9393
libxml_clear_errors();
9494

95-
throw new UnexpectedValueException($error->message);
95+
throw new NotEncodableValueException($error->message);
9696
}
9797

9898
foreach ($dom->childNodes as $child) {
9999
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
100-
throw new UnexpectedValueException('Document types are not allowed.');
100+
throw new NotEncodableValueException('Document types are not allowed.');
101101
}
102102
}
103103

@@ -363,7 +363,7 @@ private function parseXmlValue(\DOMNode $node)
363363
*
364364
* @return bool
365365
*
366-
* @throws UnexpectedValueException
366+
* @throws NotEncodableValueException
367367
*/
368368
private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
369369
{
@@ -417,7 +417,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
417417
return $this->appendNode($parentNode, $data, 'data');
418418
}
419419

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

423423
/**
@@ -465,7 +465,7 @@ private function needsCdataWrapping($val)
465465
*
466466
* @return bool
467467
*
468-
* @throws UnexpectedValueException
468+
* @throws NotEncodableValueException
469469
*/
470470
private function selectNodeType(\DOMNode $node, $val)
471471
{
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
@@ -16,7 +16,7 @@
1616
use Symfony\Component\Serializer\Exception\CircularReferenceException;
1717
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
1818
use Symfony\Component\Serializer\Exception\LogicException;
19-
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
19+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2020
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
2121
use Symfony\Component\PropertyInfo\Type;
2222
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)
@@ -287,7 +287,7 @@ private function validateAndDenormalize($currentClass, $attribute, $data, $forma
287287
}
288288
}
289289

290-
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)));
290+
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)));
291291
}
292292

293293
/**

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

Lines changed: 3 additions & 3 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

@@ -32,7 +32,7 @@ class ArrayDenormalizer implements DenormalizerInterface, SerializerAwareInterfa
3232
/**
3333
* {@inheritdoc}
3434
*
35-
* @throws UnexpectedValueException
35+
* @throws NotNormalizableValueException
3636
*/
3737
public function denormalize($data, $class, $format = null, array $context = array())
3838
{
@@ -52,7 +52,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
5252
$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
5353
foreach ($data as $key => $value) {
5454
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)));
55+
throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, gettype($key)));
5656
}
5757

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

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

Lines changed: 4 additions & 4 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.
@@ -81,12 +81,12 @@ public function supportsNormalization($data, $format = null)
8181
* @see https://gist.github.com/bgrins/6194623
8282
*
8383
* @throws InvalidArgumentException
84-
* @throws UnexpectedValueException
84+
* @throws NotNormalizableValueException
8585
*/
8686
public function denormalize($data, $class, $format = null, array $context = array())
8787
{
8888
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)) {
89-
throw new UnexpectedValueException('The provided "data:" URI is not valid.');
89+
throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
9090
}
9191

9292
try {
@@ -99,7 +99,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
9999
return new \SplFileObject($data);
100100
}
101101
} catch (\RuntimeException $exception) {
102-
throw new UnexpectedValueException($exception->getMessage(), $exception->getCode(), $exception);
102+
throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
103103
}
104104

105105
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.
@@ -64,7 +64,7 @@ public function supportsNormalization($data, $format = null)
6464
/**
6565
* {@inheritdoc}
6666
*
67-
* @throws UnexpectedValueException
67+
* @throws NotNormalizableValueException
6868
*/
6969
public function denormalize($data, $class, $format = null, array $context = array())
7070
{
@@ -79,7 +79,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
7979

8080
$dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();
8181

82-
throw new UnexpectedValueException(sprintf(
82+
throw new NotNormalizableValueException(sprintf(
8383
'Parsing datetime string "%s" using format "%s" resulted in %d errors:'."\n".'%s',
8484
$data,
8585
$dateTimeFormat,
@@ -91,7 +91,7 @@ public function denormalize($data, $class, $format = null, array $context = arra
9191
try {
9292
return \DateTime::class === $class ? new \DateTime($data) : new \DateTimeImmutable($data);
9393
} catch (\Exception $e) {
94-
throw new UnexpectedValueException($e->getMessage(), $e->getCode(), $e);
94+
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
9595
}
9696
}
9797

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 8 additions & 7 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,10 +161,10 @@ 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
/**
@@ -252,7 +253,7 @@ final public function decode($data, $format, array $context = array())
252253
* @return object
253254
*
254255
* @throws LogicException
255-
* @throws UnexpectedValueException
256+
* @throws NotNormalizableValueException
256257
*/
257258
private function denormalizeObject($data, $class, $format, array $context = array())
258259
{
@@ -264,7 +265,7 @@ private function denormalizeObject($data, $class, $format, array $context = arra
264265
return $normalizer->denormalize($data, $class, $format, $context);
265266
}
266267

267-
throw new UnexpectedValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
268+
throw new NotNormalizableValueException(sprintf('Could not denormalize object of type %s, no supporting normalizer found.', $class));
268269
}
269270

270271
/**

0 commit comments

Comments
 (0)