Skip to content

Raise GlpiProtocolError when a success response body is not JSON - #52

Merged
baraline merged 4 commits into
mainfrom
fix/non-json-success-response
Nov 23, 2026
Merged

baraline merged 4 commits into
mainfrom
fix/non-json-success-response

Conversation

@baraline

@baraline baraline commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Problem

When a server or a proxy answered a request with status 200 but a body that is not JSON (an HTML error page, a truncated or garbled answer), the client let json.JSONDecodeError escape. That is a bare ValueError, so a caller could not tell it from a refused argument, and it carried no response facts.

Fix

  • New GlpiResponseBodyError, a subclass of GlpiProtocolError, exported from the package root. It carries status_code, url, content_type, body_length and a bounded body_excerpt. The body is kept out of the message, and the decode error is chained as __cause__.
  • Every decode of a success body goes through one helper, decode_json_body: the v2 transport, the v1 session and the OAuth token manager.
  • An empty answer on a route that accepts one (204, DELETE, v1 calls without a payload) is unchanged. An empty 200 on a route that must return JSON raises the new error.
  • The lenient helpers response_json_or_empty and response_json_mapping still read an empty or whitespace-only body as empty, but now raise the new error for a non-empty body that is not JSON.
  • The error is not retried: a 2xx means the server processed the request.
  • GlpiProtocolError is a ValueError subclass, like the error it replaces, so existing except ValueError handlers still catch it.

Tests

On both trees:

  • An HTML or garbled 200 raises the new error after exactly one request, with no retry. This is checked on the v2 transport (list and get), the v1 session (init, upload and JSON calls) and the token manager.
  • decode_json_body:
    • HTML, garbled and empty bodies raise the error;
    • the excerpt is bounded and kept out of the message;
    • valid JSON passes through untouched;
    • the error survives a pickle round trip.
  • The lenient helpers keep an empty or newline-only body empty and reject HTML.

Locally, ruff check, mypy glpi_python_client, unasync_build.py --check and the sphinx -W build pass, and so do the test files this branch touches (227 passed). The full suite and the coverage gate run in CI.

Overlaps with other open PRs

This PR only conflicts with the other open PRs (#50, #51, #53) in CHANGELOG.md: keep both entries.

Suggested merge order: #50, #51, #52, #53. After each merge I rebase the next branch: the conflicts are the CHANGELOG.md entries, plus one import line in _v1_session.py for #51.

🤖 Generated with Claude Code

baraline and others added 3 commits November 23, 2026 00:44
…onse body is not JSON, instead of a bare json.JSONDecodeError.

Every decode of a success body on the v2 transport, the v1 session and the token manager goes through decode_json_body. It carries the status, URL, content type, body length and a bounded excerpt, chains the decode error, and is not retried.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…helpers, which now raise the same error for a non-empty body that is not JSON.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… and the docs give the error excerpt in bytes, not characters.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov-commenter

codecov-commenter commented Nov 23, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.13%. Comparing base (ac11353) to head (46f9f82).
⚠️ Report is 5 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #52      +/-   ##
==========================================
+ Coverage   98.11%   98.13%   +0.01%     
==========================================
  Files          97       97              
  Lines        3875     3908      +33     
==========================================
+ Hits         3802     3835      +33     
  Misses         73       73              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@baraline
baraline merged commit f6feada into main Nov 23, 2026
6 checks passed
@baraline
baraline deleted the fix/non-json-success-response branch November 23, 2026 08:47
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.

2 participants