Skip to content

Commit 4cc060b

Browse files
author
Mohamed Khaled
committed
Remove ResponseException::fromBadResponse and enhance ServerException with status mapping
1 parent 4e5529c commit 4cc060b

File tree

4 files changed

+23
-39
lines changed

4 files changed

+23
-39
lines changed

src/Providers/Http/Exception/ResponseException.php

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

77
use WordPress\AiClient\Common\Exception\RuntimeException;
8-
use WordPress\AiClient\Providers\Http\DTO\Response;
9-
use WordPress\AiClient\Providers\Http\Utilities\ErrorMessageExtractor;
108

119
/**
1210
* Exception class for HTTP response errors.
@@ -53,31 +51,4 @@ public static function fromInvalidData(string $apiName, string $message): self
5351
{
5452
return new self(sprintf('Unexpected %s API response: %s', $apiName, $message));
5553
}
56-
57-
/**
58-
* Creates a ResponseException from a bad HTTP response.
59-
*
60-
* This method extracts error details from common API response formats
61-
* and creates an exception with a descriptive message and status code.
62-
*
63-
* @since n.e.x.t
64-
*
65-
* @param Response $response The HTTP response that failed.
66-
* @return self
67-
*/
68-
public static function fromBadResponse(Response $response): self
69-
{
70-
$errorMessage = sprintf(
71-
'Bad status code: %d.',
72-
$response->getStatusCode()
73-
);
74-
75-
// Extract error message from response data using centralized utility
76-
$extractedError = ErrorMessageExtractor::extractFromResponseData($response->getData());
77-
if ($extractedError !== null) {
78-
$errorMessage .= ' ' . $extractedError;
79-
}
80-
81-
return new self($errorMessage, $response->getStatusCode());
82-
}
8354
}

src/Providers/Http/Exception/ServerException.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,23 @@ class ServerException extends RuntimeException
2929
* @param Response $response The HTTP response that failed.
3030
* @return self
3131
*/
32-
public static function fromServerError(Response $response): self
32+
public static function fromServerErrorResponse(Response $response): self
3333
{
34+
$statusCode = $response->getStatusCode();
35+
$statusTexts = [
36+
500 => 'Internal Server Error',
37+
502 => 'Bad Gateway',
38+
503 => 'Service Unavailable',
39+
504 => 'Gateway Timeout',
40+
507 => 'Insufficient Storage',
41+
];
42+
43+
$statusText = $statusTexts[$statusCode] ?? 'Server Error';
44+
3445
$errorMessage = sprintf(
35-
'Server error (%d): Request failed due to server-side issue',
36-
$response->getStatusCode()
46+
'Server error (%d %s): Request failed due to server-side issue',
47+
$statusCode,
48+
$statusText
3749
);
3850

3951
// Extract error message from response data using centralized utility

src/Providers/Http/Util/ResponseUtil.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use WordPress\AiClient\Providers\Http\DTO\Response;
88
use WordPress\AiClient\Providers\Http\Exception\ClientException;
99
use WordPress\AiClient\Providers\Http\Exception\RedirectException;
10-
use WordPress\AiClient\Providers\Http\Exception\ResponseException;
1110
use WordPress\AiClient\Providers\Http\Exception\ServerException;
1211

1312
/**
@@ -25,15 +24,15 @@ class ResponseUtil
2524
* - 3xx: RedirectException (redirect responses)
2625
* - 4xx: ClientException (client errors)
2726
* - 5xx: ServerException (server errors)
28-
* - Other unsuccessful responses: ResponseException (malformed responses)
27+
* - Other unsuccessful responses: RuntimeException (invalid status codes)
2928
*
3029
* @since 0.1.0
3130
*
3231
* @param Response $response The HTTP response to check.
3332
* @throws RedirectException If the response indicates a redirect (3xx).
3433
* @throws ClientException If the response indicates a client error (4xx).
3534
* @throws ServerException If the response indicates a server error (5xx).
36-
* @throws ResponseException If the response format is unexpected.
35+
* @throws \RuntimeException If the response has an invalid status code.
3736
*/
3837
public static function throwIfNotSuccessful(Response $response): void
3938
{
@@ -55,10 +54,11 @@ public static function throwIfNotSuccessful(Response $response): void
5554

5655
// 5xx Server Errors
5756
if ($statusCode >= 500 && $statusCode < 600) {
58-
throw ServerException::fromServerError($response);
57+
throw ServerException::fromServerErrorResponse($response);
5958
}
6059

61-
// Other unsuccessful responses - should be extremely rare
62-
throw ResponseException::fromBadResponse($response);
60+
throw new \RuntimeException(
61+
sprintf('Response returned invalid status code: %s', $response->getStatusCode())
62+
);
6363
}
6464
}

tests/unit/Providers/Http/Util/ResponseUtilTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ public function testThrowIfNotSuccessfulThrowsServerExceptionFor5xxErrors(
117117
$this->expectException(ServerException::class);
118118
$this->expectExceptionCode($statusCode);
119119
$this->expectExceptionMessageMatches(
120-
"/^Server error \\({$statusCode}\\): Request failed due to server-side issue( - {$expectedMessagePart})?$/"
120+
"/^Server error \\({$statusCode} [^)]+\\): Request failed due to " .
121+
"server-side issue( - {$expectedMessagePart})?$/"
121122
);
122123

123124
ResponseUtil::throwIfNotSuccessful($response);

0 commit comments

Comments
 (0)