Skip to content

Don't report caller errors as invalid API responses - #75

Open
Shiivu13 wants to merge 2 commits into
prooflinedev:mainfrom
Shiivu13:fix/caller-errors-before-request
Open

Shiivu13 wants to merge 2 commits into
prooflinedev:mainfrom
Shiivu13:fix/caller-errors-before-request

Conversation

@Shiivu13

Copy link
Copy Markdown

While reading operation_errors() in proofline/_transport.py I noticed it catches every exception that isn't a ProoflineError or httpx2.HTTPError and re-raises it as the static invalid-response problem with from None. For a body that came back from the server and failed to decode, that's what you want. But the generated _get_kwargs runs inside the same block, so mistakes made while the request is still being built get the same treatment:

client = ProoflineClient("tok", base_url="http://127.0.0.1:9")  # nothing listens here

client.list_reviews(org, limt=5)
# ProoflineError invalid-response, __cause__ None

client.request_patch_review(org, repo, body={"patch": "..."})  # dict instead of PatchReviewRequest
# ProoflineError invalid-response, __cause__ None

Neither call sends a request, yet the error says Proofline replied, and the original TypeError / AttributeError and its traceback are gone. You end up checking the token and the network when the problem is a typo. A bug in the SDK's own request building would be hidden the same way.

Fix

The mapping really depends on one thing: did a response arrive?

RetryTransport and AsyncRetryTransport now set a RESPONDED context var as soon as a response comes back, whatever the status. operation_errors() clears the flag on entry. If something raises before a response came back, the original exception goes through untouched. After a response, the behaviour is the same as before.

I put this in the transport and left the generated code alone. client.py and the template-generated Operations (request_scan today) both go through operation_errors(), so one change covers both and templates/ stays as it is. A ContextVar keeps the flag separate per thread and per asyncio task, so concurrent calls on one client don't see each other's state.

The private-body guarantee is unchanged: a decode failure on a body that did arrive still uses from None. test_successful_private_body_decoder_failure_is_static still passes, and the new test checks it again for request_scan.

Proofline review

I ran Proofline on this branch in my own fork first: Shiivu13#1. The finding and my reply are on that PR; this one is the same two commits rebased onto 7.1.0.

Proofline flagged one serious issue on the first commit, and it was right. I had only set RESPONDED on 2xx. A 4xx/5xx body nested deeper than json can decode ([ * 100000 fits easily in 8 MiB) raises RecursionError inside problem(), and with the flag still false operation_errors() let it through raw. On main the catch-all had hidden that as invalid-response, so this was a regression I introduced. I reproduced it against the loopback server for 400 and 500 in sync and async before touching anything.

While reproducing it I also found the same RecursionError leaking on main from export_reviews and the MCP calls, because they don't go through operation_errors() at all. The second commit fixes both:

  • problem() treats RecursionError like any other undecodable body, so the caller gets the real status and "Proofline returned an HTTP error without a problem document." That covers operations, exports and MCP in one place.
  • The flag is set on any response, not only 2xx, so anything unexpected while handling a server reply still maps to an SDK problem.

One thing I left out on purpose: a 200 MCP reply nested that deep still raises RecursionError from _mcp._decode, and so would a deep line in an NDJSON export. Both are like that on main too and live outside the transport, so I'd rather send them as a separate small PR than grow this one.

Tests

Three new tests in tests/test_http.py, run in sync and async mode against the loopback server:

  • test_caller_errors_before_any_request_are_not_reported_as_api_responses covers an unknown keyword, a dict body on request_patch_review and on the generated request_scan, and a missing argument. Each one raises its own TypeError or AttributeError with a traceback, and the server sees zero requests.
  • test_a_body_that_arrives_still_decodes_to_the_static_sdk_problem makes a caller error first, then checks that a malformed 200 still maps to invalid-response for get_finding and request_scan without the private body in the traceback.
  • test_problem_nested_past_the_decoder_limit_stays_a_typed_problem sends a too-deep body with 400 and 500 to get_finding, request_scan and export_reviews, and expects a ProoflineError with that status.

All 8 cases fail on current main. The deep-nesting one also fails on the first commit of this branch alone, which is how Proofline's finding showed up.

I also ran a few things by hand that aren't worth a permanent test: a typo and a slow malformed reply running at the same time under asyncio.gather and on two threads sharing one sync client (each got its own error), a 503 with Retry-After: 0 followed by a good or a malformed 200, a header timeout, a refused connection, and a typo straight after an export_reviews call. All behaved as described above.

python -m ruff check . && python -m ruff format --check .
python -m mypy proofline scripts
python -m pytest -q

ruff and mypy are clean and 226 tests pass. The full scripts/check.py run is green on Linux on my fork: https://github.com/Shiivu13/sdk-python/actions/runs/44877733204. One note: on Windows, test_body_stream_deadline_does_not_turn_authority_into_timeout sometimes fails with WinError 10053. It fails the same way on an untouched main (once in three runs for me), so it isn't related to this change.

operation_errors() turned every exception that wasn't a ProoflineError or
httpx.HTTPError into "Proofline returned an invalid API response" and
dropped the context. That also caught errors raised while the request was
still being built, like an unknown keyword or a dict passed as the body,
so a local typo looked like a server problem and the traceback was gone.
No request had even been sent.

The retry transports now set a RESPONDED context var when a 2xx response
comes back. operation_errors() only maps to the static problem after
that; before it, the original exception propagates. Decode failures on a
body that did arrive still map to invalid-response with the private body
kept out of the traceback.
Proofline caught this on the first commit: RESPONDED was only set for
2xx, so a 4xx/5xx body nested deeper than json can decode raised
RecursionError inside problem(), and operation_errors let it through
because the flag was still false. Before that commit the catch-all had
turned it into invalid-response.

Two changes:
- problem() treats RecursionError like any other undecodable body, so
  the caller gets the real status and "HTTP error without a problem
  document". This also fixes export_reviews and the MCP calls, which
  leaked the same RecursionError on main because they don't go through
  operation_errors at all.
- The transports set RESPONDED as soon as any response comes back,
  not only on 2xx, so anything unexpected while handling a server
  reply still maps to the SDK problem.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant