Changed error handling, for good I believe

This commit is contained in:
Maximilien Rigaut 2022-06-01 04:12:59 +02:00
parent 70a52d6a13
commit e822c12162
No known key found for this signature in database
GPG Key ID: 4F14B23709ADA5B1
8 changed files with 17 additions and 16 deletions

View File

@ -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__())

View File

@ -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)

View File

@ -112,6 +112,7 @@ _list_classes_expected_value = [
[
context_mngr.HeaderContainer([
context_mngr.TextContainer([
sy.HeaderToken(["#", 'test11']),
sy.TextToken(['test11']),
]),
]),

View File

@ -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

View File

@ -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)

View File

@ -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", "", {}))

View File

@ -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")

View File

@ -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)),