Skip to content

Commit a033cb1

Browse files
authored
Merge pull request #2053 from sopel-irc/currency-KeyError
currency: improve error handling
2 parents ed2b825 + cec1500 commit a033cb1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

sopel/modules/currency.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ def exchange(bot, match):
9797
bot.reply("Something went wrong while I was getting the exchange rate.")
9898
LOGGER.error("Error in GET request: {}".format(err))
9999
return
100-
except ValueError:
101-
bot.reply("Error: Got malformed data.")
102-
LOGGER.error("Invalid json on update_rates")
100+
except (KeyError, ValueError) as err:
101+
bot.reply("Error: Could not update exchange rates. Try again later.")
102+
LOGGER.error("{} on update_rates".format(
103+
'Invalid JSON' if type(err).__name__ == 'ValueError' else 'Missing JSON value',
104+
))
103105
return
104106
except FixerError as err:
105107
bot.reply('Sorry, something went wrong with Fixer')
@@ -191,7 +193,6 @@ def update_rates(bot):
191193

192194
response.raise_for_status()
193195
rates_fiat = response.json()
194-
rates_updated = time.time()
195196

196197
rates = rates_fiat['rates']
197198
rates['EUR'] = 1.0 # Put this here to make logic easier
@@ -202,6 +203,10 @@ def update_rates(bot):
202203
if rate.upper() not in rates:
203204
rates[rate.upper()] = rates_crypto['rates'][rate]['value'] * eur_btc_rate
204205

206+
# if an error aborted the operation prematurely, we want the next call to retry updating rates
207+
# therefore we'll update the stored timestamp at the last possible moment
208+
rates_updated = time.time()
209+
205210

206211
@plugin.command('cur', 'currency', 'exchange')
207212
@plugin.example('.cur 100 usd in btc cad eur',

0 commit comments

Comments
 (0)