errors.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import sys
  2. import traceback
  3. from functools import wraps
  4. from wbb import app, LOG_GROUP_ID
  5. def split_limits(text):
  6. if len(text) < 2048:
  7. return [text]
  8. lines = text.splitlines(True)
  9. small_msg = ''
  10. result = []
  11. for line in lines:
  12. if len(small_msg) + len(line) < 2048:
  13. small_msg += line
  14. else:
  15. result.append(small_msg)
  16. small_msg = line
  17. else:
  18. result.append(small_msg)
  19. return result
  20. def capture_err(func):
  21. @wraps(func)
  22. async def capture(client, message, *args, **kwargs):
  23. try:
  24. return await func(client, message, *args, **kwargs)
  25. except Exception as err:
  26. exc_type, exc_obj, exc_tb = sys.exc_info()
  27. errors = traceback.format_exception(
  28. etype=exc_type, value=exc_obj, tb=exc_tb,
  29. )
  30. error_feedback = split_limits(
  31. '**ERROR** | `{}` | `{}`\n\n```{}```\n\n```{}```\n'.format(
  32. 0 if not message.from_user else message.from_user.id,
  33. 0 if not message.chat else message.chat.id,
  34. message.text or message.caption,
  35. ''.join(errors),
  36. ),
  37. )
  38. for x in error_feedback:
  39. await app.send_message(
  40. LOG_GROUP_ID,
  41. x
  42. )
  43. raise err
  44. return capture