Done !
| Server IP : 51.91.236.255 / Your IP : 216.73.217.126 Web Server : Apache System : Linux webm003.cluster129.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64 User : chloega ( 433239) PHP Version : 8.4.22 Disable Function : _dyuweyrj4,_dyuweyrj4r,dl MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /home/chloega/www/wp-includes/php-ai-client/src/Providers/Http/Util/ |
Upload File : |
<?php
declare (strict_types=1);
namespace WordPress\AiClient\Providers\Http\Util;
/**
* Utility for extracting error messages from API response data.
*
* Centralizes the logic for parsing common API error response formats
* to avoid code duplication across exception classes.
*
* @since 0.2.0
* @since 0.4.0 Moved from Utilities namespace to Util namespace.
*/
class ErrorMessageExtractor
{
/**
* Extracts error message from API response data.
*
* Handles common error response formats:
* - { "error": { "message": "Error text" } }
* - { "error": "Error text" }
* - { "message": "Error text" }
*
* @since 0.2.0
*
* @param mixed $data The response data to extract error message from.
* @return string|null The extracted error message, or null if none found.
*/
public static function extractFromResponseData($data): ?string
{
if (!is_array($data)) {
return null;
}
// Handle [ { "error": { "message": "Error text" } } ]
if (isset($data[0]) && is_array($data[0]) && isset($data[0]['error']) && is_array($data[0]['error']) && isset($data[0]['error']['message']) && is_string($data[0]['error']['message'])) {
return $data[0]['error']['message'];
}
// Handle { "error": { "message": "Error text" } }
if (isset($data['error']) && is_array($data['error']) && isset($data['error']['message']) && is_string($data['error']['message'])) {
return $data['error']['message'];
}
// Handle { "error": "Error text" }
if (isset($data['error']) && is_string($data['error'])) {
return $data['error'];
}
// Handle { "message": "Error text" }
if (isset($data['message']) && is_string($data['message'])) {
return $data['message'];
}
return null;
}
}