Skip to content

Commit f640fbb

Browse files
committed
Fail gracefully if config lacks the mandatory main section
Currently, if a user didn't put `[general]` section in config file, bugwarrior fails like this: Traceback (most recent call last): File "/usr/bin/bugwarrior", line 8, in <module> sys.exit(cli()) ~~~^^ File "/usr/lib/python3.14/site-packages/click/core.py", line 1485, in __call__ return self.main(*args, **kwargs) ~~~~~~~~~^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.14/site-packages/click/core.py", line 1406, in main rv = self.invoke(ctx) File "/usr/lib/python3.14/site-packages/click/core.py", line 1873, in invoke return _process_result(sub_ctx.command.invoke(sub_ctx)) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ File "/usr/lib/python3.14/site-packages/click/core.py", line 1269, in invoke return ctx.invoke(self.callback, **ctx.params) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.14/site-packages/click/core.py", line 824, in invoke return callback(*args, **kwargs) File "/usr/lib/python3.14/site-packages/click/decorators.py", line 34, in new_func return f(get_current_context(), *args, **kwargs) File "/usr/lib/python3.14/site-packages/bugwarrior/command.py", line 62, in wrapped_subcommand_callback return ctx.invoke(subcommand_callback, *args, **kwargs) ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.14/site-packages/click/core.py", line 824, in invoke return callback(*args, **kwargs) File "/usr/lib/python3.14/site-packages/bugwarrior/command.py", line 105, in pull config = _try_load_config(main_section, interactive, quiet) File "/usr/lib/python3.14/site-packages/bugwarrior/command.py", line 35, in _try_load_config return load_config(main_section, interactive, quiet) File "/usr/lib/python3.14/site-packages/bugwarrior/config/load.py", line 122, in load_config rawconfig['flavor'][main_section]['interactive'] = interactive ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ KeyError: 'general' This is confusing — it looks more like bugwarrior broke due to some API change in Python, rather than because of a user mistake. So handle this case with explicit check. Now it will fail instead like this: Validation error found in /home/constantine/.config/bugwarrior/bugwarriorrc See https://bugwarrior.readthedocs.io No section: 'general'
1 parent 5bca612 commit f640fbb

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

bugwarrior/config/load.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ def parse_file(configpath: str) -> dict:
119119
def load_config(main_section, interactive, quiet) -> Config:
120120
configpath = get_config_path()
121121
rawconfig = parse_file(configpath)
122-
rawconfig['flavor'][main_section]['interactive'] = interactive
122+
for section in rawconfig['flavor']:
123+
if isinstance(section, dict):
124+
section['interactive'] = interactive
123125
config = validate_config(rawconfig, main_section, configpath)
124126
configure_logging(
125127
config.main.log_file, 'WARNING' if quiet else config.main.log_level

tests/config/test_load.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,22 @@ def test_ini_wrong_prefix(self):
262262

263263
with self.assertRaises(SystemExit):
264264
load.parse_file(config_path)
265+
266+
267+
class TestLoadConfig(LoadTest):
268+
def test_main_section_does_not_exist(self):
269+
config_path = self.create(".bugwarriorrc")
270+
with open(config_path, 'w') as fout:
271+
fout.write(
272+
textwrap.dedent("""
273+
[redmine]
274+
service = redmine
275+
redmine.url = example.com
276+
""")
277+
)
278+
279+
with self.assertRaises(SystemExit):
280+
load.load_config("general", False, False)
281+
282+
self.assertEqual(len(self.caplog.records), 1)
283+
self.assertIn("No section: 'general'", self.caplog.records[0].message)

0 commit comments

Comments
 (0)