Skip to content

[Live] Adding 2 errors related to an invalid component set up #590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
Expand Down Expand Up @@ -71,7 +73,11 @@ public function dehydrate(MountedComponent $mounted): DehydratedComponent
$frontendPropertyNames[$frontendName] = $name;

// TODO: improve error message if not readable
$value = $this->propertyAccessor->getValue($component, $name);
try {
$value = $this->propertyAccessor->getValue($component, $name);
} catch (UninitializedPropertyException $exception) {
throw new \LogicException(sprintf('The "%s" property on the "%s" component is uninitialized. Did you forget to pass this into the component?', $name, \get_class($component)), 0, $exception);
}
$dehydratedValue = null;

if ($method = $liveProp->dehydrateMethod()) {
Expand Down Expand Up @@ -153,7 +159,24 @@ public function hydrate(object $component, array $data, string $componentName):
} elseif (!$value && $type && $type->allowsNull() && is_a($type->getName(), \BackedEnum::class, true) && !\in_array($value, array_map(fn (\BackedEnum $e) => $e->value, $type->getName()::cases()))) {
$value = null;
} elseif (null !== $value && $type && !$type->isBuiltin()) {
$value = $this->normalizer->denormalize($value, $type->getName(), 'json', [self::LIVE_CONTEXT => true]);
try {
$value = $this->normalizer->denormalize($value, $type->getName(), 'json', [self::LIVE_CONTEXT => true]);
} catch (ExceptionInterface $exception) {
$json = json_encode($value);
$message = sprintf(
'The normalizer was used to hydrate/denormalize the "%s" property on your "%s" live component, but it failed: %s',
$name,
\get_class($component),
$exception->getMessage()
);

// unless the data is gigantic, include it in the error to help
if (\strlen($json) < 1000) {
$message .= sprintf(' The data sent from the frontend was: %s', $json);
}

throw new \LogicException($message, 0, $exception);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of this:

Screen Shot 2022-12-02 at 3 42 06 PM

I wish I could add line breaks, but at least all the info is there.

}

if ($dehydratedComponent->hasExposed($frontendName)) {
Expand Down