|
7 | 7 | use WordPress\AiClient\Providers\Http\DTO\Response; |
8 | 8 | use WordPress\AiClient\Providers\Http\Exception\ClientException; |
9 | 9 | use WordPress\AiClient\Providers\Http\Exception\ResponseException; |
| 10 | +use WordPress\AiClient\Providers\Http\Exception\ServerException; |
10 | 11 |
|
11 | 12 | /** |
12 | 13 | * Class with static utility methods to process HTTP responses. |
|
16 | 17 | class ResponseUtil |
17 | 18 | { |
18 | 19 | /** |
19 | | - * Throws a response exception if the given response is not successful. |
| 20 | + * Throws an appropriate exception if the given response is not successful. |
20 | 21 | * |
21 | 22 | * This method checks the HTTP status code of the response and throws |
22 | | - * a ResponseException if the status code indicates an error (i.e., not in the |
23 | | - * 2xx range). It also attempts to extract a more detailed error message from |
24 | | - * the response body if available. |
| 23 | + * the appropriate exception type based on the status code range: |
| 24 | + * - 4xx: ClientException (client errors) |
| 25 | + * - 5xx: ServerException (server errors) |
| 26 | + * - Other unsuccessful responses: ResponseException (malformed responses) |
25 | 27 | * |
26 | 28 | * @since 0.1.0 |
27 | 29 | * |
28 | 30 | * @param Response $response The HTTP response to check. |
29 | | - * @throws ResponseException If the response is not successful. |
| 31 | + * @throws ClientException If the response indicates a client error (4xx). |
| 32 | + * @throws ServerException If the response indicates a server error (5xx). |
| 33 | + * @throws ResponseException If the response format is unexpected. |
30 | 34 | */ |
31 | 35 | public static function throwIfNotSuccessful(Response $response): void |
32 | 36 | { |
33 | 37 | if ($response->isSuccessful()) { |
34 | 38 | return; |
35 | 39 | } |
36 | 40 |
|
37 | | - // Check for 400 Bad Request responses indicating invalid request data |
38 | | - if ($response->getStatusCode() === 400) { |
39 | | - $body = (string) $response->getBody(); |
40 | | - $errorDetail = $body ? substr($body, 0, 200) : 'Invalid request parameters'; |
41 | | - throw ClientException::fromBadRequestResponse($errorDetail); |
| 41 | + $statusCode = $response->getStatusCode(); |
| 42 | + |
| 43 | + // 4xx Client Errors |
| 44 | + if ($statusCode >= 400 && $statusCode < 500) { |
| 45 | + // Special handling for 400 Bad Request |
| 46 | + if ($statusCode === 400) { |
| 47 | + $body = (string) $response->getBody(); |
| 48 | + $errorDetail = $body ? substr($body, 0, 200) : 'Invalid request parameters'; |
| 49 | + throw ClientException::fromBadRequestResponse($errorDetail); |
| 50 | + } |
| 51 | + // General 4xx client errors |
| 52 | + throw ClientException::fromClientError($response); |
| 53 | + } |
| 54 | + |
| 55 | + // 5xx Server Errors |
| 56 | + if ($statusCode >= 500 && $statusCode < 600) { |
| 57 | + throw ServerException::fromServerError($response); |
42 | 58 | } |
43 | 59 |
|
| 60 | + // Other unsuccessful responses (3xx redirects, etc.) - these should be rare |
| 61 | + // as most HTTP clients handle redirects automatically |
44 | 62 | throw ResponseException::fromBadResponse($response); |
45 | 63 | } |
46 | 64 | } |
0 commit comments