Use FileNotFoundError instead of DeviceError when file/folder is not found

This commit is contained in:
Kovid Goyal 2025-01-21 07:45:49 +05:30
parent 77936feb22
commit 9ab99b34c2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 6 additions and 4 deletions

View File

@ -436,7 +436,7 @@ class MTP_DEVICE(MTPDeviceBase):
parent_id = self.libmtp.LIBMTP_FILES_AND_FOLDERS_ROOT if parent.is_storage else parent.object_id parent_id = self.libmtp.LIBMTP_FILES_AND_FOLDERS_ROOT if parent.is_storage else parent.object_id
x = self.dev.list_folder_by_name(parent.storage_id, parent_id, names) x = self.dev.list_folder_by_name(parent.storage_id, parent_id, names)
if x is None: if x is None:
raise DeviceError(f'Could not find folder named: {"/".join(names)} in {parent.full_path}') raise FileNotFoundError(f'Could not find folder named: {"/".join(names)} in {parent.full_path}')
return x return x
@synchronous @synchronous
@ -462,7 +462,7 @@ class MTP_DEVICE(MTPDeviceBase):
parent_id = self.libmtp.LIBMTP_FILES_AND_FOLDERS_ROOT if parent.is_storage else parent.object_id parent_id = self.libmtp.LIBMTP_FILES_AND_FOLDERS_ROOT if parent.is_storage else parent.object_id
x = self.dev.get_file_by_name(parent.storage_id, parent_id, names, stream, callback) x = self.dev.get_file_by_name(parent.storage_id, parent_id, names, stream, callback)
if x is None: if x is None:
raise DeviceError(f'Could not find file named: {"/".join(names)} in {parent.full_path}') raise FileNotFoundError(f'Could not find file named: {"/".join(names)} in {parent.full_path}')
ok, errs = x ok, errs = x
if not ok: if not ok:
raise DeviceError(f'Failed to get file: {"/".join(names)} in {parent.full_path} with errors: {self.format_errorstack(errs)}') raise DeviceError(f'Failed to get file: {"/".join(names)} in {parent.full_path} with errors: {self.format_errorstack(errs)}')

View File

@ -398,7 +398,7 @@ class MTP_DEVICE(MTPDeviceBase):
raise ValueError(f'{parent.full_path} is not a folder') raise ValueError(f'{parent.full_path} is not a folder')
x = self.dev.list_folder_by_name(parent.object_id, names) x = self.dev.list_folder_by_name(parent.object_id, names)
if x is None: if x is None:
raise DeviceError(f'Could not find folder named: {"/".join(names)} in {parent.full_path}') raise FileNotFoundError(f'Could not find folder named: {"/".join(names)} in {parent.full_path}')
return list(x.values()) return list(x.values())
@same_thread @same_thread
@ -423,8 +423,10 @@ class MTP_DEVICE(MTPDeviceBase):
except self.wpd.WPDFileBusy: except self.wpd.WPDFileBusy:
time.sleep(2) time.sleep(2)
self.dev.get_file_by_name(parent.object_id, names, stream, callback) self.dev.get_file_by_name(parent.object_id, names, stream, callback)
except KeyError as e:
raise FileNotFoundError(f'Failed to find the file {os.sep.join(names)} in {parent.full_path}') from e
except Exception as e: except Exception as e:
raise DeviceError(f'Failed to fetch the file {os.sep.join(names)} from {parent.full_path} with error: {as_unicode(e)}') raise DeviceError(f'Failed to fetch the file {os.sep.join(names)} in {parent.full_path} with error: {as_unicode(e)}')
stream.seek(0) stream.seek(0)
if set_name: if set_name:
stream.name = '/'.join(names) stream.name = '/'.join(names)