Skip to content

AI: Catch Throwable in WP_AI_Client_Prompt_Builder so TypeErrors become WP_Error#12256

Open
itzmekhokan wants to merge 2 commits into
WordPress:trunkfrom
itzmekhokan:fix/ai-client-prompt-builder-catch-throwable
Open

AI: Catch Throwable in WP_AI_Client_Prompt_Builder so TypeErrors become WP_Error#12256
itzmekhokan wants to merge 2 commits into
WordPress:trunkfrom
itzmekhokan:fix/ai-client-prompt-builder-catch-throwable

Conversation

@itzmekhokan

Copy link
Copy Markdown

WP_AI_Client_Prompt_Builder's contract is that any failure during a method chain puts the builder into an error state and is surfaced as a WP_Error from a generating method. The constructor and the __call() proxy enforce this with try/catch, but both only caught Exception.

__call() forwards caller arguments into the strict-typed php-ai-client SDK (usingTemperature(float), usingMaxTokens(int), etc. — the SDK uses strict_types=1). A wrong argument type throws a TypeError, which extends Error, not Exception. The existing catch missed it, so it escaped uncaught and fataled the request (HTTP 500) instead of returning a WP_Error.

Steps to reproduce

$builder = wp_ai_client_prompt( 'Write a haiku' );
// An array can never be coerced to float -> TypeError from usingTemperature().
$result = $builder->using_temperature( array( 0.7 ) )->generate_text();
// Expected: WP_Error. Before this change: uncaught TypeError -> fatal.

Changes

  • Catch Throwable instead of Exception in the constructor and __call().
  • Widen exception_to_wp_error() to accept Throwable. Its instanceof ladder already falls through to prompt_builder_error / 500 for unrecognized types, so Error/TypeError map cleanly with no other change.
  • Add regression tests:
    • exception_to_wp_error() maps a TypeError to prompt_builder_error / 500.
    • End-to-end: passing an uncoercible argument type to a wrapped SDK method is caught and surfaced as a WP_Error from a generating method instead of fataling.

Testing instructions

Run the AI Client suite:

npm run test:php -- --group ai-client

181 tests, 463 assertions, all passing.

Trac ticket: https://core.trac.wordpress.org/ticket/65505

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Used for: Used for test cases. Reviewed and edited by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…s become `WP_Error`.

`WP_AI_Client_Prompt_Builder` promises that any failure during a method chain
puts the builder into an error state, surfaced as a `WP_Error` from a generating
method. The constructor and the `__call()` proxy enforce this with try/catch, but
both only caught `Exception`.

`__call()` forwards caller arguments into the strict-typed php-ai-client SDK
(`usingTemperature(float)`, `usingMaxTokens(int)`, etc.). A wrong argument type
throws a `TypeError`, which extends `Error`, not `Exception`, so the existing
catch missed it and the error escaped uncaught, fataling the request instead of
returning a `WP_Error`.

Catch `Throwable` instead of `Exception` in both locations, and widen
`exception_to_wp_error()` to accept `Throwable`. Its instanceof ladder already
falls through to `prompt_builder_error` / 500 for unrecognized types, so any
`Error` maps cleanly with no further change.

Adds regression tests covering the `TypeError` mapping and the end-to-end
catch path.

Props itzmekhokan.
Fixes #65505.
@github-actions

github-actions Bot commented Jun 22, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props khokansardar, gziolo, alaminfirdows.

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

@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php Outdated
Comment thread tests/phpunit/tests/ai-client/wpAiClientPromptBuilder.php Outdated
Following the widening of the catch blocks to `Throwable`, the conversion
helper and its parameter are renamed so their names reflect what they now
accept. The test helper, its reflection lookup, and call sites are updated
to match.

Follow-up to [65505].
@alaminfirdows

Copy link
Copy Markdown

To make it more readable and consistent, I prefer using $th instead of $e or $throwable. We already use $th in many other places, so it also keeps the naming consistent.

reference:

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.

3 participants