- md5
- 4e4a4cee00d9147c103a657c3629a531
- sha1
- 979c9eff6c9d63530394b2bb48e296a3ce2a863f
- sha256
- 77be1c2c302acd84c00a6d3707ab64e7ab820de3fa5983b66918da93754e1ca2
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Providers\Http\Exception;
use WordPress\AiClient\Common\Exception\RuntimeException;
use WordPress\AiClient\Providers\Http\DTO\Response;
use WordPress\AiClient\Providers\Http\Util\ErrorMessageExtractor;
/**
* Exception thrown for 5xx HTTP server errors.
*
* This represents errors where the server failed to fulfill
* a valid request due to internal server errors.
*
* @since 0.2.0
*/
class ServerException extends RuntimeException
{
/**
* Creates a ServerException from a server error response.
*
* This method extracts error details from common API response formats
* and creates an exception with a descriptive message and status code.
*
* @since 0.2.0
*
* @param Response $response The HTTP response that failed.
* @return self
*/
public static function fromServerErrorResponse(Response $response): self
{
$statusCode = $response->getStatusCode();
$statusTexts = [500 => 'Internal Server Error', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Timeout', 507 => 'Insufficient Storage'];
if (isset($statusTexts[$statusCode])) {
$errorMessage = sprintf('%s (%d)', $statusTexts[$statusCode], $statusCode);
} else {
$errorMessage = sprintf('Server error (%d): Request was rejected due to server-side issue', $statusCode);
}
// Extract error message from response data using centralized utility
$extractedError = ErrorMessageExtractor::extractFromResponseData($response->getData());
if ($extractedError !== null) {
$errorMessage .= ' - ' . $extractedError;
}
return new self($errorMessage, $response->getStatusCode());
}
}
The same content is in 4 releases, WordPress 7.0-beta1 to 7.0-beta4. See them all, or every version of this file.