[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClient\Providers\Http\Util;
   5  
   6  /**
   7   * Utility for extracting error messages from API response data.
   8   *
   9   * Centralizes the logic for parsing common API error response formats
  10   * to avoid code duplication across exception classes.
  11   *
  12   * @since 0.2.0
  13   * @since 0.4.0 Moved from Utilities namespace to Util namespace.
  14   */
  15  class ErrorMessageExtractor
  16  {
  17      /**
  18       * Extracts error message from API response data.
  19       *
  20       * Handles common error response formats:
  21       * - { "error": { "message": "Error text" } }
  22       * - { "error": "Error text" }
  23       * - { "message": "Error text" }
  24       *
  25       * @since 0.2.0
  26       *
  27       * @param mixed $data The response data to extract error message from.
  28       * @return string|null The extracted error message, or null if none found.
  29       */
  30      public static function extractFromResponseData($data): ?string
  31      {
  32          if (!is_array($data)) {
  33              return null;
  34          }
  35          // Handle [ { "error": { "message": "Error text" } } ]
  36          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'])) {
  37              return $data[0]['error']['message'];
  38          }
  39          // Handle { "error": { "message": "Error text" } }
  40          if (isset($data['error']) && is_array($data['error']) && isset($data['error']['message']) && is_string($data['error']['message'])) {
  41              return $data['error']['message'];
  42          }
  43          // Handle { "error": "Error text" }
  44          if (isset($data['error']) && is_string($data['error'])) {
  45              return $data['error'];
  46          }
  47          // Handle { "message": "Error text" }
  48          if (isset($data['message']) && is_string($data['message'])) {
  49              return $data['message'];
  50          }
  51          return null;
  52      }
  53  }


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