WP_AI_Client_Prompt_Builder::exception_to_wp_error( Exception $e ): WP_Error
- Since
- 7.0.0
- Source
wp-includes/ai-client/class-wp-ai-client-prompt-builder.php:383
Description
This method maps different exception types to specific WP_Error codes and HTTP status codes.
The presence of the status codes means these WP_Error objects can be easily used in REST API responses or other contexts where HTTP semantics are relevant.
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$eException- The exception to convert.
Return value
WP_Error- The resulting WP_Error object.
Performance profile
How much work a call to WP_AI_Client_Prompt_Builder::exception_to_wp_error() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 18–29
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_AI_Client_Prompt_Builder::exception_to_wp_error() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 18–26 | ->getMessage(), status() |
!($e instanceof) && $e instanceof && !->getCode() | 24–26 | ->getCode(), ->getMessage(), status() |
!($e instanceof) && $e instanceof && ->getCode() | 27–29 | ->getCode(), ->getCode(), ->getMessage(), status() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 54 | 18–28 | 7 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 56 | 18–29 | 7 | |
| 8.4 | 56 | 18–29 | 7 | |
| 8.3 | 56 | 18–29 | 7 | |
| 8.2 | 56 | 18–29 | 7 | |
| 8.1 | 56 | 18–29 | 7 | |
| 7.4 | 56 | 18–29 | 7 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- WP_Error::__construct()Initializes the error.
Used by · 2
- WP_AI_Client_Prompt_Builder::__call()Magic method to proxy snake_case method calls to their PHP AI Client camelCase counterparts.
- WP_AI_Client_Prompt_Builder::__construct()Constructor.
Source code
private function exception_to_wp_error( Exception $e ): WP_Error { if ( $e instanceof NetworkException ) { $error_code = 'prompt_network_error'; $status_code = 503; } elseif ( $e instanceof ClientException ) { // `ClientException` uses HTTP status codes as exception codes, so we can rely on them. $error_code = 'prompt_client_error'; $status_code = $e->getCode() ? $e->getCode() : 400; } elseif ( $e instanceof ServerException ) { // `ServerException` uses HTTP status codes as exception codes, so we can rely on them. $error_code = 'prompt_upstream_server_error'; $status_code = $e->getCode() ? $e->getCode() : 500; } elseif ( $e instanceof TokenLimitReachedException ) { $error_code = 'prompt_token_limit_reached'; $status_code = 400; } elseif ( $e instanceof InvalidArgumentException ) { $error_code = 'prompt_invalid_argument'; $status_code = 400; } else { $error_code = 'prompt_builder_error'; $status_code = 500; } return new WP_Error( $error_code, $e->getMessage(), array( 'status' => $status_code, 'exception_class' => get_class( $e ), ) ); }Changelog
Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.
Signature, return type and hooks compared across 2 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/ai-client/class-wp-ai-client-prompt-builder.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.