Skip to content

Commit

Permalink
fix: Improve exception message.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 13, 2024
1 parent 8d40fca commit 6ef4a56
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.5.6

* fix: Improve exception message.

## 0.5.5

* docs: Add link to documentation website.
Expand Down
37 changes: 37 additions & 0 deletions src/Transformer/Exception/UnableToReadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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
);
}
}
37 changes: 37 additions & 0 deletions src/Transformer/Exception/UnableToWriteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

/*
* This file is part of rekalogika/mapper package.
*
* (c) Priyadi Iman Nurcahyo <https://rekalogika.dev>
*
* 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
);
}
}
9 changes: 8 additions & 1 deletion src/Transformer/ObjectToObjectTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6ef4a56

Please sign in to comment.