AI: Wrap SDK constructor exceptions into WP_Error in prompt builder#11114
AI: Wrap SDK constructor exceptions into WP_Error in prompt builder#11114gziolo wants to merge 4 commits intoWordPress:trunkfrom
Conversation
Trac ticket: https://core.trac.wordpress.org/ticket/64591 When invalid input is passed to `wp_ai_client_prompt()`, the SDK's `PromptBuilder` constructor throws an `InvalidArgumentException` directly, bypassing the `__call()` exception wrapping. This catches the exception in the constructor and stores it as a `WP_Error` in `$this->error`, so it surfaces consistently through the fluent chain. Follow-up to WordPress#10881 (comment). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
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. |
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. |
felixarntz
left a comment
There was a problem hiding this comment.
@gziolo Thanks, LGTM! Just a tiny nit.
src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php
Outdated
Show resolved
Hide resolved
|
Committed in https://core.trac.wordpress.org/changeset/61787 |
Trac ticket: https://core.trac.wordpress.org/ticket/64591
Summary
PromptBuilderconstructor call in a try/catch insideWP_AI_Client_Prompt_Builder::__construct(), so invalid input (empty string, empty array, invalid type) is stored as aWP_Errorinstead of throwing anInvalidArgumentException__call()behaviorWP_Errordirectly instead of catching exceptionswp_ai_client_prompt( ' ' )returns a builder that yieldsWP_Errorfromgenerate_text()Follow-up to #10881 (comment).
Test plan
npm run test:php -- --group ai-clientpasses (209 tests)🤖 Generated with Claude Code