Skip to content

Commit 805fe08

Browse files
AJMansfielddpgeorge
authored andcommitted
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 <[email protected]>
1 parent cee0419 commit 805fe08

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

tools/mpremote/mpremote/commands.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,8 @@ def do_filesystem(state, args):
393393
)
394394
else:
395395
do_filesystem_cp(state, path, cp_dest, len(paths) > 1, not args.force)
396-
except FileNotFoundError as er:
397-
raise CommandError("{}: {}: No such file or directory.".format(command, er.args[0]))
398-
except IsADirectoryError as er:
399-
raise CommandError("{}: {}: Is a directory.".format(command, er.args[0]))
400-
except FileExistsError as er:
401-
raise CommandError("{}: {}: File exists.".format(command, er.args[0]))
396+
except OSError as er:
397+
raise CommandError("{}: {}: {}.".format(command, er.strerror, os.strerror(er.errno)))
402398
except TransportError as er:
403399
raise CommandError("Error with transport:\n{}".format(er.args[0]))
404400

tools/mpremote/mpremote/transport.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,10 @@ def __init__(self, status_code, error_output):
5555
# Takes a Transport error (containing the text of an OSError traceback) and
5656
# raises it as the corresponding OSError-derived exception.
5757
def _convert_filesystem_error(e, info):
58-
if "OSError" in e.error_output and "ENOENT" in e.error_output:
59-
return FileNotFoundError(info)
60-
if "OSError" in e.error_output and "EISDIR" in e.error_output:
61-
return IsADirectoryError(info)
62-
if "OSError" in e.error_output and "EEXIST" in e.error_output:
63-
return FileExistsError(info)
64-
if "OSError" in e.error_output and "ENODEV" in e.error_output:
65-
return FileNotFoundError(info)
66-
if "OSError" in e.error_output and "EINVAL" in e.error_output:
67-
return OSError(errno.EINVAL, info)
68-
if "OSError" in e.error_output and "EPERM" in e.error_output:
69-
return OSError(errno.EPERM, info)
58+
if "OSError" in e.error_output:
59+
for code, estr in errno.errorcode.items():
60+
if estr in e.error_output:
61+
return OSError(code, info)
7062
return e
7163

7264

0 commit comments

Comments
 (0)