Skip to content

Commit

Permalink
Print more helpful errors on config parsing failure
Browse files Browse the repository at this point in the history
Signed-off-by: Jeremy Cline <jcline@redhat.com>
  • Loading branch information
jeremycline committed Aug 3, 2018
1 parent cc889c9 commit f8c1933
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
5 changes: 3 additions & 2 deletions fedora_messaging/config.py
Expand Up @@ -404,8 +404,9 @@ def load_config(self, config_path=None):
for key in file_config:
config[key.lower()] = file_config[key]
except pytoml.core.TomlError as e:
_log.error('Failed to parse {}: {}'.format(config_path, str(e)))
raise exceptions.ConfigurationException(e)
msg = 'Failed to parse {}: error at line {}, column {}'.format(
config_path, e.line, e.col)
raise exceptions.ConfigurationException(msg)
else:
_log.info('The configuration file, {}, does not exist.'.format(config_path))

Expand Down
6 changes: 6 additions & 0 deletions fedora_messaging/exceptions.py
Expand Up @@ -8,6 +8,12 @@ class BaseException(Exception):
class ConfigurationException(BaseException):
"""Raised when there's an invalid configuration setting"""

def __init__(self, message):
self.message = message

def __str__(self):
return 'Configuration error: ' + self.message


class PublishException(BaseException):
"""Base class for exceptions related to publishing."""
Expand Down
12 changes: 5 additions & 7 deletions fedora_messaging/tests/unit/test_config.py
Expand Up @@ -120,15 +120,13 @@ def test_invalid_key(self, mock_exists):
self.assertRaises(ConfigurationException, config.load_config)

@mock.patch('fedora_messaging.config.open', mock.mock_open(read_data='Ni!'))
@mock.patch('fedora_messaging.config._log', autospec=True)
@mock.patch('fedora_messaging.config.os.path.exists', return_value=True)
def test_bad_config_file(self, mock_exists, mock_log):
def test_bad_config_file(self, mock_exists):
"""Assert an invalid TOML file raises a ConfigurationException."""
self.assertRaises(ConfigurationException, msg_config.LazyConfig().load_config)
mock_log.info.assert_called_once_with(
'Loading configuration from /etc/fedora-messaging/config.toml')
error = 'Failed to parse /etc/fedora-messaging/config.toml: <string>(1, 1): msg'
self.assertEqual(error, mock_log.error.call_args_list[0][0][0])
with self.assertRaises(ConfigurationException) as cm:
msg_config.LazyConfig().load_config()
error = 'Failed to parse /etc/fedora-messaging/config.toml: error at line 1, column 1'
self.assertEqual(error, cm.exception.message)

@mock.patch('fedora_messaging.config.open', mock.mock_open(read_data=partial_config))
@mock.patch('fedora_messaging.config._log', autospec=True)
Expand Down

0 comments on commit f8c1933

Please sign in to comment.