snowplow / scala-forex

High-performance Scala library for performing exchange rate lookups and currency conversions
http://snowplow.github.io/scala-forex/
46 stars 12 forks source link

EOD lookup throws exception #27

Closed jz4112 closed 10 years ago

jz4112 commented 10 years ago

throw unavailbleException for eod loopup and conversion with illegal dates

alexanderdean commented 10 years ago

Should switch these to Eithers

jz4112 commented 10 years ago

@alexanderdean In the oerClient class, when I do API request with the invalid date, I used an if statement to decide whether to throw an exception. if (date.isBefore(new DateTime(1999,1,1,0,0)) || date.isAfter(DateTime.now)) { throw new UnavailableExchangeRateException("exchange rate unavailable on the date[%s]".format(date)) }

When I call the method in the main class, I used try catch clause to return a Left with the messge
case (e: UnavailableExchangeRateException) => Left(e.msg)

is this right?

alexanderdean commented 10 years ago

Hi @jz4112 - this is a good start. However, it would be cleaner to update OerClient to return an Either itself, rather than throwing an Exception in OerClient and then converting it to an Either in Forex.

In other words: it's best not to use Exceptions when you can use Eithers.