feat(debug): Add function to uniformly render debug OSError output

This commit is contained in:
hygienic-books 2022-07-05 17:22:03 +02:00
parent 0406077aa5
commit 6f5687a98b

View File

@ -473,6 +473,28 @@ def gen_fwd_direct_scaffolding() -> lxml.builder.ElementMaker:
return fw_rule_data return fw_rule_data
def ose_handler(
os_error: OSError,
human_text: str = None,
exit_code: int = None) -> None:
nl = "\n"
log.error(f"{human_text if human_text else exit_code_desc.get(exit_code)}"
f"{nl}Verbatim exception was:\n"
f"{os_error}"
f"""{nl + "Exiting " + str(exit_code) + " ..." if exit_code else ""}""")
def get_xml_str_repr() -> str:
global arg_fw_rule_data
fwd_direct_xml_str = lxml.etree.tostring(arg_fw_rule_data,
pretty_print=True,
encoding="UTF-8",
xml_declaration=True).decode()
return fwd_direct_xml_str
def write_new_fwd_direct_xml( def write_new_fwd_direct_xml(
config_obj: configparser.ConfigParser()) -> bool: config_obj: configparser.ConfigParser()) -> bool:
global arg_fw_rule_data global arg_fw_rule_data
@ -490,10 +512,7 @@ def write_new_fwd_direct_xml(
fwd_file_handle.write(fwd_direct_xml_str) fwd_file_handle.write(fwd_direct_xml_str)
fwd_file_handle.truncate() fwd_file_handle.truncate()
except OSError as ose: except OSError as ose:
log.error(f"Unable to open firewalld direct rules file for updating.\n" ose_handler(os_error=ose, exit_code=9)
f"Verbatim exception was:\n"
f"f{ose}\n"
f"Exiting 9 ...")
sys.exit(9) sys.exit(9)
else: else:
return True return True