tools/mpremote: Refactor error handling to apply generally to any errno.
This rewrites the code that previously manually emitted and caught various OSError subclasses with equivalent code that uses the errno name dictionary to do this generically, and updates the exception handler in do_filesystem to catch them in a similarly-generic fashion using os.strerror to retrieve an appropriate error message text equivalent to the current messages. Note that in the CPython environments where mpremote runs, the call to the OSError constructor already returns an instance of the corresponding mapped exception subtype. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
This commit is contained in:
committed by
Damien George
parent
cee0419021
commit
805fe083a3
@@ -393,12 +393,8 @@ def do_filesystem(state, args):
|
||||
)
|
||||
else:
|
||||
do_filesystem_cp(state, path, cp_dest, len(paths) > 1, not args.force)
|
||||
except FileNotFoundError as er:
|
||||
raise CommandError("{}: {}: No such file or directory.".format(command, er.args[0]))
|
||||
except IsADirectoryError as er:
|
||||
raise CommandError("{}: {}: Is a directory.".format(command, er.args[0]))
|
||||
except FileExistsError as er:
|
||||
raise CommandError("{}: {}: File exists.".format(command, er.args[0]))
|
||||
except OSError as er:
|
||||
raise CommandError("{}: {}: {}.".format(command, er.strerror, os.strerror(er.errno)))
|
||||
except TransportError as er:
|
||||
raise CommandError("Error with transport:\n{}".format(er.args[0]))
|
||||
|
||||
|
||||
@@ -55,18 +55,10 @@ listdir_result = namedtuple("dir_result", ["name", "st_mode", "st_ino", "st_size
|
||||
# Takes a Transport error (containing the text of an OSError traceback) and
|
||||
# raises it as the corresponding OSError-derived exception.
|
||||
def _convert_filesystem_error(e, info):
|
||||
if "OSError" in e.error_output and "ENOENT" in e.error_output:
|
||||
return FileNotFoundError(info)
|
||||
if "OSError" in e.error_output and "EISDIR" in e.error_output:
|
||||
return IsADirectoryError(info)
|
||||
if "OSError" in e.error_output and "EEXIST" in e.error_output:
|
||||
return FileExistsError(info)
|
||||
if "OSError" in e.error_output and "ENODEV" in e.error_output:
|
||||
return FileNotFoundError(info)
|
||||
if "OSError" in e.error_output and "EINVAL" in e.error_output:
|
||||
return OSError(errno.EINVAL, info)
|
||||
if "OSError" in e.error_output and "EPERM" in e.error_output:
|
||||
return OSError(errno.EPERM, info)
|
||||
if "OSError" in e.error_output:
|
||||
for code, estr in errno.errorcode.items():
|
||||
if estr in e.error_output:
|
||||
return OSError(code, info)
|
||||
return e
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user