mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-25 20:10:28 +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
|
return None, None
|
||||||
|
|
||||||
logger.debug(f'Matched command start: '
|
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()
|
full_command = cmd_string[len(matched_start):].lstrip()
|
||||||
|
|
||||||
if not full_command:
|
if not full_command:
|
||||||
@ -565,6 +565,8 @@ async def _real_run_command(session: CommandSession,
|
|||||||
SwitchException) as e:
|
SwitchException) as e:
|
||||||
raise e
|
raise e
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
logger.error(f'An exception occurred while '
|
||||||
|
f'running command {session.cmd.name}:')
|
||||||
logger.exception(e)
|
logger.exception(e)
|
||||||
handled = True
|
handled = True
|
||||||
raise _FinishException(handled)
|
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
|
# at the same time some plugins may want to handle it
|
||||||
msg_text_length = len(session.msg_text)
|
msg_text_length = len(session.msg_text)
|
||||||
|
|
||||||
coros = []
|
futures = []
|
||||||
for p in _nl_processors:
|
for p in _nl_processors:
|
||||||
if not p.allow_empty_message and not session.msg:
|
if not p.allow_empty_message and not session.msg:
|
||||||
# don't allow empty msg, but it is one, so skip to next
|
# 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
|
should_run = False
|
||||||
|
|
||||||
if should_run:
|
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
|
# 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)
|
key=lambda r: r.confidence, reverse=True)
|
||||||
logger.debug(f'NLP results: {results}')
|
logger.debug(f'NLP results: {results}')
|
||||||
if results and results[0].confidence >= 60.0:
|
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)
|
session = RequestSession(bot, ctx)
|
||||||
|
|
||||||
logger.debug(f'Emitting event: {event}')
|
logger.debug(f'Emitting event: {event}')
|
||||||
|
try:
|
||||||
await _bus.emit(event, session)
|
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:
|
def _log_notice(ctx: Context_T) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user