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

Exceptions should inherit from same base class #11

Closed
svisser opened this issue Sep 11, 2014 · 2 comments
Closed

Exceptions should inherit from same base class #11

svisser opened this issue Sep 11, 2014 · 2 comments
Assignees

Comments

@svisser
Copy link

svisser commented Sep 11, 2014

The exception classes in this package currently don't all inherit from the same base class. This makes it difficult to "do some money calculations" and stop whenever an error occurs. For example:

from decimal import Decimal
from money import xrates, Money, XMoney
xrates.install('money.exchange.SimpleBackend')
xrates.base = 'GBP'
xrates.setrate('EUR', Decimal('2'))

# Raises money.exceptions.ExchangeRateNotFound:
price1 = XMoney('3.00', 'GBP') + Money('50.00', 'AUD')

# Raises money.exceptions.CurrencyMismatch:
price2 = Money('3.00', 'GBP') + XMoney('50.00', 'AUD')

This means as a developer I basically have to inspect the money code to know what exceptions can be raised. I would like to write:

try:
    price1 = XMoney('3.00', 'GBP') + Money('50.00', 'AUD')
    price2 = Money('3.00', 'GBP') + XMoney('50.00', 'AUD')
except (all money exceptions to catch here):
    print("Something went wrong")

but I can't because I'd need to know what all those exceptions are. By introducing a base class we can catch and handle everything:

try:
    price1 = XMoney('3.00', 'GBP') + Money('50.00', 'AUD')
    price2 = Money('3.00', 'GBP') + XMoney('50.00', 'AUD')
except MoneyException:
    print("Something went wrong")

Also, please don't consider these several issue reports as a bad thing; I'm considering to use this package and I want to make sure we don't run into unexpected issues down the line.

@carlospalol
Copy link
Owner

Yes, it could be helpful to separate money-only exceptions.

@carlospalol
Copy link
Owner

Thanks for the suggestion.

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

No branches or pull requests

2 participants