[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/Providers/Http/Exception/ -> ResponseException.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\Http\Exception;
   5  
   6  use WordPress\AiClient\Common\Exception\RuntimeException;
   7  /**
   8   * Exception class for HTTP response errors.
   9   *
  10   * This is used when response data is unexpected or malformed,
  11   * typically indicating that a provider changed in ways our code
  12   * is not aware of or when parsing response data fails.
  13   *
  14   * @since 0.1.0
  15   */
  16  class ResponseException extends RuntimeException
  17  {
  18      /**
  19       * Creates a ResponseException for missing expected data.
  20       *
  21       * @since 0.2.0
  22       *
  23       * @param string $apiName The name of the API/provider.
  24       * @param string $fieldName The field that was expected but missing.
  25       * @return self
  26       */
  27      public static function fromMissingData(string $apiName, string $fieldName): self
  28      {
  29          $message = sprintf('Unexpected %s API response: Missing the "%s" key.', $apiName, $fieldName);
  30          return new self($message);
  31      }
  32      /**
  33       * Creates a ResponseException from invalid data in an API response.
  34       *
  35       * @since 0.2.0
  36       *
  37       * @param string $apiName The name of the API service (e.g., 'OpenAI', 'Anthropic').
  38       * @param string $fieldName The field that was invalid.
  39       * @param string $message The specific error message describing the invalid data.
  40       * @return self
  41       */
  42      public static function fromInvalidData(string $apiName, string $fieldName, string $message): self
  43      {
  44          return new self(sprintf('Unexpected %s API response: Invalid "%s" key: %s', $apiName, $fieldName, $message));
  45      }
  46  }


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref