From 5fd28444b61a53722f7f7faa52f9c7fe0e8ec010 Mon Sep 17 00:00:00 2001 From: Javinator9889 Date: Wed, 12 Feb 2020 19:30:22 +0100 Subject: [PATCH] Handle errors in the callback function --- YouTubeMDBot/tests/downloader.py | 13 +++++++++---- YouTubeMDBot/tests/identifier.py | 16 ++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/YouTubeMDBot/tests/downloader.py b/YouTubeMDBot/tests/downloader.py index 72b4b8f..b1c0e00 100755 --- a/YouTubeMDBot/tests/downloader.py +++ b/YouTubeMDBot/tests/downloader.py @@ -22,10 +22,10 @@ def test_multithread_download(self): url="https://www.youtube.com/watch?v=9HfoNUjw5u8") ytdl = MultipleYouTubeDownloader() - ft1 = ytdl.download_async(yt1) - ft2 = ytdl.download_async(yt2) - ft3 = ytdl.download_async(yt3) - ft4 = ytdl.download_async(yt4) + ft1 = ytdl.download_async(yt1, error_callback=handle_error) + ft2 = ytdl.download_async(yt2, error_callback=handle_error) + ft3 = ytdl.download_async(yt3, error_callback=handle_error) + ft4 = ytdl.download_async(yt4, error_callback=handle_error) t1 = threading.Thread(target=self.write_to_file, args=(ft1, "v1.m4a",)) t2 = threading.Thread(target=self.write_to_file, args=(ft2, "v2.m4a",)) @@ -56,5 +56,10 @@ def write_to_file(self, future, name: str): self.barrier() +def handle_error(exception): + print("Unexpected exception: " + str(exception)) + raise exception() + + if __name__ == '__main__': unittest.main() diff --git a/YouTubeMDBot/tests/identifier.py b/YouTubeMDBot/tests/identifier.py index bad5457..a95713f 100755 --- a/YouTubeMDBot/tests/identifier.py +++ b/YouTubeMDBot/tests/identifier.py @@ -53,12 +53,12 @@ def test_multiple_download_identification(self): ytdl = MultipleYouTubeDownloader() - f1 = ytdl.download_async(yt1) - f2 = ytdl.download_async(yt2) - f3 = ytdl.download_async(yt3) - f4 = ytdl.download_async(yt4) - f5 = ytdl.download_async(yt5) - f6 = ytdl.download_async(yt6) + f1 = ytdl.download_async(yt1, error_callback=handle_error) + f2 = ytdl.download_async(yt2, error_callback=handle_error) + f3 = ytdl.download_async(yt3, error_callback=handle_error) + f4 = ytdl.download_async(yt4, error_callback=handle_error) + f5 = ytdl.download_async(yt5, error_callback=handle_error) + f6 = ytdl.download_async(yt6, error_callback=handle_error) t1 = threading.Thread(target=self.find_metadata, args=(f1, yt1,)) t2 = threading.Thread(target=self.find_metadata, args=(f2, yt2,)) @@ -124,5 +124,9 @@ def find_metadata(self, future, downloader) -> Tuple[BytesIO, bytes, dict]: return io, data, song_info +def handle_error(exception): + raise exception() + + if __name__ == '__main__': unittest.main()