Skip to content
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

Reorganize exceptions structure #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

solodkiy
Copy link
Contributor

Usually exceptions divide into checked and unchecked. It is more java concept, but also suitable for php world.

When \LogicException (unchecked) arises you don't usually want to catch and continue to work, better way is just show some error and fail fast. Because this exception means some bug in source code and only good way to deal with it - fix this bug.
This is the reason why phpstorm skips \LogicException by default.

My proposal is reorganisation money exceptions structure to make MoneyMismatchException and UnknownCurrencyException unchecked by extending \LogicException.

Before:
image
What should I do? Write useless try-catch, or add @throws annotation (which will be a lie)

After:
image
Phpstorm just skips this exception check.

Be aware, this changes is not really backward compatible!

return new self('No single currency for country ' . $countryCode . ': ' . implode(', ', $currencyCodes));
}
}
<?php
Copy link
Contributor Author

@solodkiy solodkiy Oct 17, 2020

Choose a reason for hiding this comment

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

Something was wrong with the line endings here.

@BenMorel
Copy link
Member

Hi, I know it's annoying to have PHPStorm complain about checked exceptions, however I'm not sure if I agree that UnknownCurrencyException should extend LogicException; if can happen in legitimate cases, like getting a currency code from user input and attempting to create a Money from there; you might want to catch the exception to warn the user that they provided an unknown currency.

@solodkiy
Copy link
Contributor Author

solodkiy commented Oct 17, 2020

In my PR UnknownCurrencyException doesn't extend LogicException directly, but through DomainException with is completely pass the case.

getting a currency code from user input and attempting to create a Money from there

You just should validate input first. You also can got something like array or object from some input and raise \TypeError. But should you catch it? I don't think so.

Also unchecked exceptions don't mean that you can catch it. You still can do it if you really want.

@solodkiy
Copy link
Contributor Author

solodkiy commented Oct 17, 2020

Also, there is already unchecked \InvalidArgumentException (which extend LogicException) in Currency constructor.
And still, we can create this object from user, or db data, we just should validate it first.
image

@BenMorel
Copy link
Member

Good point, I agree that there may be inconsistencies, but I'm still not sure which way is the right way to go.

@antonkomarev
Copy link
Contributor

I like the idea to replace MoneyException base class with MoneyException interface 👍

@jvanschie
Copy link

jvanschie commented Feb 17, 2021

I like the idea behind the logic exception, but maybe a runtime exception is better.

I also find it quite annoying that I have to add a throws annotation to all my doc blocks.

@jvanschie
Copy link

Does anyone know when this is going to be released?

@romkatsu
Copy link

@BenMorel When will it be released?

@BenMorel
Copy link
Member

This will be released as soon as I know for sure which direction is the way to go :)
I need to sit down and take the time to review each use case / exception and see if I agree with which one should be "checked" and which one shouldn't.

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

Successfully merging this pull request may close these issues.

5 participants