From e822c1216264df013c674cf0dfc2c1b7fcecdcda Mon Sep 17 00:00:00 2001 From: Maximilien Rigaut Date: Wed, 1 Jun 2022 04:12:59 +0200 Subject: [PATCH] Changed error handling, for good I believe --- src/bootstraparse/modules/error_mngr.py | 2 +- tests/modules/test_config.py | 8 ++++---- tests/modules/test_context_mngr_export.py | 1 + tests/modules/test_environmemt.py | 4 ++-- tests/modules/test_error_mngr.py | 2 +- tests/modules/test_export.py | 2 +- tests/modules/test_preparser.py | 8 ++++---- tests/modules/test_syntax_context.py | 6 +++--- 8 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/bootstraparse/modules/error_mngr.py b/src/bootstraparse/modules/error_mngr.py index 20c4a28..567c711 100644 --- a/src/bootstraparse/modules/error_mngr.py +++ b/src/bootstraparse/modules/error_mngr.py @@ -76,7 +76,7 @@ def log_exception(exception, level="ERROR"): logging.__getattribute__(level)(' ' + line) if level in ["critical", "error"]: print("An unrecoverable error occurred, please check the log file for more information.") - sys.exit() # could also re-raise the exception + raise exception # Could also re-raise the exception if exception.__class__.__name__ == "ParsingError": logging.error(exception.__str__()) diff --git a/tests/modules/test_config.py b/tests/modules/test_config.py index 09e74fe..343f29e 100644 --- a/tests/modules/test_config.py +++ b/tests/modules/test_config.py @@ -41,7 +41,7 @@ def test_config_load(): assert usr_c["aliases"]["aliases"]["test_alias"] == "test_parser" with pytest.raises(KeyError): assert usr_c["aliases"]["aliases"]["test_alias2"] is not None - with pytest.raises(SystemExit): # Not FileNotFoundError + with pytest.raises(KeyError): # Not FileNotFoundError assert usr_c["aliases2"] is not None assert usr_c.__repr__() @@ -55,7 +55,7 @@ def test_add_to_config(): "parser": {"name": "test_parser", "type": "test_parser"}} assert from_list["parser_config"] == {"parser": {"name": "test_parser", "type": "test_parser"}} - with pytest.raises(SystemExit): # Not FileNotFoundError + with pytest.raises(KeyError): # Not FileNotFoundError assert from_empty["aliases"] from_empty.add_folder(user_conf) assert from_empty["aliases"] == {"aliases": {"test_alias": "test_parser"}} @@ -64,7 +64,7 @@ def test_add_to_config(): assert from_empty["aliases"] == {"aliases": {"test_alias": "test_parser"}, "parser": {"name": "test_parser", "type": "test_parser"}} - with pytest.raises(SystemExit): + with pytest.raises(TypeError): from_empty.load_from_file(os.path.join(bad_conf, "bad_config.yaml")) @@ -74,5 +74,5 @@ def test_contains(): def test_bad_type(): - with pytest.raises(SystemExit): + with pytest.raises(TypeError): config.ConfigLoader(1) diff --git a/tests/modules/test_context_mngr_export.py b/tests/modules/test_context_mngr_export.py index f64ec80..b6a91f5 100644 --- a/tests/modules/test_context_mngr_export.py +++ b/tests/modules/test_context_mngr_export.py @@ -112,6 +112,7 @@ _list_classes_expected_value = [ [ context_mngr.HeaderContainer([ context_mngr.TextContainer([ + sy.HeaderToken(["#", 'test11']), sy.TextToken(['test11']), ]), ]), diff --git a/tests/modules/test_environmemt.py b/tests/modules/test_environmemt.py index 69bef8f..f199613 100644 --- a/tests/modules/test_environmemt.py +++ b/tests/modules/test_environmemt.py @@ -59,7 +59,7 @@ def test_getter(): try: test_check.purposefully_non_existing_parameter - except SystemExit: + except AttributeError: pass except Exception: assert False # Not AttributeError when getting non-existing parameter @@ -86,7 +86,7 @@ def test_setter(): try: test_check.purposefully_non_existing_parameter = "test_value" - except SystemExit: + except AttributeError: pass except Exception: assert False # Not AttributeError when getting non-existing parameter diff --git a/tests/modules/test_error_mngr.py b/tests/modules/test_error_mngr.py index d70baa1..fb57347 100644 --- a/tests/modules/test_error_mngr.py +++ b/tests/modules/test_error_mngr.py @@ -33,7 +33,7 @@ class TestLogging(TestCase): def test_log_exception(self): with self.assertLogs() as captured: - with pytest.raises(SystemExit): + with pytest.raises(Exception): error_mngr.log_exception(Exception(''), level="CRITICAL") self.assertGreaterEqual(len(captured.records), 3) diff --git a/tests/modules/test_export.py b/tests/modules/test_export.py index a53b379..6df9482 100644 --- a/tests/modules/test_export.py +++ b/tests/modules/test_export.py @@ -109,7 +109,7 @@ def test_transform(export_type, export_subtype): def test_get_template_error(): - with pytest.raises(SystemExit): + with pytest.raises(KeyError): em = export.ExportManager("test", "test") em(export.ExportRequest("nope", "header", "", {})) diff --git a/tests/modules/test_preparser.py b/tests/modules/test_preparser.py index 6ab3b0b..9445a0a 100644 --- a/tests/modules/test_preparser.py +++ b/tests/modules/test_preparser.py @@ -322,13 +322,13 @@ def test_errors(): pp = preparser.PreParser("not_existing_file.bprr", env) pp.readlines() - with pytest.raises(SystemExit): # Not ImportError + with pytest.raises(ImportError): # Not ImportError nofile = temp_name("real_file.bpr") make_new_file(nofile, "::< not_existing_file.bpr >") pp = preparser.PreParser(nofile, env) pp.make_import_list() - with pytest.raises(SystemExit): # Not RecursionError + with pytest.raises(RecursionError): # Not RecursionError path = temp_name("test_recursion_error.bpr") make_new_file(path, "::< test_recursion_error.bpr >") pp = preparser.PreParser(path, env) @@ -386,9 +386,9 @@ def test_get_errors(): pp = preparser.PreParser(from_config, env) assert pp.get_element_from_config("aliases", "shortcuts", "do_not_remove_s") == "This is a test" assert pp.get_element_from_config("aliases", "images", "do_not_remove_p") == 'This is a test' - with pytest.raises(SystemExit): + with pytest.raises(KeyError): pp.get_element_from_config("aliases", "not_existing", "do_not_remove_s") - with pytest.raises(SystemExit): + with pytest.raises(KeyError): pp.get_element_from_config("images", "not_existing", "do_not_remove_p") diff --git a/tests/modules/test_syntax_context.py b/tests/modules/test_syntax_context.py index e8bff6a..8be6b73 100644 --- a/tests/modules/test_syntax_context.py +++ b/tests/modules/test_syntax_context.py @@ -9,7 +9,7 @@ _type_counterpart_container = [ (syntax.SemanticType, None, "error", tools.__GLk(1)), (syntax.ExplicitSemanticType, None, "token", tools.__GLk(1)), (syntax.FinalSemanticType, None, "self", tools.__GLk(1)), - (syntax.OpenedSemanticType, None, "self", tools.__GLk(1)), + (syntax.OpenedSemanticType, None, "error", tools.__GLk(1)), (syntax.ClosedSemanticType, None, "error", tools.__GLk(1)), (syntax.EmptySemanticType, None, "self", tools.__GLk(1)), (syntax.UnimplementedToken, None, "error", tools.__GLk(1)), @@ -24,8 +24,8 @@ _type_counterpart_container = [ (syntax.EtOlistToken, None, "self", tools.__GLk(1)), (syntax.HeaderToken, None, "self", tools.__GLk(1)), (syntax.DisplayToken, None, "self", tools.__GLk(1)), - (syntax.StructuralElementStartToken, None, "self", tools.__GLk(1)), - (syntax.StructuralElementEndToken, 'se:start:test', "error", tools.__GLk(1)), + (syntax.StructuralElementStartToken, None, "error", tools.__GLk(1)), # A start can't be in a container (MismatchedContainerError) + (syntax.StructuralElementEndToken, 'se:start:test', "error", tools.__GLk(1)), # An end can't be in a container (MismatchedContainerError) (syntax.HyperlinkToken, None, "error", tools.__GLk(1)), # Shouldn't be an error (syntax.TableToken, None, "error", tools.__GLk(1)), (syntax.TableRowToken, None, "error", tools.__GLk(1)),