Skip to content

AI: Wrap SDK constructor exceptions into WP_Error in prompt builder#11114

Closed
gziolo wants to merge 4 commits intoWordPress:trunkfrom
gziolo:fix/ai-client-constructor-exception-handling
Closed

AI: Wrap SDK constructor exceptions into WP_Error in prompt builder#11114
gziolo wants to merge 4 commits intoWordPress:trunkfrom
gziolo:fix/ai-client-constructor-exception-handling

Conversation

@gziolo
Copy link
Member

@gziolo gziolo commented Mar 2, 2026

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

Summary

  • Wraps the SDK's PromptBuilder constructor call in a try/catch inside WP_AI_Client_Prompt_Builder::__construct(), so invalid input (empty string, empty array, invalid type) is stored as a WP_Error instead of throwing an InvalidArgumentException
  • The error surfaces consistently through the fluent chain when a generating method is called, matching the existing __call() behavior
  • Updates three existing tests to assert WP_Error directly instead of catching exceptions
  • Adds a new test verifying wp_ai_client_prompt( ' ' ) returns a builder that yields WP_Error from generate_text()

Follow-up to #10881 (comment).

Test plan

  • Verify npm run test:php -- --group ai-client passes (209 tests)

🤖 Generated with Claude Code

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>
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

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 gziolo, flixos90.

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

@gziolo gziolo requested a review from felixarntz March 2, 2026 14:21
@gziolo gziolo self-assigned this Mar 2, 2026
@github-actions
Copy link

github-actions bot commented Mar 2, 2026

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.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gziolo Thanks, LGTM! Just a tiny nit.

@felixarntz
Copy link
Member

@felixarntz felixarntz closed this Mar 3, 2026
@gziolo gziolo deleted the fix/ai-client-constructor-exception-handling branch March 3, 2026 03:32
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.

2 participants