Skip to content

Fix: AI Request Logs record HTTP error responses (4xx/5xx) as success - #1040

Merged
jeffpaul merged 3 commits into
WordPress:developfrom
theaminulai:fix/http-error-responses-logged-as-success
Sep 29, 2026
Merged

jeffpaul merged 3 commits into
WordPress:developfrom
theaminulai:fix/http-error-responses-logged-as-success

Conversation

@theaminulai

@theaminulai theaminulai commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

What?

Closes #1032

Fixes Logging_Http_Transporter so a non-2xx HTTP response from a provider (429, 503, etc.) is logged as error with the status code and message, instead of success. Also fixes Log_Data_Extractor::detect_request_kind() mislabeling every Gemini text generation request as Metadata.

Why?

Logging_Http_Transporter::send() initializes $status to 'success' and only changes it in the catch ( Throwable $e ) branch. The SDK's HttpTransporterInterface::send() only throws for PSR-18 network or client exceptions; a non-2xx response comes back as an ordinary Response, and the provider rejects it later (e.g. GoogleTextGenerationModel::generateTextResult() calls ResponseUtil::throwIfNotSuccessful() right after send() returns). By the time that exception surfaces to the user, the log row has already been written as success, with no status code, no error message, no token counts, and no output preview — so the AI Request Logs screen looks healthy while a feature is failing on every request.

A second, smaller problem on the same rows: detect_request_kind() returns metadata for any path containing /models/. Every Gemini generation URL has the form .../v1beta/models/{model}:generateContent, so all Google text requests were labeled Metadata instead of Text.

How?

  • includes/Logging/Logging_Http_Transporter.php: after the transporter returns a response, check Response::isSuccessful(). When it's false, set $status = 'error', build an error message with describe_error_response() (the HTTP status code, plus the provider's error.message from the JSON body when present), and record the status code in $log_data['context']['http_status'] so error rows are filterable.
  • includes/Logging/Log_Data_Extractor.php: detect_request_kind() now checks for a :generateContent / :streamGenerateContent suffix before applying the /models/ → metadata rule, so Gemini generation requests fall through to the normal text/embeddings/image/audio detection (and default to text) instead of being misclassified.
  • Added test coverage in Logging_Http_TransporterTest (non-2xx response with a JSON error body, non-2xx without a body, a normal 2xx success, and the pre-existing thrown-exception path) and in Log_Data_ExtractorTest (:generateContent and :streamGenerateContent paths).

This only touches Logging_Http_Transporter::send(), the same method #757 edits for its event-listener fallback; the two PRs will need to be rebased against each other, whichever lands second.

Testing Instructions

  1. composer install
  2. vendor/bin/phpunit --filter "Logging_Http_TransporterTest|Log_Data_ExtractorTest"

Note: I wasn't able to execute the WP-integrated PHPUnit suite in my local environment — my local WordPress core checkout has a pre-existing, unrelated fatal error (a signature mismatch between wp-includes/ai-client's WP_AI_Client_HTTP_Client and wp-includes/php-ai-client's ClientWithOptionsInterface) that breaks the bootstrap for this plugin's test suite entirely. I confirmed this reproduces identically on a clean develop checkout with none of this PR's changes, so it's an environment issue, not something introduced here. I did verify php -l, phpcs --standard=phpcs.xml.dist, and phpstan analyse all pass cleanly on the changed files, and read through the new/updated test bodies carefully against the SDK's actual Response DTO (isSuccessful(), getStatusCode(), getData()) to confirm they exercise the fix correctly. It'd be great if a maintainer (or CI) could confirm the test suite passes in a working environment.

Manual repro steps from the issue, to confirm end-to-end:

  1. Connect the Google provider and enable Request Logging.
  2. Trigger a request the provider rejects (e.g. exceed a free-tier quota for 429, or hit a 503).
  3. Open Tools > AI Request Logs and check the row: expect status error, the HTTP status code, and the provider's error message, with Request Kind Text rather than Metadata.

Changelog Entry

Fixed - AI Request Logs now records HTTP error responses (4xx/5xx) from providers as error with the status code and message, instead of success; Gemini text generation requests are no longer mislabeled as Metadata.

Open WordPress Playground Preview

Logging_Http_Transporter::send() initialized $status to 'success' and
only changed it in the catch block for thrown exceptions. The SDK's
HttpTransporterInterface::send() only throws for PSR-18 network or
client exceptions; a non-2xx response (429, 503, etc.) comes back as
an ordinary Response and is rejected later by the caller, after the
log row was already written as 'success' with no status code or error
message.

Check Response::isSuccessful() after the transporter returns and log
'error' with the HTTP status code and, when present, the provider's
JSON error message. The status code is also recorded in the log
context so error rows are filterable.

Also fix detect_request_kind() mislabeling every Gemini text
generation request as 'metadata': Google appends the method name to
the model resource path as `:generateContent` or
`:streamGenerateContent` (e.g. `/v1beta/models/{model}:generateContent`),
which matched the '/models/' metadata rule before this fix.

Fixes WordPress#1032
@theaminulai
theaminulai requested a review from a team September 17, 2026 14:12
@github-actions

github-actions Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @nielash.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: nielash.

Co-authored-by: theaminulai <theaminuldev@git.wordpress.org>
Co-authored-by: dkotter <dkotter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@codecov

codecov Bot commented Sep 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.33%. Comparing base (8f275a9) to head (ce62ff9).

Additional details and impacted files
@@              Coverage Diff              @@
##             develop    #1040      +/-   ##
=============================================
+ Coverage      80.83%   81.33%   +0.50%     
- Complexity      3007     3013       +6     
=============================================
  Files            125      125              
  Lines          12011    12025      +14     
=============================================
+ Hits            9709     9781      +72     
+ Misses          2302     2244      -58     
Flag Coverage Δ
unit 81.33% <100.00%> (+0.50%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@jeffpaul jeffpaul added this to the 1.4.0 milestone Sep 21, 2026
@jeffpaul
jeffpaul merged commit dbbe3a4 into WordPress:develop Sep 29, 2026
33 checks passed
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.

AI Request Logs: HTTP error responses (4xx/5xx) are recorded as "success"

3 participants