Skip to content

Commit

Permalink
added Exception's from SerializerBundle
Browse files Browse the repository at this point in the history
  • Loading branch information
lsmith77 committed Jun 9, 2011
1 parent 715dfcd commit 796152d
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Encoder/XmlEncoder.php
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\Serializer\Encoder;

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -268,7 +269,7 @@ private function buildXml($parentNode, $data)

return $this->appendNode($parentNode, $data, 'data');
}
throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
throw new UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
}

/**
Expand Down
21 changes: 21 additions & 0 deletions Exception/Exception.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* Base exception
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface Exception
{
}
21 changes: 21 additions & 0 deletions Exception/InvalidArgumentException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* InvalidArgumentException
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements Exception
{
}
21 changes: 21 additions & 0 deletions Exception/LogicException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* LogicException
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class LogicException extends \LogicException implements Exception
{
}
21 changes: 21 additions & 0 deletions Exception/RuntimeException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* RuntimeException
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class RuntimeException extends \RuntimeException implements Exception
{
}
21 changes: 21 additions & 0 deletions Exception/UnexpectedValueException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* UnexpectedValueException
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*/
class UnexpectedValueException extends \UnexpectedValueException implements Exception
{
}
21 changes: 21 additions & 0 deletions Exception/UnsupportedException.php
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Symfony\Component\Serializer\Exception;

/**
* UnsupportedException
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class UnsupportedException extends InvalidArgumentException
{
}
3 changes: 2 additions & 1 deletion Normalizer/GetSetMethodNormalizer.php
Expand Up @@ -3,6 +3,7 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -80,7 +81,7 @@ public function denormalize($data, $class, $format = null)
// don't run set for a parameter passed to the constructor
unset($data[$paramName]);
} else if (!$constructorParameter->isOptional()) {
throw new \RuntimeException(
throw new RuntimeException(
'Cannot create an instance of ' . $class .
' from serialized data because its constructor requires ' .
'parameter "' . $constructorParameter->getName() .
Expand Down
21 changes: 13 additions & 8 deletions Serializer.php
Expand Up @@ -6,6 +6,9 @@
use Symfony\Component\Serializer\Encoder\EncoderInterface;
use Symfony\Component\Serializer\Encoder\DecoderInterface;
use Symfony\Component\Serializer\Encoder\NormalizationAwareInterface;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -41,7 +44,7 @@ class Serializer implements SerializerInterface
public function serialize($data, $format)
{
if (!isset($this->encoders[$format])) {
throw new \UnexpectedValueException('No encoder registered for the '.$format.' format');
throw new UnexpectedValueException('No encoder registered for the '.$format.' format');
}
if (!$this->encoders[$format] instanceof NormalizationAwareInterface) {
$data = $this->normalize($data);
Expand Down Expand Up @@ -83,7 +86,7 @@ public function normalize($data, $format = null)

return $data;
}
throw new \UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
throw new UnexpectedValueException('An unexpected value could not be normalized: '.var_export($data, true));
}

/**
Expand Down Expand Up @@ -128,7 +131,7 @@ public function decode($data, $format)
private function normalizeObject($object, $format = null)
{
if (!$this->normalizers) {
throw new \LogicException('You must register at least one normalizer to be able to normalize objects.');
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
}
$class = get_class($object);
if (isset($this->normalizerCache[$class][$format])) {
Expand All @@ -141,7 +144,7 @@ private function normalizeObject($object, $format = null)
return $normalizer->normalize($object, $format);
}
}
throw new \UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
throw new UnexpectedValueException('Could not normalize object of type '.$class.', no supporting normalizer found.');
}

/**
Expand All @@ -155,7 +158,7 @@ private function normalizeObject($object, $format = null)
private function denormalizeObject($data, $class, $format = null)
{
if (!$this->normalizers) {
throw new \LogicException('You must register at least one normalizer to be able to denormalize objects.');
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
}
if (isset($this->denormalizerCache[$class][$format])) {
return $this->denormalizerCache[$class][$format]->denormalize($data, $class, $format);
Expand All @@ -167,7 +170,7 @@ private function denormalizeObject($data, $class, $format = null)
return $normalizer->denormalize($data, $class, $format);
}
}
throw new \UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
throw new UnexpectedValueException('Could not denormalize object of type '.$class.', no supporting normalizer found.');
}

/**
Expand Down Expand Up @@ -205,9 +208,11 @@ public function setEncoder($format, EncoderInterface $encoder)
*/
public function getEncoder($format)
{
if (isset($this->encoders[$format])) {
return $this->encoders[$format];
if (!isset($this->encoders[$format])) {
throw new RuntimeException(sprintf('No encoder found for format "%s".', $format));
}

return $this->encoders[$format];
}

/**
Expand Down

0 comments on commit 796152d

Please sign in to comment.