| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 3 declare (strict_types=1); 4 namespace WordPress\AiClient\Providers\Http\Exception; 5 6 use WordPress\AiClient\Common\Exception\InvalidArgumentException; 7 use WordPress\AiClient\Providers\Http\DTO\Request; 8 use WordPress\AiClient\Providers\Http\DTO\Response; 9 use WordPress\AiClient\Providers\Http\Util\ErrorMessageExtractor; 10 /** 11 * Exception thrown for 4xx HTTP client errors. 12 * 13 * This represents errors where the client request was malformed, 14 * unauthorized, forbidden, or otherwise invalid. 15 * 16 * @since 0.2.0 17 */ 18 class ClientException extends InvalidArgumentException 19 { 20 /** 21 * The request that failed. 22 * 23 * @var Request|null 24 */ 25 protected ?Request $request = null; 26 /** 27 * Returns the request that failed as our Request DTO. 28 * 29 * @since 0.2.0 30 * 31 * @return Request 32 * @throws \RuntimeException If no request is available 33 */ 34 public function getRequest(): Request 35 { 36 if ($this->request === null) { 37 throw new \RuntimeException('Request object not available. This exception was directly instantiated. ' . 'Use a factory method that provides request context.'); 38 } 39 return $this->request; 40 } 41 /** 42 * Creates a ClientException from a client error response (4xx). 43 * 44 * This method extracts error details from common API response formats 45 * and creates an exception with a descriptive message and status code. 46 * 47 * @since 0.2.0 48 * 49 * @param Response $response The HTTP response that failed. 50 * @return self 51 */ 52 public static function fromClientErrorResponse(Response $response): self 53 { 54 $statusCode = $response->getStatusCode(); 55 $statusTexts = [400 => 'Bad Request', 401 => 'Unauthorized', 403 => 'Forbidden', 404 => 'Not Found', 422 => 'Unprocessable Entity', 429 => 'Too Many Requests']; 56 if (isset($statusTexts[$statusCode])) { 57 $errorMessage = sprintf('%s (%d)', $statusTexts[$statusCode], $statusCode); 58 } else { 59 $errorMessage = sprintf('Client error (%d): Request was rejected due to client-side issue', $statusCode); 60 } 61 // Extract error message from response data using centralized utility 62 $extractedError = ErrorMessageExtractor::extractFromResponseData($response->getData()); 63 if ($extractedError !== null) { 64 $errorMessage .= ' - ' . $extractedError; 65 } 66 return new self($errorMessage, $statusCode); 67 } 68 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jun 13 09:38:55 2026 | Cross-referenced by PHPXref |