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 6ef4a56 commit 8d00037
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# CHANGELOG

## 0.5.6
## 0.5.7

* fix: Improve exception message.

Expand Down
41 changes: 25 additions & 16 deletions src/Transformer/ObjectToObjectTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8d00037

Please sign in to comment.