Skip to content

Handle non-JSON API responses on 2xx without fataling#2782

Open
namespacebrian wants to merge 1 commit into4.xfrom
non-json-parsing
Open

Handle non-JSON API responses on 2xx without fataling#2782
namespacebrian wants to merge 1 commit into4.xfrom
non-json-parsing

Conversation

@namespacebrian
Copy link
Copy Markdown
Contributor

Summary

  • When the API returns a non-JSON body on a 2xx response, Terminus now substitutes an empty stdClass instead of letting the raw string flow through to downstream code that expects object properties. This prevents fatals from ->property access on strings and garbage data from (array) casting strings.
  • The fix is centralized in Request::request() so all callers are protected automatically. A warning is logged with diagnostics (URI, status code, Content-Type, body preview) when this occurs.
  • Added a jsonDecodeFailed flag to RequestOperationResult for callers that need to distinguish a JSON decode failure from a legitimately empty response.
  • Added an explicit guard in Workflows::create() before accessing getData()->id to throw a clear error message instead of a cryptic null property access.

Test plan

  • Existing test suite passes (54 tests, 1028 assertions)
  • Verify normal API behavior is unchanged (e.g. terminus site:list, terminus env:info)
  • Verify that when a non-JSON 2xx response occurs, a warning is logged (visible with -vvv) and the command degrades gracefully instead of fataling

When the API returns a non-JSON body on a 2xx response (an API contract
violation since we send Accept: application/json), substitute an empty
stdClass instead of letting the raw string flow through. This prevents
downstream fatals where code accesses ->property on a string or casts
a string to array and gets garbage data.

- Request::request(): On 2xx JsonException, log a warning with
  diagnostics (URI, status, Content-Type, body preview) and set
  decoded_body to empty stdClass. Non-2xx responses preserve raw
  string for error messages.
- RequestOperationResult: Add jsonDecodeFailed flag so callers can
  detect when JSON decoding failed if they need to distinguish from
  a legitimately empty response.
- Workflows::create(): Add explicit guard before accessing
  getData()->id to throw a clear error instead of a cryptic null
  property access.
@namespacebrian namespacebrian requested a review from a team as a code owner February 11, 2026 05:17
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