|
| 1 | +"""``$mcp_error_status``: the HTTP status behind a failed call, read off the |
| 2 | +raised exception's cause chain (posthog-python#1223, sdk-specs mcp-analytics |
| 3 | +"Failure capture").""" |
| 4 | + |
| 5 | +from http import HTTPStatus |
| 6 | +from types import SimpleNamespace |
| 7 | + |
| 8 | +import httpx |
| 9 | +import pytest |
| 10 | + |
| 11 | +from posthog.mcp import PostHogMCP |
| 12 | +from posthog.mcp.constants import PostHogMCPAnalyticsProperty as P |
| 13 | +from posthog.test.mcp._helpers import events_named, flush_background |
| 14 | + |
| 15 | + |
| 16 | +class Failure(Exception): |
| 17 | + def __init__(self, message="failed", **attributes): |
| 18 | + super().__init__(message) |
| 19 | + for name, value in attributes.items(): |
| 20 | + setattr(self, name, value) |
| 21 | + |
| 22 | + |
| 23 | +def _http_status_error(status): |
| 24 | + request = httpx.Request("GET", "https://api.example.com/invoices/1") |
| 25 | + response = httpx.Response(status, request=request) |
| 26 | + return httpx.HTTPStatusError( |
| 27 | + f"{status} from upstream", request=request, response=response |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def _raised_from(outer, inner): |
| 32 | + try: |
| 33 | + try: |
| 34 | + raise inner |
| 35 | + except Exception as error: |
| 36 | + raise outer from error |
| 37 | + except Exception as error: |
| 38 | + return error |
| 39 | + |
| 40 | + |
| 41 | +def _raised_during(outer, inner): |
| 42 | + try: |
| 43 | + try: |
| 44 | + raise inner |
| 45 | + except Exception: |
| 46 | + raise outer |
| 47 | + except Exception as error: |
| 48 | + return error |
| 49 | + |
| 50 | + |
| 51 | +def _raised_from_none(outer, inner): |
| 52 | + try: |
| 53 | + try: |
| 54 | + raise inner |
| 55 | + except Exception: |
| 56 | + raise outer from None |
| 57 | + except Exception as error: |
| 58 | + return error |
| 59 | + |
| 60 | + |
| 61 | +def _cause_without_suppressed_context(): |
| 62 | + error = _raised_during(RuntimeError("lookup failed"), Failure(status=500)) |
| 63 | + error.__cause__ = Failure(status=404) |
| 64 | + error.__suppress_context__ = False |
| 65 | + return error |
| 66 | + |
| 67 | + |
| 68 | +def _chain_of(length, status): |
| 69 | + error = Failure("innermost", status=status) |
| 70 | + for _ in range(length - 1): |
| 71 | + error = _raised_from(Failure("wrapper"), error) |
| 72 | + return error |
| 73 | + |
| 74 | + |
| 75 | +class UnreadableResponse(Exception): |
| 76 | + @property |
| 77 | + def response(self): |
| 78 | + raise RuntimeError("response not loaded") |
| 79 | + |
| 80 | + |
| 81 | +def _cycle(): |
| 82 | + first, second = Failure("first"), Failure("second") |
| 83 | + first.__cause__, second.__cause__ = second, first |
| 84 | + return first |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.parametrize( |
| 88 | + "error, status", |
| 89 | + [ |
| 90 | + (_http_status_error(503), 503), |
| 91 | + (_raised_from(RuntimeError("lookup failed"), _http_status_error(503)), 503), |
| 92 | + (_raised_during(RuntimeError("lookup failed"), _http_status_error(429)), 429), |
| 93 | + ( |
| 94 | + _raised_from_none(RuntimeError("lookup failed"), _http_status_error(503)), |
| 95 | + None, |
| 96 | + ), |
| 97 | + (_raised_from(Failure(status_code=404), _http_status_error(503)), 404), |
| 98 | + (Failure(status_code=502), 502), |
| 99 | + (Failure(statusCode=502), 502), |
| 100 | + (Failure(status=502), 502), |
| 101 | + (Failure(status_code=None, status=502), 502), |
| 102 | + (Failure(response=SimpleNamespace(status=502)), 502), |
| 103 | + (Failure(status="failed", response=SimpleNamespace(status_code=401)), 401), |
| 104 | + (Failure(status=HTTPStatus.BAD_GATEWAY), 502), |
| 105 | + (Failure(status_code=42), None), |
| 106 | + (Failure(status_code=600), None), |
| 107 | + (Failure(status_code=True), None), |
| 108 | + (Failure(status_code="503"), None), |
| 109 | + (Failure(status_code=503.0), None), |
| 110 | + (Failure("no status here"), None), |
| 111 | + (_cause_without_suppressed_context(), 404), |
| 112 | + (_chain_of(50, 503), 503), |
| 113 | + (_chain_of(51, 503), None), |
| 114 | + (_cycle(), None), |
| 115 | + (UnreadableResponse("failed"), None), |
| 116 | + ("upstream returned 503", None), |
| 117 | + ], |
| 118 | +) |
| 119 | +async def test_failed_call_carries_the_status_from_the_cause_chain(error, status): |
| 120 | + client = PostHogMCP("phc_test") |
| 121 | + captured = [] |
| 122 | + client.capture = lambda event, **kw: captured.append({"event": event, **kw}) |
| 123 | + |
| 124 | + client.capture_tool_call("fetch_invoice", is_error=True, error=error) |
| 125 | + await flush_background() |
| 126 | + |
| 127 | + [call] = events_named(captured, "$mcp_tool_call") |
| 128 | + assert call["properties"].get(P.ERROR_STATUS) == status |
| 129 | + |
| 130 | + |
| 131 | +async def test_explicit_status_wins_over_the_derived_one(): |
| 132 | + client = PostHogMCP("phc_test") |
| 133 | + captured = [] |
| 134 | + client.capture = lambda event, **kw: captured.append({"event": event, **kw}) |
| 135 | + |
| 136 | + client.capture_tool_call( |
| 137 | + "fetch_invoice", |
| 138 | + is_error=True, |
| 139 | + error=_http_status_error(503), |
| 140 | + properties={P.ERROR_STATUS: 418}, |
| 141 | + ) |
| 142 | + await flush_background() |
| 143 | + |
| 144 | + [call] = events_named(captured, "$mcp_tool_call") |
| 145 | + assert call["properties"][P.ERROR_STATUS] == 418 |
0 commit comments