From 5b3fab666d70c70e88101bc4bacd6fc3d2202dad Mon Sep 17 00:00:00 2001 From: kansi Date: Fri, 16 Mar 2018 11:47:15 +0530 Subject: [PATCH] Problem: Unkown exceptions being handled Solution: Handle only known exceptions --- bigchaindb/tendermint/lib.py | 5 +++-- bigchaindb/web/views/validators.py | 12 +++--------- tests/web/test_validators.py | 8 +++++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/bigchaindb/tendermint/lib.py b/bigchaindb/tendermint/lib.py index 5a7713a1..a48cfa25 100644 --- a/bigchaindb/tendermint/lib.py +++ b/bigchaindb/tendermint/lib.py @@ -253,8 +253,9 @@ class BigchainDB(Bigchain): return validators - except Exception: - raise Exception('Error while processing data from tendermint.') + except requests.exceptions.RequestException as e: + logger.error('Error while connecting to Tendermint HTTP API') + raise e Block = namedtuple('Block', ('app_hash', 'height', 'transactions')) diff --git a/bigchaindb/web/views/validators.py b/bigchaindb/web/views/validators.py index 4afb46bc..825b0c7d 100644 --- a/bigchaindb/web/views/validators.py +++ b/bigchaindb/web/views/validators.py @@ -1,8 +1,6 @@ from flask import current_app from flask_restful import Resource -from bigchaindb.web.views.base import make_error - class ValidatorsApi(Resource): def get(self): @@ -14,11 +12,7 @@ class ValidatorsApi(Resource): pool = current_app.config['bigchain_pool'] - try: - with pool() as bigchain: - validators = bigchain.get_validators() + with pool() as bigchain: + validators = bigchain.get_validators() - return validators - - except Exception: - return make_error(500) + return validators diff --git a/tests/web/test_validators.py b/tests/web/test_validators.py index d71e30a9..7a0e20ce 100644 --- a/tests/web/test_validators.py +++ b/tests/web/test_validators.py @@ -1,5 +1,7 @@ import pytest +from requests.exceptions import RequestException + pytestmark = pytest.mark.tendermint VALIDATORS_ENDPOINT = '/api/v1/validators/' @@ -20,11 +22,11 @@ def test_get_validators_endpoint(b, client, monkeypatch): def test_get_validators_500_endpoint(b, client, monkeypatch): def mock_get(uri): - return 'InvalidResponse' + raise RequestException monkeypatch.setattr('requests.get', mock_get) - res = client.get(VALIDATORS_ENDPOINT) - assert res.status_code == 500 + with pytest.raises(RequestException): + client.get(VALIDATORS_ENDPOINT) # Helper