Skip to content

fix(wqp): report WQP's reason for a rejected query - #478

Draft
thodson-usgs wants to merge 4 commits into
DOI-USGS:mainfrom
thodson-usgs:fix/wqp-flags-and-errors
Draft

thodson-usgs wants to merge 4 commits into
DOI-USGS:mainfrom
thodson-usgs:fix/wqp-flags-and-errors

Conversation

@thodson-usgs

@thodson-usgs thodson-usgs commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

WQP states why it rejects a query, and the raised error left that reason out. The wqp adapter now passes a detail_from function that reads the reason from the response. It also sends a bBox list in the one form WQP accepts, and raises for zip=True, whose zip archive this package cannot parse.

 wqp getter
   _check_kwargs
+    zip=True or "yes" (any case) -> NotImplementedError (WQP returns a zip archive)
     _materialize_query_iterables
+    bBox list              -> "-92.8,44.2,-88.9,46.0"
   _query_wqp
     _query_with_retry
-      _get_with_retry
+      _get_with_retry(detail_from=_wqp_error_detail)
         _raise_for_status
-          "HTTP 400 Bad Request (URL: ...)"
+          "HTTP 400 Bad Request: The value of lat=nan is not between -90 and 90. (URL: ...)"

Where WQP puts the reason (checked live on 2026-12-01):

Interface Rejected query Reason
legacy /data/ HTTP 400, empty body Warning: 299 WQP "<reason>", with a " in the reason sent as ' and a backslash sent as is
WQX3 /wqx3/ HTTP 400, text/plain body first line: Invalid request (id '...'): <reason>

This follows the code review of #473. Its boolean finding is already fixed by #477. Two other findings are left out on purpose:

  • NaN values. WQP rejects lat=nan on both interfaces, and this PR adds WQP's reason to that error. A None or nan inside a list is ignored by both interfaces, and the other values in the list still filter the results.
  • bytes and dict values. No caller sends them.

Evidence

Live WQP, before this change:

Request Response
legacy bBox=-89.5;43.0;-89.3;43.2 (a list joined by the legacy serializer) 400, not a valid bounding box
WQX3 bBox sent as four repeated keys 500, empty body
bBox=-89.5,43.0,-89.3,43.2 on either interface 200 (956 legacy sites)
zip=yes application/zip, then read_code_csv raises ParserError: Expected 2 fields in line 5, saw 3
WQX3 zip=YES or zip=Yes application/zip (legacy rejects both with a 400)

Tests in tests/wqp_test.py:

  • Before: what_sites(siteid=...) against a mocked legacy 400 raises HTTP 400 Bad Request (URL: ...).
    After: it raises HTTP 400 Bad Request: The value of bBox=... is not a valid bounding box. (URL: ...).
  • Before: what_sites(legacy=..., bBox=[-92.8, 44.2, -88.9, 46.0]) sends bBox=-92.8;44.2;-88.9;46.0 (legacy) or four bBox keys (WQX3).
    After: both send bBox=-92.8,44.2,-88.9,46.0.
  • _wqp_error_detail has unit cases for one and two Warning headers, a backslash as WQP sends it, a warn-date, a proxy's Warning beside a WQX3 body (the body's reason is used), a WQX3 body with LF and CRLF endings, an empty body and an HTML body. Each case returns the reason or None. The function also returns the expected text for the three Warning headers and the one WQX3 body that WQP sent during the live checks.

The offline suite (1,682 tests), coverage, ruff, mypy --strict, lint-imports, xenon and complexipy pass.

Merge Danger

Door: two-way

Blast Radius: wqp

  • WQP error messages now include WQP's text before the URL, so a caller that matches on the exact message would need to change. The error classes do not change.
  • zip=True, and zip="yes" in any case, now raise NotImplementedError before the request is sent. Since fix(wqp): send a boolean as yes or no #477 they returned a zip archive that failed to parse. Before Send numeric and boolean query values instead of dropping them #473, zip=True was dropped.
  • _query_with_retry takes a new keyword-only detail_from. NLDI and utils.query do not pass it, so their behavior does not change.

🤖 Generated with Claude Code

thodson-usgs and others added 3 commits December 1, 2026 10:44
WQP states why it rejected a query, and the raised error left it out.
The legacy interface sends the reason in a `Warning` header with an
empty body (`299 WQP "The value of lat=nan is not between -90 and
90."`), and WQX3 sends it as a text/plain body. A new `detail_from`
reader adds it to the message, so `what_sites(lat=float("nan"), ...)`
now says what to fix in place of a bare `HTTP 400 Bad Request`.
`_query_with_retry` passes `detail_from` to `_get_with_retry`, which
already accepted it.

Two values WQP could not read are also handled in `_check_kwargs`:

- A `bBox` given as a list of four numbers was joined with semicolons
  on the legacy interface (HTTP 400, "not a valid bounding box") and
  sent as four repeated keys on WQX3 (HTTP 500). It is now joined
  with commas, the form both interfaces read (checked live).
- `zip=True` became `zip=yes` after DOI-USGS#477, and WQP then returns a zip
  archive that the CSV parser fails on with a ParserError. A `zip`
  that requests an archive now raises `NotImplementedError` with the
  remedy, as `mimeType="geojson"` already does.

Also: the `query()` docstring states how scalars and `None` are sent,
and `lat`, `long` and `within` are documented as a string or number.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The `detail_from` docstring said "a truthy result is appended"; it now
says the function returns the reason or `None`, and a non-empty reason
is added after the status. The `bBox` and `zip` docstrings, comments,
test docstrings, the error message and NEWS now say that WQP accepts
only the comma form, and that this package cannot parse a zip archive.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
WQX3 returns a zip archive for `zip=YES` and `zip=Yes` as well as
`zip=yes` (checked live), so the refusal now compares the value without
regard to case.

WQP sends its reason as `299 WQP "text"`, with a `"` in the text replaced
by `'` and backslashes left as sent. Undoing RFC quoted-pair escapes
removed a backslash the caller had sent, and a reason that ended in one
did not match at all. A Warning from another agent, such as a proxy, was
also reported as WQP's reason and hid the WQX3 body. The pattern now
matches only `299 WQP "..."` and takes the text as sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thodson-usgs
thodson-usgs requested a review from ehinman December 1, 2026 20:14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ehinman

ehinman commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

These seem like pretty low risk edge cases. I don't see a single wqp function where zip= is even an input. Is it some standard httpx kwarg then? My suggestion is to accommodate legacy bbox inputs (though I think the examples make clear the best input format) but not worry about the zip edge case.

@thodson-usgs

thodson-usgs commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

Yeah, fair enough. zip can pass as a kwarg. So edge case in practice, but I think the spirit is correct: Pre bots, users would spend hours (days) debugging scripts, because research software was documented using examples, and lacked better error handling

@thodson-usgs thodson-usgs added the v1 Non-breaking: can merge for the next 1.x release label Dec 2, 2026
@ehinman

ehinman commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Is there a public catalog of these server-side error messages? That would be interesting to see, but not required for this pass-through.

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

v1 Non-breaking: can merge for the next 1.x release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants