mirror of
https://github.com/danog/Valinor.git
synced 2024-11-26 20:24:40 +01:00
fix: transform exception thrown during object binding into a message
This commit is contained in:
parent
cbf4e11154
commit
359e32d03d
@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace CuyZ\Valinor\Mapper\Object;
|
||||
|
||||
use CuyZ\Valinor\Definition\FunctionDefinition;
|
||||
use CuyZ\Valinor\Mapper\Tree\Message\ThrowableMessage;
|
||||
use Exception;
|
||||
|
||||
use function array_values;
|
||||
|
||||
@ -42,6 +44,10 @@ final class CallbackObjectBuilder implements ObjectBuilder
|
||||
/** @infection-ignore-all */
|
||||
$arguments = array_values($arguments);
|
||||
|
||||
return ($this->callback)(...$arguments);
|
||||
try {
|
||||
return ($this->callback)(...$arguments);
|
||||
} catch (Exception $exception) {
|
||||
throw ThrowableMessage::from($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ use CuyZ\Valinor\Mapper\MappingError;
|
||||
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
||||
use DateTime;
|
||||
use DateTimeImmutable;
|
||||
use DomainException;
|
||||
use stdClass;
|
||||
|
||||
use function get_class;
|
||||
@ -118,6 +119,23 @@ final class ObjectBindingMappingTest extends IntegrationTest
|
||||
self::assertSame(42, $result->int);
|
||||
self::assertSame(1337.404, $result->float);
|
||||
}
|
||||
|
||||
public function test_object_binding_throwing_exception_fails_mapping_with_message(): void
|
||||
{
|
||||
try {
|
||||
$this->mapperBuilder
|
||||
->bind(function (): stdClass {
|
||||
// @PHP8.0 use short closure
|
||||
throw new DomainException('some domain exception');
|
||||
})
|
||||
->mapper()
|
||||
->map(stdClass::class, []);
|
||||
|
||||
self::fail('No mapping error when one was expected');
|
||||
} catch (MappingError $exception) {
|
||||
self::assertSame('some domain exception', (string)$exception->node()->messages()[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final class SimpleDateTimeValues
|
||||
|
Loading…
Reference in New Issue
Block a user