Skip to content

Commit

Permalink
feat: Main transformer's exception now accepts $context variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 14, 2024
1 parent 841cccb commit 638e8ca
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
error messages.
* feat: Context methods now accept `$context` variable, and pass it to
exceptions.
* feat: Main transformer's exception now accepts `$context` variable.

## 0.5.8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Rekalogika\Mapper\MainTransformer\Exception;

use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Exception\UnexpectedValueException;
use Rekalogika\Mapper\Transformer\Contracts\MixedType;
use Rekalogika\Mapper\Util\TypeUtil;
Expand All @@ -24,11 +25,14 @@ class CannotFindTransformerException extends UnexpectedValueException
* @param array<int,Type|MixedType> $sourceTypes
* @param array<int,Type|MixedType> $targetTypes
*/
public function __construct(array $sourceTypes, array $targetTypes)
public function __construct(array $sourceTypes, array $targetTypes, Context $context)
{
$sourceTypes = TypeUtil::getDebugType($sourceTypes);
$targetTypes = TypeUtil::getDebugType($targetTypes);

parent::__construct(sprintf('Cannot find a matching transformer for mapping the source types "%s" to the target types "%s"', $sourceTypes, $sourceTypes));
parent::__construct(
sprintf('Cannot find a matching transformer for mapping the source types "%s" to the target types "%s"', $sourceTypes, $sourceTypes),
context: $context
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@

namespace Rekalogika\Mapper\MainTransformer\Exception;

use Rekalogika\Mapper\Context\Context;
use Rekalogika\Mapper\Exception\UnexpectedValueException;
use Rekalogika\Mapper\Transformer\Contracts\MixedType;
use Rekalogika\Mapper\Util\TypeUtil;
use Symfony\Component\PropertyInfo\Type;

class TransformerReturnsUnexpectedValueException extends UnexpectedValueException
{
public function __construct(Type|MixedType $type, mixed $target)
public function __construct(Type|MixedType $type, mixed $target, Context $context)
{
$message = sprintf(
'Mapper returns unexpected value. Expected type "%s", but got "%s"',
TypeUtil::getTypeString($type),
get_debug_type($target),
);

parent::__construct($message);
parent::__construct($message, context: $context);
}
}
4 changes: 2 additions & 2 deletions src/MainTransformer/MainTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function transform(
);

if (!TypeCheck::isVariableInstanceOf($result, $targetType)) {
throw new TransformerReturnsUnexpectedValueException($targetType, $result);
throw new TransformerReturnsUnexpectedValueException($targetType, $result, context: $context);
}

// if the target type is not null, cache it
Expand All @@ -191,6 +191,6 @@ public function transform(
return $result;
}

throw new CannotFindTransformerException($sourceTypes, $targetTypes);
throw new CannotFindTransformerException($sourceTypes, $targetTypes, context: $context);
}
}

0 comments on commit 638e8ca

Please sign in to comment.