alexprengere / currencyconverter

A Python currency converter using the European Central Bank data.
http://alexprengere.github.io/currencyconverter
Apache License 2.0
215 stars 59 forks source link

unhandled KeyError exception with unsupported currency code #9

Closed michaelDovgal closed 6 years ago

michaelDovgal commented 6 years ago

Passing to convert function of a library currency code that is not supported by library, have a KeyError as a result.

Problem is here - [0], using currency code without its validation.

Need to add some special exception that this currency is not supported or something like this.

Can do a pull request if necessary.

[0] - https://github.com/alexprengere/currencyconverter/blob/master/currency_converter/currency_converter.py#L262

alexprengere commented 6 years ago

You are right, there is an asymmetry between currency and new_currency here:

>>> c = CurrencyConverter()
>>> c.convert(100, '000', 'EUR')
Traceback (most recent call last):
...
ValueError: 000 is not a supported currency
>>> c.convert(100, 'EUR', '000')
Traceback (most recent call last):
...
KeyError: '000'

499bfa3 should fix this, both cases will raise ValueError. Note that you may test the currency validity beforehand with:

>>> 'USD' in c.currencies
True

Thanks!