Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better exception messages for json decoding #4

Merged
merged 1 commit into from May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/vendor/
composer.lock
.php_cs.cache
2 changes: 1 addition & 1 deletion src/AlsciendeSerializerBundle.php
Expand Up @@ -6,7 +6,7 @@

class AlsciendeSerializerBundle extends Bundle
{
public function getParent ()
public function getParent()
{
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/AlsciendeSerializerExtension.php
Expand Up @@ -17,7 +17,7 @@ class AlsciendeSerializerExtension extends Extension
/**
* {@inheritdoc}
*/
public function load (array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.yml');
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/BreakValueMismatchException.php
Expand Up @@ -2,6 +2,7 @@
declare(strict_types=1);

namespace Alsciende\SerializerBundle\Exception;

use Alsciende\SerializerBundle\Model\Block;

/**
Expand All @@ -19,7 +20,7 @@ class BreakValueMismatchException extends \Exception
* @param Block $block
* @param string $breakValue
*/
public function __construct (Block $block, string $breakValue)
public function __construct(Block $block, string $breakValue)
{
$this->block = $block;
$this->breakValue = $breakValue;
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/DependencyCycleException.php
Expand Up @@ -10,8 +10,8 @@
*/
class DependencyCycleException extends \Exception
{
public function __construct ()
public function __construct()
{
parent::__construct('Sources contain a cycle of dependencies, or a dependency is not configured as a Source.');
}
}
}
16 changes: 5 additions & 11 deletions src/Exception/FailedDecodingException.php
Expand Up @@ -3,26 +3,20 @@

namespace Alsciende\SerializerBundle\Exception;

use Alsciende\SerializerBundle\Model\Block;

/**
*/
class FailedDecodingException extends \Exception
{
/** @var string $format */
private $format;

/** @var string $data */
private $data;

/**
* FailedDecodingException constructor.
* @param string $format
* @param string $data
* @param Block $block
* @param string $message
*/
public function __construct (string $format, string $data, string $message)
public function __construct(string $format, Block $block, string $message)
{
$this->format = $format;
$this->data = $data;
parent::__construct($message);
parent::__construct(sprintf('Error while decoding %s file %s (%s).', $format, $block->getPath(), $message));
}
}
15 changes: 8 additions & 7 deletions src/Exception/InvalidBlockDataException.php
Expand Up @@ -3,20 +3,21 @@

namespace Alsciende\SerializerBundle\Exception;

use Alsciende\SerializerBundle\Model\Block;

/**
*/
class InvalidBlockDataException extends \Exception
{
/** @var string $data */
private $data;

/**
* InvalidBlockDataException constructor.
* @param string $data
* @param Block $block
*/
public function __construct (string $data)
public function __construct(Block $block)
{
$this->data = $data;
parent::__construct('Block data cannot be decoded to a numeric array.');
parent::__construct(sprintf(
'Error while decoding %s: data is not an array of objects.',
$block->getPath()
));
}
}
4 changes: 2 additions & 2 deletions src/Exception/MissingPropertyException.php
Expand Up @@ -18,10 +18,10 @@ class MissingPropertyException extends \Exception
* @param array $data
* @param string $columnName
*/
public function __construct (array $data, string $columnName)
public function __construct(array $data, string $columnName)
{
$this->data = $data;
$this->columnName = $columnName;
parent::__construct(sprintf('Missing property [%s] in data.', $columnName));
parent::__construct(sprintf('Missing property [%s] in data %s.', $columnName, json_encode($data)));
}
}
4 changes: 2 additions & 2 deletions src/Exception/ReverseSideException.php
Expand Up @@ -15,12 +15,12 @@ class ReverseSideException extends \Exception
* @param string $className
* @param string $fieldName
*/
public function __construct (string $className, string $fieldName)
public function __construct(string $className, string $fieldName)
{
parent::__construct(sprintf(
'Field [%s] is reverse side in class [%s]. Excepting owning side.',
$fieldName,
$className
));
}
}
}
4 changes: 2 additions & 2 deletions src/Exception/UnknownTypeException.php
Expand Up @@ -17,9 +17,9 @@ class UnknownTypeException extends \Exception
* UnknownTypeException constructor.
* @param string $type
*/
public function __construct (string $type)
public function __construct(string $type)
{
$this->type = $type;
parent::__construct('Unknown type in annotation.');
}
}
}
2 changes: 1 addition & 1 deletion src/Exception/ValidationException.php
Expand Up @@ -20,7 +20,7 @@ class ValidationException extends \Exception
* @param Fragment $fragment
* @param ConstraintViolationListInterface $errors
*/
public function __construct (Fragment $fragment, ConstraintViolationListInterface $errors)
public function __construct(Fragment $fragment, ConstraintViolationListInterface $errors)
{
parent::__construct(sprintf(
'Validation errors for path [%s] with data %s: %s.',
Expand Down
14 changes: 7 additions & 7 deletions src/Model/Block.php
Expand Up @@ -37,7 +37,7 @@ class Block
* @param string $data
* @param string|null $path
*/
public function __construct (string $data, string $path = null)
public function __construct(string $data, string $path = null)
{
$this->data = $data;
if (isset($path)) {
Expand All @@ -50,7 +50,7 @@ public function __construct (string $data, string $path = null)
*
* @return Source
*/
public function getSource (): Source
public function getSource(): Source
{
return $this->source;
}
Expand All @@ -59,7 +59,7 @@ public function getSource (): Source
*
* @return string|null
*/
public function getPath ()
public function getPath()
{
return $this->path;
}
Expand All @@ -68,7 +68,7 @@ public function getPath ()
*
* @return string
*/
public function getData (): string
public function getData(): string
{
return $this->data;
}
Expand All @@ -77,7 +77,7 @@ public function getData (): string
*
* @return string
*/
public function getName (): string
public function getName(): string
{
return $this->name;
}
Expand All @@ -87,7 +87,7 @@ public function getName (): string
* @param Source $source
* @return $this
*/
public function setSource (Source $source): self
public function setSource(Source $source): self
{
$this->source = $source;

Expand All @@ -99,7 +99,7 @@ public function setSource (Source $source): self
* @param string $name
* @return $this
*/
public function setName ($name): self
public function setName($name): self
{
$this->name = $name;

Expand Down
14 changes: 7 additions & 7 deletions src/Model/Fragment.php
Expand Up @@ -30,7 +30,7 @@ class Fragment
*/
private $hydratedEntity;

public function __construct (Block $block, array $data)
public function __construct(Block $block, array $data)
{
$this->block = $block;
$this->rawData = $data;
Expand All @@ -40,7 +40,7 @@ public function __construct (Block $block, array $data)
*
* @return array
*/
public function getRawData (): array
public function getRawData(): array
{
return $this->rawData;
}
Expand All @@ -49,15 +49,15 @@ public function getRawData (): array
*
* @return Block
*/
public function getBlock (): Block
public function getBlock(): Block
{
return $this->block;
}

/**
* @return object
*/
public function getHydratedEntity ()
public function getHydratedEntity()
{
return $this->hydratedEntity;
}
Expand All @@ -67,7 +67,7 @@ public function getHydratedEntity ()
*
* @return $this
*/
public function setHydratedEntity ($hydratedEntity): self
public function setHydratedEntity($hydratedEntity): self
{
$this->hydratedEntity = $hydratedEntity;

Expand All @@ -77,7 +77,7 @@ public function setHydratedEntity ($hydratedEntity): self
/**
* @return array
*/
public function getNormalizedData (): array
public function getNormalizedData(): array
{
return $this->normalizedData;
}
Expand All @@ -87,7 +87,7 @@ public function getNormalizedData (): array
*
* @return $this
*/
public function setNormalizedData ($normalizedData): self
public function setNormalizedData($normalizedData): self
{
$this->normalizedData = $normalizedData;

Expand Down
16 changes: 8 additions & 8 deletions src/Model/Source.php
Expand Up @@ -24,7 +24,7 @@ class Source
/** @var Block[] $blocks */
private $blocks;

public function __construct (string $className, string $break = null)
public function __construct(string $className, string $break = null)
{
$this->className = $className;
$this->break = $break;
Expand All @@ -36,15 +36,15 @@ public function __construct (string $className, string $break = null)
*
* @return string|null
*/
public function getBreak ()
public function getBreak()
{
return $this->break;
}

/**
* @return bool
*/
public function hasBreak (): bool
public function hasBreak(): bool
{
return isset($this->break);
}
Expand All @@ -53,7 +53,7 @@ public function hasBreak (): bool
*
* @return string
*/
function getClassName (): string
public function getClassName(): string
{
return $this->className;
}
Expand All @@ -63,7 +63,7 @@ function getClassName (): string
*
* @return array
*/
public function getProperties (): array
public function getProperties(): array
{
return $this->properties;
}
Expand All @@ -75,7 +75,7 @@ public function getProperties (): array
* @param Field $config
* @return $this
*/
public function addProperty ($name, Field $config): self
public function addProperty($name, Field $config): self
{
$this->properties[$name] = $config;

Expand All @@ -85,7 +85,7 @@ public function addProperty ($name, Field $config): self
/**
* @return Block[]
*/
public function getBlocks (): array
public function getBlocks(): array
{
return $this->blocks;
}
Expand All @@ -94,7 +94,7 @@ public function getBlocks (): array
* @param Block $block
* @return $this
*/
public function addBlock (Block $block): self
public function addBlock(Block $block): self
{
$this->blocks[] = $block;
$block->setSource($this);
Expand Down
12 changes: 8 additions & 4 deletions src/Service/EncodingService.php
Expand Up @@ -23,18 +23,22 @@ class EncodingService
* @throws FailedDecodingException
* @throws InvalidBlockDataException
*/
public function decode (Block $block): array
public function decode(Block $block): array
{
$list = json_decode($block->getData(), true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new FailedDecodingException('json', $block->getData(), json_last_error_msg());
throw new FailedDecodingException('json', $block, json_last_error_msg());
}
$valid = is_array($list) && (count($list) === 0 || array_key_exists(0, $list));
if ($valid === false) {
throw new InvalidBlockDataException($block->getData());
throw new InvalidBlockDataException($block);
}
$fragments = [];
foreach ($list as $data) {
$valid = is_array($data) && count($data) > 0 && !array_key_exists(0, $data);
if ($valid === false) {
throw new InvalidBlockDataException($block);
}
$fragments[] = new Fragment($block, $this->applyBreak($block, $data));
}

Expand All @@ -47,7 +51,7 @@ public function decode (Block $block): array
* @throws BreakValueMismatchException
* @return array
*/
private function applyBreak (Block $block, array $data): array
private function applyBreak(Block $block, array $data): array
{
$break = $block->getSource()->getBreak();
if (isset($break)) {
Expand Down