Skip to content

Commit 4314134

Browse files
author
Mohamed Khaled
committed
Build centralized error message extraction utility
Add ErrorMessageExtractor utility to centralize parsing of common API error response formats across exception classes. Handles standard patterns like { "error": { "message": "..." } }, { "error": "..." }, and { "message": "..." }. Implement fromPsrRequest static factory method in Request DTO to support PSR-7 to internal DTO conversion for exception context storage.
1 parent 44c8a4f commit 4314134

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Providers/Http/DTO/Request.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace WordPress\AiClient\Providers\Http\DTO;
66

77
use JsonException;
8+
use Psr\Http\Message\RequestInterface;
89
use WordPress\AiClient\Common\AbstractDataTransferObject;
910
use WordPress\AiClient\Common\Exception\InvalidArgumentException;
1011
use WordPress\AiClient\Providers\Http\Collections\HeadersCollection;
@@ -355,4 +356,29 @@ public static function fromArray(array $array): self
355356
$array[self::KEY_BODY] ?? null
356357
);
357358
}
359+
360+
/**
361+
* Creates a Request instance from a PSR-7 RequestInterface.
362+
*
363+
* @since n.e.x.t
364+
*
365+
* @param RequestInterface $psrRequest The PSR-7 request to convert.
366+
* @return self A new Request instance.
367+
* @throws InvalidArgumentException If the HTTP method is not supported.
368+
*/
369+
public static function fromPsrRequest(RequestInterface $psrRequest): self
370+
{
371+
$method = HttpMethodEnum::from($psrRequest->getMethod());
372+
$uri = (string) $psrRequest->getUri();
373+
374+
// Convert PSR-7 headers to array format expected by our constructor
375+
/** @var array<string, list<string>> $headers */
376+
$headers = $psrRequest->getHeaders();
377+
378+
// Get body content
379+
$body = $psrRequest->getBody()->getContents();
380+
$bodyOrData = !empty($body) ? $body : null;
381+
382+
return new self($method, $uri, $headers, $bodyOrData);
383+
}
358384
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Http\Utilities;
6+
7+
/**
8+
* Utility for extracting error messages from API response data.
9+
*
10+
* Centralizes the logic for parsing common API error response formats
11+
* to avoid code duplication across exception classes.
12+
*
13+
* @since n.e.x.t
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 n.e.x.t
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+
36+
// Handle { "error": { "message": "Error text" } }
37+
if (
38+
isset($data['error']) &&
39+
is_array($data['error']) &&
40+
isset($data['error']['message']) &&
41+
is_string($data['error']['message'])
42+
) {
43+
return $data['error']['message'];
44+
}
45+
46+
// Handle { "error": "Error text" }
47+
if (isset($data['error']) && is_string($data['error'])) {
48+
return $data['error'];
49+
}
50+
51+
// Handle { "message": "Error text" }
52+
if (isset($data['message']) && is_string($data['message'])) {
53+
return $data['message'];
54+
}
55+
56+
return null;
57+
}
58+
}

0 commit comments

Comments
 (0)