@@ -662,3 +662,62 @@ def download(cfg: SimpleConfig):
662662 sys .stdout = old_stdout
663663
664664 assert "train:" in output or "data:" in output
665+
666+
667+ class TestErrorHandling :
668+ """Tests for graceful error handling."""
669+
670+ def test_invalid_override_key_in_run (self ):
671+ """Test that invalid config keys show helpful error."""
672+ from nanocli .config import ConfigError , compile_config
673+
674+ with pytest .raises (ConfigError , match = "Invalid config key" ):
675+ compile_config (schema = SimpleConfig , overrides = ["invalid_key=value" ])
676+
677+ def test_invalid_nested_key (self ):
678+ """Test that invalid nested keys are caught."""
679+ from nanocli .config import ConfigError , compile_config
680+
681+ with pytest .raises (ConfigError , match = "Invalid config key" ):
682+ compile_config (schema = ParentConfig , overrides = ["child.invalid=value" ])
683+
684+ def test_run_with_invalid_key_exits (self ):
685+ """Test that run() exits gracefully with invalid key."""
686+ with pytest .raises (SystemExit ):
687+ run (SimpleConfig , args = ["invalid_key=value" ])
688+
689+ def test_nanocli_command_with_invalid_key (self ):
690+ """Test NanoCLI command with invalid key shows error and help."""
691+ app = NanoCLI ()
692+
693+ @app .command ()
694+ def train (cfg : SimpleConfig ):
695+ pass
696+
697+ with pytest .raises (SystemExit ):
698+ old_stdout = sys .stdout
699+ sys .stdout = io .StringIO ()
700+ try :
701+ app (["train" , "invalid_key=value" ])
702+ finally :
703+ sys .stdout = old_stdout
704+
705+
706+ class TestConfigErrorMessages :
707+ """Tests for config error message formatting."""
708+
709+ def test_parse_overrides_no_equals (self ):
710+ """Test that missing = raises ConfigError."""
711+ from nanocli .config import ConfigError , parse_overrides
712+
713+ with pytest .raises (ConfigError , match = "Invalid override" ):
714+ parse_overrides (["no_equals_sign" ])
715+
716+ def test_config_error_message_has_key (self ):
717+ """Test that error message contains the invalid key."""
718+ from nanocli .config import ConfigError , compile_config
719+
720+ try :
721+ compile_config (schema = SimpleConfig , overrides = ["typo_key=value" ])
722+ except ConfigError as e :
723+ assert "typo_key" in str (e )
0 commit comments