trytond.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import sentry_sdk
  2. from sentry_sdk.integrations import Integration
  3. from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
  4. from sentry_sdk.utils import ensure_integration_enabled, event_from_exception
  5. from trytond.exceptions import TrytonException # type: ignore
  6. from trytond.wsgi import app # type: ignore
  7. # TODO: trytond-worker, trytond-cron and trytond-admin intergations
  8. class TrytondWSGIIntegration(Integration):
  9. identifier = "trytond_wsgi"
  10. origin = f"auto.http.{identifier}"
  11. def __init__(self): # type: () -> None
  12. pass
  13. @staticmethod
  14. def setup_once(): # type: () -> None
  15. app.wsgi_app = SentryWsgiMiddleware(
  16. app.wsgi_app,
  17. span_origin=TrytondWSGIIntegration.origin,
  18. )
  19. @ensure_integration_enabled(TrytondWSGIIntegration)
  20. def error_handler(e): # type: (Exception) -> None
  21. if isinstance(e, TrytonException):
  22. return
  23. else:
  24. client = sentry_sdk.get_client()
  25. event, hint = event_from_exception(
  26. e,
  27. client_options=client.options,
  28. mechanism={"type": "trytond", "handled": False},
  29. )
  30. sentry_sdk.capture_event(event, hint=hint)
  31. # Expected error handlers signature was changed
  32. # when the error_handler decorator was introduced
  33. # in Tryton-5.4
  34. if hasattr(app, "error_handler"):
  35. @app.error_handler
  36. def _(app, request, e): # type: ignore
  37. error_handler(e)
  38. else:
  39. app.error_handlers.append(error_handler)