Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions newsfragments/1503.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Say what was received, rather than "unexpected shape", when the Vuforia
credentials API returns something which is not JSON.
7 changes: 6 additions & 1 deletion src/vws_web_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,12 @@ def _json_request(
try:
response_body: object = response.json()
except requests.JSONDecodeError as exc:
message = "The Vuforia credentials response had an unexpected shape."
content_type = response.headers.get("Content-Type", "unset")
message = (
f"Expected JSON from {url}, but the response had status "
f"{response.status_code} and content type {content_type}: "
f"{response.text[:500]}"
)
raise RuntimeError(message) from exc
return response_body

Expand Down
13 changes: 10 additions & 3 deletions tests/test_model_target_web_api_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# ruff: noqa: ANN401, SLF001
"""Tests for Model Target Web API detail helpers."""

import re
from typing import Any

import pytest
Expand Down Expand Up @@ -237,13 +238,19 @@ def test_json_request_raises_runtime_error_for_connection_failure() -> None:


def test_json_request_raises_runtime_error_for_invalid_json() -> None:
"""Invalid JSON responses raise a stable runtime error."""
"""A response which is not JSON says what was received instead."""
session = _Session(
response=_response(content=b"not json", status_code=200)
response=_response(content=b"<html>error</html>", status_code=200)
)

with pytest.raises(
expected_exception=RuntimeError, match="unexpected shape"
expected_exception=RuntimeError,
match=re.escape(
pattern=(
"Expected JSON from https://example.com, but the response "
"had status 200 and content type unset: <html>error</html>"
),
),
):
vws_web_tools._json_request(
session=session,
Expand Down