diff --git a/bigchaindb/config_utils.py b/bigchaindb/config_utils.py index 81f8ed46..e1dcbdc9 100644 --- a/bigchaindb/config_utils.py +++ b/bigchaindb/config_utils.py @@ -22,6 +22,7 @@ from pkg_resources import iter_entry_points, ResolutionError import bigchaindb from bigchaindb.consensus import AbstractConsensusRules +from bigchaindb import exceptions # TODO: move this to a proper configuration file for logging logging.getLogger('requests').setLevel(logging.WARNING) @@ -98,7 +99,12 @@ def file_config(filename=None): logger.debug('file_config() will try to open `{}`'.format(filename)) with open(filename) as f: - config = json.load(f) + try: + config = json.load(f) + except ValueError as err: + raise exceptions.ConfigurationError( + 'Failed to parse the JSON configuration from `{}`, {}'.format(filename, err) + ) logger.info('Configuration loaded from `{}`'.format(filename)) diff --git a/bigchaindb/exceptions.py b/bigchaindb/exceptions.py index 480a0bd8..7b652fa3 100644 --- a/bigchaindb/exceptions.py +++ b/bigchaindb/exceptions.py @@ -1,6 +1,9 @@ """Custom exceptions used in the `bigchaindb` package. """ +class ConfigurationError(Exception): + """Raised when there is a problem with server configuration""" + class OperationError(Exception): """Raised when an operation cannot go through""" diff --git a/tests/test_config_utils.py b/tests/test_config_utils.py index ea58f61a..bce2c9a8 100644 --- a/tests/test_config_utils.py +++ b/tests/test_config_utils.py @@ -229,6 +229,13 @@ def test_file_config(): assert config == {} +def test_invalid_file_config(): + from bigchaindb.config_utils import file_config, CONFIG_DEFAULT_PATH + with patch('builtins.open', mock_open(read_data='{_INVALID_JSON_}')) as m: + with pytest.raises(exceptions.ConfigurationError): + file_config() + + def test_write_config(): from bigchaindb.config_utils import write_config, CONFIG_DEFAULT_PATH m = mock_open()