From 3f53e67c65ac80bfd2a068b179a007edca1bb4a2 Mon Sep 17 00:00:00 2001 From: David Gasparian Date: Sun, 7 Aug 2016 23:23:40 +0400 Subject: [PATCH 1/4] Add some exception handling to file_config() config loader --- bigchaindb/config_utils.py | 8 ++++++-- bigchaindb/exceptions.py | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/bigchaindb/config_utils.py b/bigchaindb/config_utils.py index 7d469504..3673d95e 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) @@ -97,8 +98,11 @@ def file_config(filename=None): filename = CONFIG_DEFAULT_PATH logger.debug('file_config() will try to open `{}`'.format(filename)) - with open(filename) as f: - config = json.load(f) + try: + with open(filename) as f: + config = json.load(f) + except Exception as err: + raise exceptions.ConfigurationError('Failed to load configuration from `{}`, reason: {}'.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""" From f8c1d526436ddd1ed3ab0c9512e611f97c016784 Mon Sep 17 00:00:00 2001 From: David Gasparian Date: Mon, 8 Aug 2016 17:25:29 +0400 Subject: [PATCH 2/4] Added test case for invalid config file exception handling --- tests/test_config_utils.py | 7 +++++++ 1 file changed, 7 insertions(+) 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() From 1b33a43b77638a27e68cbb5c2782adb1b9126f52 Mon Sep 17 00:00:00 2001 From: David Gasparian Date: Mon, 15 Aug 2016 19:52:30 +0400 Subject: [PATCH 3/4] Make file config to handle only JSON errors --- bigchaindb/config_utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bigchaindb/config_utils.py b/bigchaindb/config_utils.py index a4ffead1..e861eb1e 100644 --- a/bigchaindb/config_utils.py +++ b/bigchaindb/config_utils.py @@ -98,11 +98,13 @@ def file_config(filename=None): filename = CONFIG_DEFAULT_PATH logger.debug('file_config() will try to open `{}`'.format(filename)) - try: - with open(filename) as f: + with open(filename) as f: + try: config = json.load(f) - except Exception as err: - raise exceptions.ConfigurationError('Failed to load configuration from `{}`, reason: {}'.format(filename, err)) + except ValueError as err: + raise exceptions.ConfigurationError( + 'Failed to load configuration from `{}`, reason: {}'.format(filename, err) + ) logger.info('Configuration loaded from `{}`'.format(filename)) From 6baa9b479adcd9977bd6bac15473a85a5475ba5f Mon Sep 17 00:00:00 2001 From: David Gasparian Date: Thu, 18 Aug 2016 22:19:22 +0400 Subject: [PATCH 4/4] Changed config parse exception text --- bigchaindb/config_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigchaindb/config_utils.py b/bigchaindb/config_utils.py index e861eb1e..e1dcbdc9 100644 --- a/bigchaindb/config_utils.py +++ b/bigchaindb/config_utils.py @@ -103,7 +103,7 @@ def file_config(filename=None): config = json.load(f) except ValueError as err: raise exceptions.ConfigurationError( - 'Failed to load configuration from `{}`, reason: {}'.format(filename, err) + 'Failed to parse the JSON configuration from `{}`, {}'.format(filename, err) ) logger.info('Configuration loaded from `{}`'.format(filename))