mirror of
https://github.com/kellyjonbrazil/jc.git
synced 2025-07-13 01:20:24 +02:00
add exception handling for file open errors
This commit is contained in:
26
jc/cli.py
26
jc/cli.py
@ -434,10 +434,12 @@ def magic_parser(args):
|
||||
options # jc options to preserve
|
||||
)
|
||||
|
||||
|
||||
def open_text_file(path_string):
|
||||
with open(path_string, 'r') as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def run_user_command(command):
|
||||
"""
|
||||
Use subprocess to run the user's command. Returns the STDOUT, STDERR,
|
||||
@ -597,8 +599,28 @@ def main():
|
||||
run_command_str = ' '.join(run_command) # older python versions
|
||||
|
||||
if run_command_str.startswith('/proc'):
|
||||
magic_found_parser = 'proc'
|
||||
magic_stdout = open_text_file(run_command_str)
|
||||
try:
|
||||
magic_found_parser = 'proc'
|
||||
magic_stdout = open_text_file(run_command_str)
|
||||
|
||||
except OSError as e:
|
||||
if debug:
|
||||
raise
|
||||
|
||||
error_msg = os.strerror(e.errno)
|
||||
utils.error_message([
|
||||
f'"{run_command_str}" file could not be opened: {error_msg}.'
|
||||
])
|
||||
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
|
||||
|
||||
except Exception:
|
||||
if debug:
|
||||
raise
|
||||
|
||||
utils.error_message([
|
||||
f'"{run_command_str}" file could not be opened. For details use the -d or -dd option.'
|
||||
])
|
||||
sys.exit(combined_exit_code(magic_exit_code, JC_ERROR_EXIT))
|
||||
|
||||
elif valid_command:
|
||||
try:
|
||||
|
Reference in New Issue
Block a user