Witajcie
Mam problem odnośnie obsługi błędów w kodzie, który ma za zadanie pobrać plik z połączonego hosta i zapisać go u siebie. Kod wygląda tak:
if self.send_mode == 'ascii':
try:
new_server_file = open(path, 'r')
await self.save_file_content(new_server_file)
except IsADirectoryError:
dir_error_msg = b'remote: ' + path + ': Is a directory.')
await self.loop.sock_sendall(self.control_conn, dir_error_msg)
except PermissionError:
permission_error_msg = b'remote: ' + path + ': Permission denied.'
await self.loop.sock_sendall(self.control_conn, permission_error_msg)
except FileNotFoundError:
not_found_error_msg = b'remote: 550 ' + path + ': No such file or directory.'
await self.loop.sock_sendall(self.control_conn, not_found_error_msg)
else:
try:
new_server_file = open(path, 'rb')
await self.save_file_content(new_server_file)
except IsADirectoryError:
dir_error_msg = b'remote: ' + path + ': Is a directory.')
await self.loop.sock_sendall(self.control_conn, dir_error_msg)
except PermissionError:
permission_error_msg = b'remote: ' + path + ': Permission denied.'
await self.loop.sock_sendall(self.control_conn, permission_error_msg)
except FileNotFoundError:
not_found_error_msg = b'remote: 550 ' + path + ': No such file or directory.'
await self.loop.sock_sendall(self.control_conn, not_found_error_msg)
Jak widać w obsłudze błędów występuje kopiuj-wklej. Muszę obsłużyć wszystkie powyższe błędy, ale nie mam pojęcia jak zastąpić ten skopiowany kod. Czy może to tak zostać czy koniecznie muszę wymyślić coś żeby to poprawić?