-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Uid] Add component-specific exception classes #60226
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
|
||
namespace Symfony\Component\Uid; | ||
|
||
use Symfony\Component\Uid\Exception\InvalidArgumentException; | ||
|
||
/** | ||
* @author Nicolas Grekas <[email protected]> | ||
*/ | ||
|
@@ -29,41 +31,41 @@ abstract public static function isValid(string $uid): bool; | |
/** | ||
* Creates an AbstractUid from an identifier represented in any of the supported formats. | ||
* | ||
* @throws \InvalidArgumentException When the passed value is not valid | ||
* @throws InvalidArgumentException When the passed value is not valid | ||
*/ | ||
abstract public static function fromString(string $uid): static; | ||
|
||
/** | ||
* @throws \InvalidArgumentException When the passed value is not valid | ||
* @throws InvalidArgumentException When the passed value is not valid | ||
*/ | ||
public static function fromBinary(string $uid): static | ||
{ | ||
if (16 !== \strlen($uid)) { | ||
throw new \InvalidArgumentException('Invalid binary uid provided.'); | ||
throw new InvalidArgumentException('Invalid binary uid provided.'); | ||
} | ||
|
||
return static::fromString($uid); | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException When the passed value is not valid | ||
* @throws InvalidArgumentException When the passed value is not valid | ||
*/ | ||
public static function fromBase58(string $uid): static | ||
{ | ||
if (22 !== \strlen($uid)) { | ||
throw new \InvalidArgumentException('Invalid base-58 uid provided.'); | ||
throw new InvalidArgumentException('Invalid base-58 uid provided.'); | ||
} | ||
|
||
return static::fromString($uid); | ||
} | ||
|
||
/** | ||
* @throws \InvalidArgumentException When the passed value is not valid | ||
* @throws InvalidArgumentException When the passed value is not valid | ||
*/ | ||
public static function fromBase32(string $uid): static | ||
{ | ||
if (26 !== \strlen($uid)) { | ||
throw new \InvalidArgumentException('Invalid base-32 uid provided.'); | ||
throw new InvalidArgumentException('Invalid base-32 uid provided.'); | ||
} | ||
|
||
return static::fromString($uid); | ||
|
@@ -72,12 +74,12 @@ public static function fromBase32(string $uid): static | |
/** | ||
* @param string $uid A valid RFC 9562/4122 uid | ||
* | ||
* @throws \InvalidArgumentException When the passed value is not valid | ||
* @throws InvalidArgumentException When the passed value is not valid | ||
*/ | ||
public static function fromRfc4122(string $uid): static | ||
{ | ||
if (36 !== \strlen($uid)) { | ||
throw new \InvalidArgumentException('Invalid RFC4122 uid provided.'); | ||
throw new InvalidArgumentException('Invalid RFC4122 uid provided.'); | ||
} | ||
|
||
return static::fromString($uid); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.3 | ||
--- | ||
|
||
* Add component-specific exception hierarchy | ||
|
||
7.2 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid\Exception; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException | ||
{ | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid\Exception; | ||
|
||
class InvalidUlidException extends InvalidArgumentException | ||
{ | ||
public function __construct(string $value) | ||
{ | ||
parent::__construct(\sprintf('Invalid ULID: "%s".', $value)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid\Exception; | ||
|
||
class InvalidUuidException extends InvalidArgumentException | ||
{ | ||
public function __construct( | ||
public readonly int $type, | ||
string $value, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you think it'd be possible to make value accessible as well? actually there'd been discussion regarding this in a previous MR, @fabpot prefers private property and getter There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I saw that but I think this is still better :) |
||
) { | ||
parent::__construct(\sprintf('Invalid UUID%s: "%s".', $type ? 'v'.$type : '', $value)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid\Exception; | ||
|
||
class LogicException extends \LogicException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
namespace Symfony\Component\Uid\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Uid\Exception\InvalidArgumentException; | ||
use Symfony\Component\Uid\Exception\InvalidUlidException; | ||
use Symfony\Component\Uid\MaxUlid; | ||
use Symfony\Component\Uid\NilUlid; | ||
use Symfony\Component\Uid\Tests\Fixtures\CustomUlid; | ||
|
@@ -41,7 +43,7 @@ public function testGenerate() | |
|
||
public function testWithInvalidUlid() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectException(InvalidUlidException::class); | ||
$this->expectExceptionMessage('Invalid ULID: "this is not a ulid".'); | ||
|
||
Comment on lines
-44
to
48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolas-grekas , really thank you, appreciate it a lot! |
||
new Ulid('this is not a ulid'); | ||
|
@@ -151,7 +153,7 @@ public function testFromBinary() | |
*/ | ||
public function testFromBinaryInvalidFormat(string $ulid) | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
Ulid::fromBinary($ulid); | ||
} | ||
|
@@ -178,7 +180,7 @@ public function testFromBase58() | |
*/ | ||
public function testFromBase58InvalidFormat(string $ulid) | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
Ulid::fromBase58($ulid); | ||
} | ||
|
@@ -205,7 +207,7 @@ public function testFromBase32() | |
*/ | ||
public function testFromBase32InvalidFormat(string $ulid) | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
Ulid::fromBase32($ulid); | ||
} | ||
|
@@ -232,7 +234,7 @@ public function testFromRfc4122() | |
*/ | ||
public function testFromRfc4122InvalidFormat(string $ulid) | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
Ulid::fromRfc4122($ulid); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolas-grekas , do you think it could contain value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generally don't include a value in our
InvalidArgumentException
exceptions. When such error happens for an error related to an object, this would mean that the exception retains a reference to that object (even if you never call the getter), which would have an impact on GC.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What @stof wrote (and where the previous PR blocked IMHO)
So better not to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a reference shall make the impact on garbage collection, unless it creates a circular reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, not something that makes sense to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but it's not the responsibility of the exception to carry this as a standalone value. (the native InvalidArgumentException doesn't carry it if you want an example)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, and it must have the value inside
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry you feel frustrated about the situation. We made a step in the direction you're aiming for but we don't do the last step for reasons I already shared. You'll have to parse the message for your use case...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel any frustration about anything. I have a big lump of faith down inside of me believing that it is the way to go, and that it should be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolas-grekas , I want you to receive this idea