Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid] Add value to InvalidArgumentException #60323

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
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
8 changes: 4 additions & 4 deletions src/Symfony/Component/Uid/AbstractUid.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract public static function fromString(string $uid): static;
public static function fromBinary(string $uid): static
{
if (16 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid binary uid provided.');
throw new InvalidArgumentException($uid, 'Invalid binary uid provided.');
Copy link
Member

Choose a reason for hiding this comment

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

I quickly scanned our code on how we deal in other places with invalid arguments. Having a dedicated argument is something we don't have anywhere else, but we could embed the invalid value in the exception message. That's something we do quite frequently.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having a dedicated argument is something we don't have anywhere else

That's a lie.

A lot of exceptions allow passing invalid argument, and expose it.

For example, few exceptions from DependencyInjection:

  • DependencyInjection\Exception\AutowiringFailedException exposes service id
  • DependencyInjection\Exception\ParameterCircularReferenceException exposes array of parameters
  • DependencyInjection\Exception\ParameterNotFoundException exposes key, sourceId, sourceKey, alternatives, etc
  • DependencyInjection\Exception\ServiceCircularReferenceException exposes service id
  • DependencyInjection\Exception\ServiceNotFoundException exposes service id, and other

And there are at least 40 other distinct exceptions that follow it.

}

return static::fromString($uid);
Expand All @@ -53,7 +53,7 @@ public static function fromBinary(string $uid): static
public static function fromBase58(string $uid): static
{
if (22 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid base-58 uid provided.');
throw new InvalidArgumentException($uid, 'Invalid base-58 uid provided.');
}

return static::fromString($uid);
Expand All @@ -65,7 +65,7 @@ public static function fromBase58(string $uid): static
public static function fromBase32(string $uid): static
{
if (26 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid base-32 uid provided.');
throw new InvalidArgumentException($uid, 'Invalid base-32 uid provided.');
}

return static::fromString($uid);
Expand All @@ -79,7 +79,7 @@ public static function fromBase32(string $uid): static
public static function fromRfc4122(string $uid): static
{
if (36 !== \strlen($uid)) {
throw new InvalidArgumentException('Invalid RFC4122 uid provided.');
throw new InvalidArgumentException($uid, 'Invalid RFC4122 uid provided.');
}

return static::fromString($uid);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Uid/BinaryUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
{
if (\PHP_INT_SIZE >= 8) {
if (-self::TIME_OFFSET_INT > $time = (int) $time->format('Uu0')) {
throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
throw new InvalidArgumentException($time, 'The given UUID date cannot be earlier than 1582-10-15.');
}

return str_pad(dechex(self::TIME_OFFSET_INT + $time), 16, '0', \STR_PAD_LEFT);
Expand All @@ -173,7 +173,7 @@ public static function dateTimeToHex(\DateTimeInterface $time): string
$time = $time->format('Uu0');
$negative = '-' === $time[0];
if ($negative && self::TIME_OFFSET_INT < $time = substr($time, 1)) {
throw new InvalidArgumentException('The given UUID date cannot be earlier than 1582-10-15.');
throw new InvalidArgumentException($time, 'The given UUID date cannot be earlier than 1582-10-15.');
}
$time = self::fromBase($time, self::BASE10);
$time = str_pad($time, 8, "\0", \STR_PAD_LEFT);
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Uid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add component-specific exception hierarchy
* Add `InvalidArgumentException::$value`

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@

class InvalidArgumentException extends \InvalidArgumentException
{
public function __construct(
public readonly mixed $value,
string $message,
) {
parent::__construct($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ class InvalidUlidException extends InvalidArgumentException
{
public function __construct(string $value)
{
parent::__construct(\sprintf('Invalid ULID: "%s".', $value));
parent::__construct($value, \sprintf('Invalid ULID: "%s".', $value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function __construct(
public readonly int $type,
string $value,
) {
parent::__construct(\sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value));
parent::__construct($value, \sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value));
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Uid/Ulid.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static function generate(?\DateTimeInterface $time = null): string
$time = microtime(false);
$time = substr($time, 11).substr($time, 2, 3);
} elseif (0 > $time = $time->format('Uv')) {
throw new InvalidArgumentException('The timestamp must be positive.');
throw new InvalidArgumentException($time, 'The timestamp must be positive.');
}

if ($time > self::$time || (null !== $mtime && $time !== self::$time)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Uid/UuidV6.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function toV7(): UuidV7
$uuid = $this->uid;
$time = BinaryUtil::hexToNumericString('0'.substr($uuid, 0, 8).substr($uuid, 9, 4).substr($uuid, 15, 3));
if ('-' === $time[0]) {
throw new InvalidArgumentException('Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
throw new InvalidArgumentException($uuid, 'Cannot convert UUID to v7: its timestamp is before the Unix epoch.');
}

$ms = \strlen($time) > 4 ? substr($time, 0, -4) : '0';
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Uid/UuidV7.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static function generate(?\DateTimeInterface $time = null): string
$time = microtime(false);
$time = substr($time, 11).substr($time, 2, 3);
} elseif (0 > $time = $time->format('Uv')) {
throw new InvalidArgumentException('The timestamp must be positive.');
throw new InvalidArgumentException($time, 'The timestamp must be positive.');
}

if ($time > self::$time || (null !== $mtime && $time !== self::$time)) {
Expand Down
Loading