Skip to content

Update NotNormalizableValueException with the attribute information #42733

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,17 @@
*/
class NotNormalizableValueException extends UnexpectedValueException
{
private ?string $attribute;

public function __construct(string $message, int $code = 0, \Throwable $previous = null, ?string $attribute = null)
{
$this->$attribute = $attribute;

parent::__construct($message, $code, $previous);
}

public function getAttribute(): ?string
{
return $this->attribute;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function denormalize($data, string $type, string $format = null, array $c
try {
$this->setAttributeValue($object, $attribute, $value, $format, $context);
} catch (InvalidArgumentException $e) {
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type), $e->getCode(), $e);
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": '.$e->getMessage(), $attribute, $type), $e->getCode(), $e, $attribute);
}
}

Expand Down Expand Up @@ -397,14 +397,14 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
} elseif ('true' === $data || '1' === $data) {
$data = true;
} else {
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data));
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be bool ("%s" given).', $attribute, $currentClass, $data), 0, null, $attribute);
}
break;
case Type::BUILTIN_TYPE_INT:
if (ctype_digit($data) || '-' === $data[0] && ctype_digit(substr($data, 1))) {
$data = (int) $data;
} else {
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data));
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data), 0, null, $attribute);
}
break;
case Type::BUILTIN_TYPE_FLOAT:
Expand All @@ -420,7 +420,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
case '-INF':
return -\INF;
default:
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data));
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be float ("%s" given).', $attribute, $currentClass, $data), 0, null, $attribute);
}

break;
Expand Down Expand Up @@ -491,7 +491,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
return $data;
}

throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), get_debug_type($data)));
throw new NotNormalizableValueException(sprintf('The type of the "%s" attribute for class "%s" must be one of "%s" ("%s" given).', $attribute, $currentClass, implode('", "', array_keys($expectedTypes)), get_debug_type($data)), 0, null, $attribute);
}

/**
Expand Down