🥅 catch exception raised in setup factory

This commit is contained in:
Mix 2021-08-12 22:26:41 +08:00
parent 2c6b41f62c
commit 5791018af1
2 changed files with 44 additions and 12 deletions

View File

@ -234,11 +234,20 @@ class Driver(ForwardDriver):
try: try:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
while not self.should_exit.is_set(): while not self.should_exit.is_set():
if not bot:
try:
if callable(setup): if callable(setup):
setup_ = await setup() setup_ = await setup()
else: else:
setup_ = setup setup_ = setup
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Error while parsing setup {setup!r}.</bg #f8bbd0></r>"
)
await asyncio.sleep(3)
continue
if not bot:
request = await _build_request(setup_) request = await _build_request(setup_)
if not request: if not request:
return return
@ -247,7 +256,6 @@ class Driver(ForwardDriver):
bot = BotClass(setup.self_id, request) bot = BotClass(setup.self_id, request)
self._bot_connect(bot) self._bot_connect(bot)
elif callable(setup): elif callable(setup):
setup_ = await setup()
request = await _build_request(setup_) request = await _build_request(setup_)
if not request: if not request:
await asyncio.sleep(setup_.poll_interval) await asyncio.sleep(setup_.poll_interval)
@ -308,10 +316,18 @@ class Driver(ForwardDriver):
try: try:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
while True: while True:
try:
if callable(setup): if callable(setup):
setup_ = await setup() setup_ = await setup()
else: else:
setup_ = setup setup_ = setup
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Error while parsing setup {setup!r}.</bg #f8bbd0></r>"
)
await asyncio.sleep(3)
continue
url = URL(setup_.url) url = URL(setup_.url)
if not url.is_absolute() or not url.host: if not url.is_absolute() or not url.host:

View File

@ -349,11 +349,20 @@ class Driver(ReverseDriver, ForwardDriver):
try: try:
async with httpx.AsyncClient(http2=True) as session: async with httpx.AsyncClient(http2=True) as session:
while not self.shutdown.is_set(): while not self.shutdown.is_set():
if not bot:
try:
if callable(setup): if callable(setup):
setup_ = await setup() setup_ = await setup()
else: else:
setup_ = setup setup_ = setup
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Error while parsing setup {setup!r}.</bg #f8bbd0></r>"
)
await asyncio.sleep(3)
continue
if not bot:
request = await _build_request(setup_) request = await _build_request(setup_)
if not request: if not request:
return return
@ -361,7 +370,6 @@ class Driver(ReverseDriver, ForwardDriver):
bot = BotClass(setup.self_id, request) bot = BotClass(setup.self_id, request)
self._bot_connect(bot) self._bot_connect(bot)
elif callable(setup): elif callable(setup):
setup_ = await setup()
request = await _build_request(setup_) request = await _build_request(setup_)
if not request: if not request:
await asyncio.sleep(setup_.poll_interval) await asyncio.sleep(setup_.poll_interval)
@ -406,10 +414,18 @@ class Driver(ReverseDriver, ForwardDriver):
try: try:
while True: while True:
try:
if callable(setup): if callable(setup):
setup_ = await setup() setup_ = await setup()
else: else:
setup_ = setup setup_ = setup
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Error while parsing setup {setup!r}.</bg #f8bbd0></r>"
)
await asyncio.sleep(3)
continue
url = httpx.URL(setup_.url) url = httpx.URL(setup_.url)
if not url.netloc: if not url.netloc: