Repository: rotki/rotki

Issue:
Remote Error not handled in aave balance query
## Problem Definition

Seems like an exception was thrown in tests during an aave balance query due to the inability to reach any of the open nodes during a Travis test.

```
[14/09/2020 08:28:44 UTC] rotkehlchen.api.server: Exception on /api/1/blockchains/ETH/modules/aave/balances [GET]
Traceback (most recent call last):
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/flask_restful/__init__.py", line 458, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/flask_restful/__init__.py", line 573, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/webargs/core.py", line 366, in wrapper
    return func(*args, **kwargs)
  File "/home/travis/build/rotki/rotki/rotkehlchen/api/v1/resources.py", line 824, in get
    return self.rest_api.get_aave_balances(async_query)
  File "/home/travis/build/rotki/rotki/rotkehlchen/api/rest.py", line 125, in wrapper
    return f(wrappingobj, *args, **kwargs)
  File "/home/travis/build/rotki/rotki/rotkehlchen/api/rest.py", line 1534, in get_aave_balances
    given_defi_balances=lambda: self.rotkehlchen.chain_manager.defi_balances,
  File "/home/travis/build/rotki/rotki/rotkehlchen/api/rest.py", line 1481, in _api_query_for_eth_module
    **kwargs,
  File "/home/travis/build/rotki/rotki/rotkehlchen/api/rest.py", line 1442, in _eth_module_query
    self.rotkehlchen.chain_manager.query_balances(blockchain=SupportedBlockchain.ETHEREUM)
  File "/home/travis/build/rotki/rotki/rotkehlchen/utils/interfaces.py", line 111, in wrapper
    result = f(wrappingobj, *args, **kwargs)
  File "/home/travis/build/rotki/rotki/rotkehlchen/utils/interfaces.py", line 70, in wrapper
    result = f(wrappingobj, *args, **kwargs)
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/manager.py", line 351, in query_balances
    self.query_ethereum_balances(force_token_detection=force_token_detection)
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/manager.py", line 872, in query_ethereum_balances
    self.query_defi_balances()
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/manager.py", line 838, in query_defi_balances
    balances = zerion.all_balances_for_account(account)
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/zerion.py", line 163, in all_balances_for_account
    base_balance = self._get_single_balance(protocol.name, balances[0])
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/zerion.py", line 202, in _get_single_balance
    token_name=token_name,
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/zerion.py", line 243, in handle_protocols
    usd_price = handle_defi_price_query(self.ethereum, token_symbol, underlying_asset_price)
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/defi.py", line 176, in handle_defi_price_query
    asset_price=underlying_asset_price,
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/defi.py", line 101, in _handle_curvepool_price
    arguments=[],
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/manager.py", line 552, in call_contract
    arguments=arguments,
  File "/home/travis/build/rotki/rotki/rotkehlchen/chain/ethereum/manager.py", line 308, in query
    f'Failed to query {str(callable)} after trying the following '
rotkehlchen.errors.RemoteError: Failed to query <built-in function callable> after trying the following nodes: ['blockscout', 'etherscan', 'mycrypto', 'avado pool']
[14/09/2020 08:28:44 UTC] rotkehlchen.api.server.pywsgi: 127.0.0.1 - - "GET /api/1/blockchains/ETH/modules/aave/balances HTTP/1.1" 500 164 22.546647
```

## Task

Such an error should be handled properly. We need to catch the remote error. Also that's quite rare ... that all possible ethereum nodes can't be contacted. Have not seen it before. Perhaps a networking hiccup in Travis?


Discussion:

Resolving patch:
diff --git a/rotkehlchen/api/rest.py b/rotkehlchen/api/rest.py
--- a/rotkehlchen/api/rest.py
+++ b/rotkehlchen/api/rest.py
@@ -1437,9 +1437,15 @@ def _eth_module_query(
         status_code = HTTPStatus.OK
 
         if query_specific_balances_before and 'defi' in query_specific_balances_before:
+
             # Make sure ethereum balances are queried (this is protected by lock and by time cache)
             # so most of the times it should have already ran
-            self.rotkehlchen.chain_manager.query_balances(blockchain=SupportedBlockchain.ETHEREUM)
+            try:
+                self.rotkehlchen.chain_manager.query_balances(
+                    blockchain=SupportedBlockchain.ETHEREUM,
+                )
+            except (RemoteError, EthSyncError) as e:
+                return {'result': None, 'message': str(e), 'status_code': HTTPStatus.BAD_GATEWAY}
 
         module_obj = getattr(self.rotkehlchen.chain_manager, module)
         if module_obj is None:


Test patch: