Skip to content

fix: Handle non-JSON / empty error response bodies gracefully #84

@ewuerger

Description

@ewuerger

Summary

All generated _parse_response functions in open_api_client/api/**/ call response.json() unconditionally for every error HTTP status code (400, 401, 403, 404, 409, 413, 415, 500, 503). When Polarion returns an HTML error page or an empty body instead of JSON (e.g. on a 400 or 401), this raises a raw json.decoder.JSONDecodeError before the library can raise a proper PolarionApiException.

Steps to reproduce

  1. Send any PATCH/POST/GET request against a Polarion endpoint that returns a non-JSON error body (e.g. an Apache 401 HTML page).
  2. The client raises json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) instead of PolarionApiUnexpectedException.

Traceback from real pipeline run

File "polarion_rest_api_client/open_api_client/api/work_items/patch_work_item.py", line 59, in _parse_response
    response_400 = Errors.from_dict(response.json())
                                    ^^^^^^^^^^^^^^^
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Expected behaviour

A PolarionApiUnexpectedException (or PolarionApiException with the status code) should be raised instead, allowing callers to handle the error correctly and the retry mechanism to work.

Fix

Wrap every response.json() call in the generated _parse_response functions in a try/except json.JSONDecodeError and return None on failure so _raise_on_error emits PolarionApiUnexpectedException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions