You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi,
when deserialize data to an entity with an enum attribute and data contains an invalid enum 's value, the serializer throws InvalidArgumentException which is a logic exception.
This is not a program logic error so InvalidArgumentException is not correct.
InvalidArgumentException is very a common exception so catching it is very difficult. A programmer must test the exception's message if an other logic exception have to be threw and logged.
For example, when deserialize a wrong date format then NotNormalizableValueException is thrown. For a programmer these situations are equal.
Example
enum MyEnum: int
{
case A = 0;
case B = 1;
}
class MyEntity
{
publicMyEnum$myEnum;
}
$entity = $serializer->deserialize('{"myEnum":2}', MyEntity::class, 'json')
Before:
Throws InvalidArgumentException
After:
Throws NotNormalizableValueException
The text was updated successfully, but these errors were encountered:
OskarStark
changed the title
Serializer should throws NotNormalizableValueException instead of InvalidArgumentException when an invalid enum is deserialized
Serializer should throw NotNormalizableValueException instead of InvalidArgumentException when an invalid enum is deserialized
Mar 16, 2023
In my opinion we should throw NotNormalizableValueException instead of InvalidArgumentException. At this moment it's not possible to use backed enums in real world app while deserializing invalid data from user (user provide wrong value to backed enum setter). InvalidArgumentException does not have methods to return the detailed error message to user that the allowed values are x, y, z but the provided value is k, we can only return "Hey, something goes wrong, guess what!", without any details.
Description
Hi,
when deserialize data to an entity with an enum attribute and data contains an invalid enum 's value, the serializer throws
InvalidArgumentException
which is a logic exception.InvalidArgumentException
is not correct.InvalidArgumentException
is very a common exception so catching it is very difficult. A programmer must test the exception's message if an other logic exception have to be threw and logged.For example, when deserialize a wrong date format then
NotNormalizableValueException
is thrown. For a programmer these situations are equal.Example
Before:
Throws
InvalidArgumentException
After:
Throws
NotNormalizableValueException
The text was updated successfully, but these errors were encountered: