ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(errors): attribute a frontend crash report to the logged-in user
Juan Denisdcb8810635d1
diff --git a/backend/tests/test_error_logs.py b/backend/tests/test_error_logs.pyindex cb81db55..12034749 100644--- a/backend/tests/test_error_logs.py+++ b/backend/tests/test_error_logs.py@@ -253,3 +253,44 @@ def test_500_handler_does_not_commit_the_crashed_request_work(client, app):# ...but the crash itself was still recorded.assert ErrorLog.query.filter_by(endpoint='/api/v1/__test_partial_write').count() == 1+++# --------------------------------------------------------------------------- #+# Client ingestion: attribution+# --------------------------------------------------------------------------- #++def test_client_report_is_attributed_when_a_token_is_sent(client, auth_headers):+ """The frontend attaches Authorization so the row is not anonymous.++ ErrorLog.user_id, to_dict()['username'] and the "User #N" row in+ pages/Errors.jsx were dead end-to-end while reportClientError sent no+ header: every frontend row was anonymous no matter who was logged in.+ """+ res = client.post('/api/v1/error-logs/client',+ json={'message': 'boom in the browser'},+ headers=auth_headers)+ assert res.status_code == 201+ entry = db.session.get(ErrorLog, res.get_json()['id'])+ assert entry.user_id is not None+ assert entry.to_dict()['username'] == 'testadmin'+++def test_client_report_without_a_token_is_anonymous_not_rejected(client):+ """Reporting must still work with no session -- that is when crashes happen."""+ res = client.post('/api/v1/error-logs/client',+ json={'message': 'anonymous browser crash'})+ assert res.status_code == 201+ entry = db.session.get(ErrorLog, res.get_json()['id'])+ assert entry.user_id is None+++def test_client_report_with_a_junk_token_is_anonymous_not_a_500(client):+ """verify_jwt_in_request(optional=True) tolerates a MISSING token only --+ a malformed or expired one raises, and the route's except is what turns+ that into an anonymous report instead of a crash."""+ res = client.post('/api/v1/error-logs/client',+ json={'message': 'stale session crash'},+ headers={'Authorization': 'Bearer not.a.jwt'})+ assert res.status_code == 201+ entry = db.session.get(ErrorLog, res.get_json()['id'])+ assert entry.user_id is Nonediff --git a/frontend/src/services/api/errorLogs.js b/frontend/src/services/api/errorLogs.jsindex 680a5fb6..2d695a7e 100644--- a/frontend/src/services/api/errorLogs.js+++ b/frontend/src/services/api/errorLogs.js@@ -42,11 +42,25 @@ export async function deleteErrorLog(id) {// boundary error is likely), and a failed error report must never throw —// it is called from ErrorBoundary.componentDidCatch, where throwing would// re-trip the boundary it is reporting from.+//+// The Authorization header is still attached by hand when a token exists, so+// the backend can attribute the report (ErrorLog.user_id, rendered as "User #N"+// in pages/Errors.jsx). Without it every frontend row is anonymous.+//+// A stale token costs nothing here, but NOT for the reason it looks like:+// verify_jwt_in_request(optional=True) tolerates a MISSING token only — an+// expired one raises ExpiredSignatureError. The route survives because it wraps+// that call in try/except and falls back to an anonymous report, so do not+// tighten that except without revisiting this.export async function reportClientError(payload) {try {+ const token = this.getToken();await fetch(`${this.baseUrl}/error-logs/client`, {method: 'POST',- headers: { 'Content-Type': 'application/json' },+ headers: {+ 'Content-Type': 'application/json',+ ...(token && { Authorization: `Bearer ${token}` }),+ },body: JSON.stringify(payload),});} catch {