[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/src/Providers/Http/Util/ -> ResponseUtil.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\Http\Util;
   5  
   6  use WordPress\AiClient\Providers\Http\DTO\Response;
   7  use WordPress\AiClient\Providers\Http\Exception\ClientException;
   8  use WordPress\AiClient\Providers\Http\Exception\RedirectException;
   9  use WordPress\AiClient\Providers\Http\Exception\ServerException;
  10  /**
  11   * Class with static utility methods to process HTTP responses.
  12   *
  13   * @since 0.1.0
  14   */
  15  class ResponseUtil
  16  {
  17      /**
  18       * Throws an appropriate exception if the given response is not successful.
  19       *
  20       * This method checks the HTTP status code of the response and throws
  21       * the appropriate exception type based on the status code range:
  22       * - 3xx: RedirectException (redirect responses)
  23       * - 4xx: ClientException (client errors)
  24       * - 5xx: ServerException (server errors)
  25       * - Other unsuccessful responses: RuntimeException (invalid status codes)
  26       *
  27       * @since 0.1.0
  28       *
  29       * @param Response $response The HTTP response to check.
  30       * @throws RedirectException If the response indicates a redirect (3xx).
  31       * @throws ClientException If the response indicates a client error (4xx).
  32       * @throws ServerException If the response indicates a server error (5xx).
  33       * @throws \RuntimeException If the response has an invalid status code.
  34       */
  35      public static function throwIfNotSuccessful(Response $response): void
  36      {
  37          if ($response->isSuccessful()) {
  38              return;
  39          }
  40          $statusCode = $response->getStatusCode();
  41          // 3xx Redirect Responses
  42          if ($statusCode >= 300 && $statusCode < 400) {
  43              throw RedirectException::fromRedirectResponse($response);
  44          }
  45          // 4xx Client Errors
  46          if ($statusCode >= 400 && $statusCode < 500) {
  47              throw ClientException::fromClientErrorResponse($response);
  48          }
  49          // 5xx Server Errors
  50          if ($statusCode >= 500 && $statusCode < 600) {
  51              throw ServerException::fromServerErrorResponse($response);
  52          }
  53          throw new \RuntimeException(sprintf('Response returned invalid status code: %s', $response->getStatusCode()));
  54      }
  55  }


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