🎨 Better Exception-Handling for AvatarListPlugin.get_final_data

This commit is contained in:
洛水居室 2022-12-27 20:42:41 +08:00
parent 620b5afc8d
commit f5f8735e88
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -2,8 +2,9 @@
import asyncio import asyncio
from typing import Iterable, List, Optional, Sequence from typing import Iterable, List, Optional, Sequence
from aiohttp import ClientConnectorError
from arkowrapper import ArkoWrapper from arkowrapper import ArkoWrapper
from enkanetwork import Assets as EnkaAssets, EnkaNetworkAPI from enkanetwork import Assets as EnkaAssets, EnkaNetworkAPI, VaildateUIDError, UIDNotFounded, HTTPException
from genshin import Client, GenshinException, InvalidCookies from genshin import Client, GenshinException, InvalidCookies
from genshin.models import CalculatorCharacterDetails, CalculatorTalent, Character from genshin.models import CalculatorCharacterDetails, CalculatorTalent, Character
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, Update, User from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Message, Update, User
@ -26,6 +27,7 @@ from utils.decorators.error import error_callable
from utils.decorators.restricts import restricts from utils.decorators.restricts import restricts
from utils.helpers import get_genshin_client from utils.helpers import get_genshin_client
from utils.log import logger from utils.log import logger
from utils.patch.aiohttp import AioHttpTimeoutException
class AvatarListPlugin(Plugin, BasePlugin): class AvatarListPlugin(Plugin, BasePlugin):
@ -140,8 +142,13 @@ class AvatarListPlugin(Plugin, BasePlugin):
rarity = 5 rarity = 5
else: else:
rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(response.player.avatar.id)] rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(response.player.avatar.id)]
except Exception as exc: # pylint: disable=W0703 return name_card, avatar, nickname, rarity
logger.error("enka 请求失败: %s", str(exc)) except (VaildateUIDError, UIDNotFounded, HTTPException) as exc:
logger.warning("EnkaNetwork 请求失败: %s", str(exc))
except (AioHttpTimeoutException, ClientConnectorError) as exc:
logger.warning("EnkaNetwork 请求超时: %s", str(exc))
except Exception as exc:
logger.error("EnkaNetwork 请求失败: %s", exc_info=exc)
choices = ArkoWrapper(characters).filter(lambda x: x.friendship == 10) # 筛选出好感满了的角色 choices = ArkoWrapper(characters).filter(lambda x: x.friendship == 10) # 筛选出好感满了的角色
if choices.length == 0: # 若没有满好感角色、则以好感等级排序 if choices.length == 0: # 若没有满好感角色、则以好感等级排序
choices = ArkoWrapper(characters).sort(lambda x: x.friendship, reverse=True) choices = ArkoWrapper(characters).sort(lambda x: x.friendship, reverse=True)