babel icon indicating copy to clipboard operation
babel copied to clipboard

Inconsistent exceptions

Open JEphron opened this issue 5 years ago • 0 comments

Based on the exceptions section of the docs, I'd expect format_currency('aaaa', 'usd') to raise a NumberFormatError, but it actually raises decimal.InvalidOperation if you pass it a nonsense string:

>>> format_currency('aaaa', 'usd')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/u/.virtualenvs/v/lib/python3.6/site-packages/babel/numbers.py", line 422, in format_currency
    return pattern.apply(number, locale, currency=currency, force_frac=frac)
  File "/Users/u/.virtualenvs/v/lib/python3.6/site-packages/babel/numbers.py", line 657, in apply
    value = decimal.Decimal(str(value))
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]

This is surprising. I know fixing it would be a breaking change at this point, but maybe something to keep in mind for a future major version.

JEphron avatar Apr 03 '20 00:04 JEphron