Files
karaoke-gen/backend/tests/api/test_client_errors.py
T
Andrew Beveridge ☄️andClaude Opus 5.5 5d56ee9069 fix(frontend): stop reporting opaque cross-origin "Script error." (v0.238.6) (#1050)
## Summary
The error monitor keeps alerting on a frontend pattern
`@https://gen.nomadkaraoke.com/` with sample `Error: Script error.` (iOS
Safari, 4 occurrences since June). That's the browser's opaque
cross-origin error: a script from another origin (a third-party tag,
in-app browser or extension injection) threw. The browser hides
everything, so there's no error object, filename or line. Our
`window.onerror` handler wrapped it in `new Error(event.message)`, so
the only "stack" frame (`63bfc60a…js:6:874`) is our own handler. There's
nothing actionable in these reports.

## Changes
- `frontend/lib/client-error-setup.ts`: new exported
`isOpaqueCrossOriginError(event)`. The window `error` listener returns
early when `event.error == null` and the message is `Script error.`.
- `backend/services/error_monitor/frontend_ingestion.py` +
`backend/api/routes/client_errors.py`: `is_opaque_script_error()` drops
bare `Script error.` / `Error: Script error.` reports at ingestion.
They're accepted (202) but not persisted, the same way the bot filter
works. This also stops older cached bundles from alerting.
- `docs/ARCHITECTURE.md`: a note on the crash-report noise filters.
- Version 0.238.5 → 0.238.6.

## Testing
- New Jest `lib/__tests__/client-error-setup.test.ts`: tests the
predicate; an opaque event is not reported; a real error is still
reported. Full Jest: 1342 passed.
- Backend: parametrized API tests (dropped vs "Script error in …" still
persisted) and unit tests for the predicate. `tests/unit` passed (1834),
and `backend/tests` without the emulator passed (4591). Emulator tests
are left to CI.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Bug Fixes**
* Opaque cross-origin “Script error” events without an error object and
location details are no longer recorded. Other client errors, including
script errors with location details or different messages, continue
through the reporting flow.
* Matching is case- and whitespace-tolerant and allows an optional
trailing period.
* **Documentation**
* Updated Error Monitor guidance to describe which browser and
server-side errors are ignored.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 13:58:03 -04:00

145 lines
5.5 KiB
Python

"""API tests for POST /api/client-errors."""
from __future__ import annotations
from unittest.mock import MagicMock
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from backend.api.routes.client_errors import router as client_errors_router
@pytest.fixture
def client(monkeypatch):
# reset the module-level limiter between tests
import backend.api.routes.client_errors as mod
mod._limiter = mod.RateLimiter(max_per_minute=60)
fake_adapter = MagicMock()
fake_adapter.upsert_pattern.return_value = MagicMock(
pattern_id="deadbeef", is_new=True, previous_status=None
)
monkeypatch.setattr(mod, "_get_adapter", lambda: fake_adapter)
app = FastAPI()
app.include_router(client_errors_router, prefix="/api")
client = TestClient(app)
client.fake_adapter = fake_adapter # type: ignore[attr-defined]
return client
def _payload(**overrides):
base = {
"message": "TypeError: Cannot read properties of null",
"stack": "TypeError: Cannot read properties of null\n at x (app.js:1:2)",
"url": "https://gen.nomadkaraoke.com/en/app/jobs/#/abc/review",
"user_agent": "Mozilla/5.0 (Android 14; Mobile; rv:138.0) Gecko/138.0 Firefox/138.0",
"release": "abc1234",
"user_email": None,
"viewport": {"w": 412, "h": 915},
"locale": "en",
"source": "window.onerror",
}
base.update(overrides)
return base
def test_accepts_valid_report_and_upserts(client):
resp = client.post("/api/client-errors", json=_payload())
assert resp.status_code == 202
assert resp.json()["pattern_id"] == "deadbeef"
client.fake_adapter.upsert_pattern.assert_called_once()
(pd,), _ = client.fake_adapter.upsert_pattern.call_args
assert pd.service == "frontend"
assert pd.resource_type == "browser"
def test_bot_report_is_accepted_but_not_persisted(client):
bot_ua = (
"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; "
"bingbot/2.0; +http://www.bing.com/bingbot.htm) Chrome/136.0.0.0 Safari/537.36"
)
resp = client.post("/api/client-errors", json=_payload(user_agent=bot_ua))
# Accepted (so the bot won't retry) but never upserted / alerted.
assert resp.status_code == 202
assert resp.json()["is_new"] is False
client.fake_adapter.upsert_pattern.assert_not_called()
@pytest.mark.parametrize("message", ["Script error.", "Error: Script error.", "script error"])
def test_opaque_script_error_is_accepted_but_not_persisted(client, message):
# Opaque cross-origin error (older cached bundles still report it) — the
# "stack" is only our own handler's frame, so it's pure alert noise.
resp = client.post(
"/api/client-errors",
json=_payload(
message=message,
stack=f"Error: {message}\n@https://gen.nomadkaraoke.com/_next/static/chunks/x.js:6:874",
source="window.onerror",
extra={"filename": "", "lineno": 0, "colno": 0},
),
)
assert resp.status_code == 202
assert resp.json()["is_new"] is False
client.fake_adapter.upsert_pattern.assert_not_called()
def test_located_script_error_message_is_persisted(client):
# A genuine `throw new Error("Script error.")` from our own code has a real
# file/line on the ErrorEvent — actionable, so it must still be recorded.
resp = client.post(
"/api/client-errors",
json=_payload(
message="Error: Script error.",
stack="Error: Script error.\n at foo (https://gen.nomadkaraoke.com/_next/static/chunks/y.js:3:120)",
source="window.onerror",
extra={"filename": "https://gen.nomadkaraoke.com/_next/static/chunks/y.js", "lineno": 3, "colno": 120},
),
)
assert resp.status_code == 202
client.fake_adapter.upsert_pattern.assert_called_once()
def test_message_merely_mentioning_script_error_is_persisted(client):
resp = client.post("/api/client-errors", json=_payload(message="Script error in chunk loader"))
assert resp.status_code == 202
client.fake_adapter.upsert_pattern.assert_called_once()
def test_rejects_missing_message(client):
resp = client.post("/api/client-errors", json=_payload(message=""))
assert resp.status_code == 422
def test_caps_oversized_payload(client):
huge = "x" * 200_000
resp = client.post("/api/client-errors", json=_payload(stack=huge))
# server accepts but truncates — sample_message must be <= MAX
assert resp.status_code == 202
(pd,), _ = client.fake_adapter.upsert_pattern.call_args
from backend.services.error_monitor.frontend_ingestion import (
MAX_SAMPLE_MESSAGE_CHARS,
)
assert len(pd.sample_message) <= MAX_SAMPLE_MESSAGE_CHARS
def test_rate_limit_returns_429(client, monkeypatch):
import backend.api.routes.client_errors as mod
mod._limiter = mod.RateLimiter(max_per_minute=2)
assert client.post("/api/client-errors", json=_payload()).status_code == 202
assert client.post("/api/client-errors", json=_payload()).status_code == 202
r3 = client.post("/api/client-errors", json=_payload())
assert r3.status_code == 429
def test_strips_sensitive_query_string(client):
resp = client.post(
"/api/client-errors",
json=_payload(url="https://gen.nomadkaraoke.com/en/app/jobs/?token=SECRET#/abc/review"),
)
assert resp.status_code == 202
(pd,), _ = client.fake_adapter.upsert_pattern.call_args
assert "SECRET" not in pd.sample_message
assert "token=" not in pd.sample_message