Skip to content

[Serializer] throw more specific exceptions #21239

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
Sep 27, 2017
Merged

Conversation

xabbuh
Copy link
Member

@xabbuh xabbuh commented Jan 11, 2017

Q A
Branch? 3.4
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #20534
License MIT
Doc PR

@fabpot
Copy link
Member

fabpot commented Jan 11, 2017

👍

*/
public function denormalize($data, $class, $format = null, array $context = array())
{
if (!preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i', $data)) {
throw new UnexpectedValueException('The provided "data:" URI is not valid.');
throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO this example here should actually continue to throw the old exception. This is decodeable, but just not valid in the domain. I guess this example would be a 422 instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

But the same is true for the other places where we throw a NotNormalizableValueException in this PR, isn't it?

@@ -79,7 +79,7 @@ public function denormalize($data, $class, $format = null, array $context = arra

$dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();

throw new UnexpectedValueException(sprintf(
throw new NotNormalizableValueException(sprintf(
Copy link
Member

@dunglas dunglas Jan 11, 2017

Choose a reason for hiding this comment

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

Maybe can we set a specific exception code depending of the cause to be able to easily detect if it's an invalid data: URI, or an invalid date and so on?

Copy link
Member Author

Choose a reason for hiding this comment

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

I have thought about that too. The reason I did not add it is consistency. We would only be able to add codes for Symfony's own normalizer. However, it's quite common to add your own normalizers in libraries/projects. As a consequence exception codes would only be useful when they are issued by built-in normalizers. I don't really like that. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

We can maybe reserve a range for app codes?

@nicolas-grekas nicolas-grekas added this to the 3.3 milestone Jan 16, 2017
@fabpot
Copy link
Member

fabpot commented Mar 1, 2017

@xabbuh Any feedback on the comments?

@nicolas-grekas nicolas-grekas modified the milestones: 3.3, 3.4 Apr 26, 2017
@xabbuh xabbuh changed the base branch from master to 3.4 May 18, 2017 07:16
@fabpot
Copy link
Member

fabpot commented Jul 6, 2017

@xabbuh What about this one?

@xabbuh
Copy link
Member Author

xabbuh commented Jul 12, 2017

I think I need @dunglas to answer my comments. :)

@fabpot
Copy link
Member

fabpot commented Sep 26, 2017

So, now that @dunglas approved this PR, @xabbuh can you rebase it so that we can merge it? Thanks.

@xabbuh
Copy link
Member Author

xabbuh commented Sep 27, 2017

PR rebased

@fabpot
Copy link
Member

fabpot commented Sep 27, 2017

Thank you @xabbuh.

@fabpot fabpot merged commit aa30d04 into symfony:3.4 Sep 27, 2017
fabpot added a commit that referenced this pull request Sep 27, 2017
This PR was merged into the 3.4 branch.

Discussion
----------

[Serializer] throw more specific exceptions

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #20534
| License       | MIT
| Doc PR        |

Commits
-------

aa30d04 [Serializer] throw more specific exceptions
@xabbuh xabbuh deleted the issue-20534 branch September 27, 2017 14:36
@dawehner
Copy link
Contributor

Thank you for solving this problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants