Skip to content

Commit

Permalink
gui: app crash when fetch_more failed (#761)
Browse files Browse the repository at this point in the history
Before, it only catches ProviderIOError.
Now, it catches ProviderIOError and Exception (by removing duplicated code).
  • Loading branch information
cosven authored Jan 13, 2024
1 parent b155c50 commit 901f9a3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/source/coding_style.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
错误处理
------------

- Qt 中同步调用资源提供方接口时,都应该处理 Exception 异常,否则应用可能会 crash

日志和提示
-----------

Expand Down
31 changes: 17 additions & 14 deletions feeluown/gui/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
except ImportError:
pass

from feeluown.utils import aio
from feeluown.utils.aio import run_afn, run_fn
from feeluown.utils.reader import AsyncReader, Reader
from feeluown.utils.typing_ import Protocol
from feeluown.excs import ProviderIOError, ResourceNotFound
Expand Down Expand Up @@ -240,7 +240,9 @@ def adjust_height(self):
# qt will trigger fetchMore when the last row is
# inside the viewport, so we always hide the last
# two row to ensure fetch-more will not be
# triggered automatically
# triggered automatically.
#
# qt triggers fetchMore when user scrolls down to bottom.
index = self._last_visible_index()
rect = self.visualRect(index)
height = self.sizeHint().height() - int(rect.height() * 1.5) - \
Expand Down Expand Up @@ -425,17 +427,11 @@ async def fetch():
if count == step:
break
return items
future = aio.create_task(fetch())
future.add_done_callback(self._async_fetch_cb)
task = run_afn(fetch)
else:
assert isinstance(reader, Reader)
try:
items = reader.read_range(self.rowCount(), step + self.rowCount())
except ProviderIOError:
logger.exception('fetch more items failed')
self._fetch_more_cb(None)
else:
self._fetch_more_cb(items)
task = run_fn(reader.read_range, self.rowCount(), step + self.rowCount())
task.add_done_callback(self._async_fetch_cb)

def on_items_fetched(self: ModelUsingReader[T], items: List[T]):
begin = len(self._items)
Expand All @@ -452,10 +448,17 @@ def _fetch_more_cb(self: ModelUsingReader[T], items: Optional[List[T]]):
def _async_fetch_cb(self: ModelUsingReader[T], future):
try:
items = future.result()
except ProviderIOError as e:
logger.error(f'async fetch more items failed, reason: {e}')
self._fetch_more_cb(None)
except: # noqa
logger.exception('async fetch more items failed')
self._fetch_more_cb(None)
else:
if not items:
# The reader should not return empty list when fetching more items,
# maybe something wrong with the reader.
logger.warning('async fetch more items return empty list')
self._fetch_more_cb(items)


Expand Down Expand Up @@ -490,7 +493,7 @@ async def fetch_song_pic_url(model, cb):
# If the song is a v1 model, just fallback to use its album cover.
if not is_v2_model:
try:
upgraded_song = await aio.run_fn(library.song_upgrade, model)
upgraded_song = await run_fn(library.song_upgrade, model)
except NotSupported:
cb(None)
else:
Expand All @@ -510,7 +513,7 @@ async def fetch_song_pic_url(model, cb):

# Image is not in cache.
try:
upgraded_song = await aio.run_fn(library.song_upgrade, model)
upgraded_song = await run_fn(library.song_upgrade, model)
except (NotSupported, ResourceNotFound):
cb(None)
else:
Expand Down Expand Up @@ -540,7 +543,7 @@ async def fetch_other_model_cover(model, cb):
return

try:
img_url = await aio.run_fn(library.model_get_cover, model)
img_url = await run_fn(library.model_get_cover, model)
except NotSupported:
img_url = ''

Expand Down

0 comments on commit 901f9a3

Please sign in to comment.