From 8d00037a665729ff99d93a6ca0b2cc5a5614349d Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Sun, 14 Jan 2024 00:47:21 +0700 Subject: [PATCH] fix: Improve exception message. --- CHANGELOG.md | 2 +- src/Transformer/ObjectToObjectTransformer.php | 41 +++++++++++-------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d27ce165..a9122abc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # CHANGELOG -## 0.5.6 +## 0.5.7 * fix: Improve exception message. diff --git a/src/Transformer/ObjectToObjectTransformer.php b/src/Transformer/ObjectToObjectTransformer.php index 327d7900..c38bf789 100644 --- a/src/Transformer/ObjectToObjectTransformer.php +++ b/src/Transformer/ObjectToObjectTransformer.php @@ -25,6 +25,7 @@ use Rekalogika\Mapper\Transformer\Exception\InstantiationFailureException; use Rekalogika\Mapper\Transformer\Exception\InvalidClassException; use Rekalogika\Mapper\Transformer\Exception\NotAClassException; +use Rekalogika\Mapper\Transformer\Exception\UnableToReadException; use Rekalogika\Mapper\Transformer\Exception\UnableToWriteException; use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; use Rekalogika\Mapper\Util\TypeCheck; @@ -155,12 +156,24 @@ private function resolveTargetPropertyValue( throw new InvalidArgumentException(sprintf('Cannot get type of target property "%s::$%s".', $targetClass, $propertyName)); } - /** @var mixed */ - $sourcePropertyValue = $this->propertyAccessor->getValue($source, $propertyName); + try { + /** @var mixed */ + $sourcePropertyValue = $this->propertyAccessor->getValue($source, $propertyName); + } catch (NoSuchPropertyException $e) { + throw new IncompleteConstructorArgument($source, $targetClass, $propertyName, $e); + } catch (AccessException | UnexpectedTypeException $e) { + throw new UnableToReadException($source, $target, $source, $propertyName, $e); + } if ($target !== null) { - /** @var mixed */ - $targetPropertyValue = $this->propertyAccessor->getValue($target, $propertyName); + try { + /** @var mixed */ + $targetPropertyValue = $this->propertyAccessor->getValue($target, $propertyName); + } catch (NoSuchPropertyException $e) { + throw new IncompleteConstructorArgument($source, $targetClass, $propertyName, $e); + } catch (AccessException | UnexpectedTypeException $e) { + throw new UnableToReadException($source, $target, $target, $propertyName, $e); + } } else { $targetPropertyValue = null; } @@ -204,18 +217,14 @@ protected function instantiateTarget( $constructorArguments = []; foreach ($initializableTargetProperties as $propertyName) { - try { - /** @var mixed */ - $targetPropertyValue = $this->resolveTargetPropertyValue( - source: $source, - target: null, - propertyName: $propertyName, - targetClass: $targetClass, - context: $context - ); - } catch (NoSuchPropertyException $e) { - throw new IncompleteConstructorArgument($source, $targetClass, $propertyName, $e); - } + /** @var mixed */ + $targetPropertyValue = $this->resolveTargetPropertyValue( + source: $source, + target: null, + propertyName: $propertyName, + targetClass: $targetClass, + context: $context + ); /** @psalm-suppress MixedAssignment */ $constructorArguments[$propertyName] = $targetPropertyValue;