Skip to content

Commit

Permalink
feat: truncate exceptions (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
tm-a-t authored and vanutp committed Apr 20, 2024
1 parent 20ec126 commit 739fbbc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
9 changes: 6 additions & 3 deletions tgpy/_core/eval_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from tgpy import app
from tgpy._core import message_design
from tgpy._core.utils import convert_result, format_traceback
from tgpy.api.tgpy_eval import tgpy_eval
from tgpy.api import constants, tgpy_eval


async def eval_message(code: str, message: Message) -> Message | None:
Expand All @@ -14,15 +14,18 @@ async def eval_message(code: str, message: Message) -> Message | None:
try:
eval_result = await tgpy_eval(code, message, filename=None)
except Exception:
result = 'Error occurred'
result = None
output = ''
exc = ''.join(format_traceback())
exc, full_exc = format_traceback()
else:
if app.ctx.is_manual_output:
return
result = convert_result(eval_result.result)
output = eval_result.output
exc = ''
full_exc = None

constants['exc'] = full_exc

try:
# noinspection PyProtectedMember
Expand Down
5 changes: 4 additions & 1 deletion tgpy/_core/message_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ async def edit_message(
traceback: str = '',
output: str = '',
) -> Message:
if result is None and output:
if not result and output:
result = output
output = ''
if not result and traceback:
result = traceback
traceback = ''

title = Utf16CodepointsWrapper(TITLE)
parts = [
Expand Down
7 changes: 5 additions & 2 deletions tgpy/_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ def convert_result(result):
return result


def format_traceback():
def format_traceback() -> tuple[str, str]:
exc_type, exc_value, exc_traceback = sys.exc_info()
exc_traceback = exc_traceback.tb_next.tb_next
return traceback.format_exception(exc_type, exc_value, exc_traceback)
te = traceback.TracebackException(
type(exc_value), exc_value, exc_traceback, compact=True
)
return ''.join(te.format_exception_only()), ''.join(te.format())
2 changes: 1 addition & 1 deletion tgpy/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def run_modules():
await module.run()
except Exception:
logger.error(f'Error during running module {module.name!r}')
logger.error(''.join(format_traceback()))
logger.error(format_traceback()[1])
continue


Expand Down
1 change: 1 addition & 0 deletions tgpy/std/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@

tgpy.api.constants['ctx'] = app.ctx
tgpy.api.constants['client'] = app.client
tgpy.api.constants['exc'] = None

__all__ = []

0 comments on commit 739fbbc

Please sign in to comment.