Files
posthog.com/contents/docs/libraries/python/_snippets/python-flask-exception-autocapture.mdx
Vincent (Wen Yu) Ge 9622fbe001 Stepped installation guide for error tracking (#12220)
Co-authored-by: Vincent (Wen Yu) Ge <gewenyu99@gmail.com>
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
Co-authored-by: David Newell <david@posthog.com>
Co-authored-by: edwinyjlim <edwinyjlim@gmail.com>
Co-authored-by: Edwin Lim <edwin@posthog.com>
2025-07-30 13:33:17 -04:00

17 lines
608 B
Plaintext

```python
from flask import Flask, jsonify
from posthog import Posthog
posthog = Posthog('<ph_project_api_key>', host='<ph_client_api_host>')
@app.errorhandler(Exception)
def handle_exception(e):
# Capture methods, including capture_exception, return the UUID of the captured event,
# which you can use to find specific errors users encountered
event_id = posthog.capture_exception(e)
# You can show the event ID to your user, and ask them to include it in bug reports
response = jsonify({'message': str(e), 'error_id': event_id})
response.status_code = 500
return response
```