diff --git a/CHANGELOG.md b/CHANGELOG.md index 0274fa40..d27ce165 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## 0.5.6 + +* fix: Improve exception message. + ## 0.5.5 * docs: Add link to documentation website. diff --git a/src/Transformer/Exception/UnableToReadException.php b/src/Transformer/Exception/UnableToReadException.php new file mode 100644 index 00000000..2e25c46b --- /dev/null +++ b/src/Transformer/Exception/UnableToReadException.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Transformer\Exception; + +class UnableToReadException extends NotMappableValueException +{ + public function __construct( + mixed $source, + mixed $target, + mixed $object, + string $propertyName, + \Throwable $previous = null + ) { + parent::__construct( + sprintf( + 'Trying to map source type "%s" to target type "%s", but encountered an error when trying to read from the property "%s" on object type "%s".', + \get_debug_type($source), + \get_debug_type($target), + $propertyName, + \get_debug_type($object) + ), + 0, + $previous + ); + } +} diff --git a/src/Transformer/Exception/UnableToWriteException.php b/src/Transformer/Exception/UnableToWriteException.php new file mode 100644 index 00000000..6aac711c --- /dev/null +++ b/src/Transformer/Exception/UnableToWriteException.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE file + * that was distributed with this source code. + */ + +namespace Rekalogika\Mapper\Transformer\Exception; + +class UnableToWriteException extends NotMappableValueException +{ + public function __construct( + mixed $source, + mixed $target, + mixed $object, + string $propertyName, + \Throwable $previous = null + ) { + parent::__construct( + sprintf( + 'Trying to map source type "%s" to target type "%s", but encountered an error when trying to write to the property "%s" on object type "%s".', + \get_debug_type($source), + \get_debug_type($target), + $propertyName, + \get_debug_type($object) + ), + 0, + $previous + ); + } +} diff --git a/src/Transformer/ObjectToObjectTransformer.php b/src/Transformer/ObjectToObjectTransformer.php index c82f28e0..327d7900 100644 --- a/src/Transformer/ObjectToObjectTransformer.php +++ b/src/Transformer/ObjectToObjectTransformer.php @@ -25,10 +25,13 @@ use Rekalogika\Mapper\Transformer\Exception\InstantiationFailureException; use Rekalogika\Mapper\Transformer\Exception\InvalidClassException; use Rekalogika\Mapper\Transformer\Exception\NotAClassException; +use Rekalogika\Mapper\Transformer\Exception\UnableToWriteException; use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; use Rekalogika\Mapper\Util\TypeCheck; use Rekalogika\Mapper\Util\TypeFactory; +use Symfony\Component\PropertyAccess\Exception\AccessException; use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; +use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface; @@ -124,7 +127,11 @@ public function transform( context: $context ); - $this->propertyAccessor->setValue($target, $propertyName, $targetPropertyValue); + try { + $this->propertyAccessor->setValue($target, $propertyName, $targetPropertyValue); + } catch (AccessException | UnexpectedTypeException $e) { + throw new UnableToWriteException($source, $target, $target, $propertyName, $e); + } } return $target;