mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
Improve exception handling
This commit is contained in:
parent
36a498cf74
commit
d90c9a072c
@ -400,7 +400,7 @@ def parse_command(bot: NoneBot,
|
||||
return None, None
|
||||
|
||||
logger.debug(f'Matched command start: '
|
||||
f'{matched_start}{"(space)" if not matched_start else ""}')
|
||||
f'{matched_start}{"(empty)" if not matched_start else ""}')
|
||||
full_command = cmd_string[len(matched_start):].lstrip()
|
||||
|
||||
if not full_command:
|
||||
@ -565,6 +565,8 @@ async def _real_run_command(session: CommandSession,
|
||||
SwitchException) as e:
|
||||
raise e
|
||||
except Exception as e:
|
||||
logger.error(f'An exception occurred while '
|
||||
f'running command {session.cmd.name}:')
|
||||
logger.exception(e)
|
||||
handled = True
|
||||
raise _FinishException(handled)
|
||||
|
@ -109,7 +109,7 @@ async def handle_natural_language(bot: NoneBot, ctx: Context_T) -> bool:
|
||||
# at the same time some plugins may want to handle it
|
||||
msg_text_length = len(session.msg_text)
|
||||
|
||||
coros = []
|
||||
futures = []
|
||||
for p in _nl_processors:
|
||||
if not p.allow_empty_message and not session.msg:
|
||||
# don't allow empty msg, but it is one, so skip to next
|
||||
@ -132,11 +132,19 @@ async def handle_natural_language(bot: NoneBot, ctx: Context_T) -> bool:
|
||||
should_run = False
|
||||
|
||||
if should_run:
|
||||
coros.append(p.func(session))
|
||||
futures.append(asyncio.ensure_future(p.func(session)))
|
||||
|
||||
if coros:
|
||||
if futures:
|
||||
# wait for possible results, and sort them by confidence
|
||||
results = sorted(filter(lambda r: r, await asyncio.gather(*coros)),
|
||||
results = []
|
||||
for fut in futures:
|
||||
try:
|
||||
results.append(await fut)
|
||||
except Exception as e:
|
||||
logger.error('An exception occurred while running '
|
||||
'some natural language processor:')
|
||||
logger.exception(e)
|
||||
results = sorted(filter(lambda r: r, results),
|
||||
key=lambda r: r.confidence, reverse=True)
|
||||
logger.debug(f'NLP results: {results}')
|
||||
if results and results[0].confidence >= 60.0:
|
||||
|
@ -94,7 +94,11 @@ async def handle_notice_or_request(bot: NoneBot, ctx: Context_T) -> None:
|
||||
session = RequestSession(bot, ctx)
|
||||
|
||||
logger.debug(f'Emitting event: {event}')
|
||||
await _bus.emit(event, session)
|
||||
try:
|
||||
await _bus.emit(event, session)
|
||||
except Exception as e:
|
||||
logger.error(f'An exception occurred while handling event {event}:')
|
||||
logger.exception(e)
|
||||
|
||||
|
||||
def _log_notice(ctx: Context_T) -> None:
|
||||
|
Loading…
Reference in New Issue
Block a user