diff options
author | Anson Mansfield <amansfield@mantaro.com> | 2025-04-07 10:08:36 -0400 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2025-04-26 15:50:00 +1000 |
commit | 805fe083a3dea76b0acd36ecc436c2b76b808059 (patch) | |
tree | 6914c32792a15bf80246aadd888edd9aab72f49e | |
parent | cee0419021396a87bd38bb3231b06311f604fad4 (diff) | |
download | micropython-805fe083a3dea76b0acd36ecc436c2b76b808059.tar.gz micropython-805fe083a3dea76b0acd36ecc436c2b76b808059.zip |
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>
-rw-r--r-- | tools/mpremote/mpremote/commands.py | 8 | ||||
-rw-r--r-- | tools/mpremote/mpremote/transport.py | 16 |
2 files changed, 6 insertions, 18 deletions
diff --git a/tools/mpremote/mpremote/commands.py b/tools/mpremote/mpremote/commands.py index 1e13b33afe..b1e5d3c7ed 100644 --- a/tools/mpremote/mpremote/commands.py +++ b/tools/mpremote/mpremote/commands.py @@ -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])) diff --git a/tools/mpremote/mpremote/transport.py b/tools/mpremote/mpremote/transport.py index 8d30c7f517..df8ef209ae 100644 --- a/tools/mpremote/mpremote/transport.py +++ b/tools/mpremote/mpremote/transport.py @@ -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 |