Handle non-JSON API responses on 2xx without fataling#2782
Open
namespacebrian wants to merge 1 commit into4.xfrom
Open
Handle non-JSON API responses on 2xx without fataling#2782namespacebrian wants to merge 1 commit into4.xfrom
namespacebrian wants to merge 1 commit into4.xfrom
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
stdClassinstead of letting the raw string flow through to downstream code that expects object properties. This prevents fatals from->propertyaccess on strings and garbage data from(array)casting strings.Request::request()so all callers are protected automatically. A warning is logged with diagnostics (URI, status code, Content-Type, body preview) when this occurs.jsonDecodeFailedflag toRequestOperationResultfor callers that need to distinguish a JSON decode failure from a legitimately empty response.Workflows::create()before accessinggetData()->idto throw a clear error message instead of a cryptic null property access.Test plan
terminus site:list,terminus env:info)-vvv) and the command degrades gracefully instead of fataling