WordPress\AiClient\Providers\Http\Exception

ServerException::fromServerErrorResponsepublic staticWP 0.2.0

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.

Method of the class: ServerException{}

No Hooks.

Returns

self.

Usage

$result = ServerException::fromServerErrorResponse( $response ): self;
$response(Response) (required)
The HTTP response that failed.

Changelog

Since 0.2.0 Introduced.

ServerException::fromServerErrorResponse() code WP 7.0

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', 529 => 'Overloaded'];
    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());
}