AI: Catch Throwable in WP_AI_Client_Prompt_Builder so TypeErrors become WP_Error#12256
AI: Catch Throwable in WP_AI_Client_Prompt_Builder so TypeErrors become WP_Error#12256itzmekhokan wants to merge 2 commits into
Conversation
…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.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Hi there! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to 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, |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
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].
|
To make it more readable and consistent, I prefer using reference: |
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 aWP_Errorfrom a generating method. The constructor and the__call()proxy enforce this with try/catch, but both only caughtException.__call()forwards caller arguments into the strict-typed php-ai-client SDK (usingTemperature(float),usingMaxTokens(int), etc. — the SDK usesstrict_types=1). A wrong argument type throws aTypeError, which extendsError, notException. The existing catch missed it, so it escaped uncaught and fataled the request (HTTP 500) instead of returning aWP_Error.Steps to reproduce
Changes
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.