Skip to content

Commit ef9a3bb

Browse files
committed
Enhance HTTP request exception message format.
1 parent 8dd8719 commit ef9a3bb

File tree

7 files changed

+36
-43
lines changed

7 files changed

+36
-43
lines changed

src/Providers/Http/Exception/ClientException.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,21 @@ public static function fromClientErrorResponse(Response $response): self
6969
429 => 'Too Many Requests',
7070
];
7171

72-
$statusText = $statusTexts[$statusCode] ?? 'Client Error';
73-
74-
$errorMessage = sprintf(
75-
'Client error (%d %s): Request was rejected due to client-side issue',
76-
$statusCode,
77-
$statusText
78-
);
72+
if (isset($statusTexts[$statusCode])) {
73+
$errorMessage = sprintf('%s (%d)', $statusTexts[$statusCode], $statusCode);
74+
} else {
75+
$errorMessage = sprintf(
76+
'Client error (%d): Request was rejected due to client-side issue',
77+
$statusCode
78+
);
79+
}
7980

8081
// Extract error message from response data using centralized utility
8182
$extractedError = ErrorMessageExtractor::extractFromResponseData($response->getData());
8283
if ($extractedError !== null) {
8384
$errorMessage .= ' - ' . $extractedError;
8485
}
8586

86-
return new self($errorMessage, $response->getStatusCode());
87+
return new self($errorMessage, $statusCode);
8788
}
8889
}

src/Providers/Http/Exception/RedirectException.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ public static function fromRedirectResponse(Response $response): self
4242
308 => 'Permanent Redirect',
4343
];
4444

45-
$statusText = $statusTexts[$statusCode] ?? 'Redirect';
46-
47-
$errorMessage = sprintf(
48-
'Redirect response (%d %s): Request needs to be retried at a different location',
49-
$statusCode,
50-
$statusText
51-
);
45+
if (isset($statusTexts[$statusCode])) {
46+
$errorMessage = sprintf('%s (%d)', $statusTexts[$statusCode], $statusCode);
47+
} else {
48+
$errorMessage = sprintf(
49+
'Redirect error (%d): Request needs to be retried at a different location',
50+
$statusCode
51+
);
52+
}
5253

5354
// Try to extract the redirect location from headers
5455
$locationValues = $response->getHeader('Location');

src/Providers/Http/Exception/ServerException.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ public static function fromServerErrorResponse(Response $response): self
4040
507 => 'Insufficient Storage',
4141
];
4242

43-
$statusText = $statusTexts[$statusCode] ?? 'Server Error';
44-
45-
$errorMessage = sprintf(
46-
'Server error (%d %s): Request failed due to server-side issue',
47-
$statusCode,
48-
$statusText
49-
);
43+
if (isset($statusTexts[$statusCode])) {
44+
$errorMessage = sprintf('%s (%d)', $statusTexts[$statusCode], $statusCode);
45+
} else {
46+
$errorMessage = sprintf(
47+
'Server error (%d): Request was rejected due to server-side issue',
48+
$statusCode
49+
);
50+
}
5051

5152
// Extract error message from response data using centralized utility
5253
$extractedError = ErrorMessageExtractor::extractFromResponseData($response->getData());

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testThrowIfNotSuccessfulThrowsClientExceptionFor400BadRequest():
6161

6262
$this->expectException(ClientException::class);
6363
$this->expectExceptionCode(400);
64-
$this->expectExceptionMessage('Client error (400 Bad Request): Request was rejected due to client-side issue');
64+
$this->expectExceptionMessage('Bad Request (400)');
6565

6666
ResponseUtil::throwIfNotSuccessful($response);
6767
}
@@ -88,8 +88,7 @@ public function testThrowIfNotSuccessfulThrowsClientExceptionFor4xxErrors(
8888
$this->expectException(ClientException::class);
8989
$this->expectExceptionCode($statusCode);
9090
$this->expectExceptionMessageMatches(
91-
"/^Client error \\({$statusCode} [^)]+\\): Request was rejected due to " .
92-
"client-side issue( - {$expectedMessagePart})?$/"
91+
"/^[A-Za-z ]+ \\({$statusCode}\\)( - {$expectedMessagePart})?$/"
9392
);
9493

9594
ResponseUtil::throwIfNotSuccessful($response);
@@ -117,8 +116,7 @@ public function testThrowIfNotSuccessfulThrowsServerExceptionFor5xxErrors(
117116
$this->expectException(ServerException::class);
118117
$this->expectExceptionCode($statusCode);
119118
$this->expectExceptionMessageMatches(
120-
"/^Server error \\({$statusCode} [^)]+\\): Request failed due to " .
121-
"server-side issue( - {$expectedMessagePart})?$/"
119+
"/^[A-Za-z ]+ \\({$statusCode}\\)( - {$expectedMessagePart})?$/"
122120
);
123121

124122
ResponseUtil::throwIfNotSuccessful($response);

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleImageGenerationModelTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public function testGenerateImageResultSuccessWithBase64JsonOutput(): void
206206
public function testGenerateImageResultApiFailure(): void
207207
{
208208
$prompt = [new Message(MessageRoleEnum::user(), [new MessagePart('A tree')])];
209-
$response = new Response(400, [], '{"error": "Bad Request"}');
209+
$response = new Response(400, [], '{"error": "Invalid parameter."}');
210210

211211
$this->mockRequestAuthentication
212212
->expects($this->once())
@@ -221,9 +221,7 @@ public function testGenerateImageResultApiFailure(): void
221221
$model = $this->createModel();
222222

223223
$this->expectException(ClientException::class);
224-
$this->expectExceptionMessage(
225-
'Client error (400 Bad Request): Request was rejected due to client-side issue - Bad Request'
226-
);
224+
$this->expectExceptionMessage('Bad Request (400) - Invalid parameter.');
227225

228226
$model->generateImageResult($prompt);
229227
}
@@ -612,13 +610,11 @@ public function testThrowIfNotSuccessfulSuccess(): void
612610
*/
613611
public function testThrowIfNotSuccessfulFailure(): void
614612
{
615-
$response = new Response(404, [], '{"error":"Not Found"}');
613+
$response = new Response(404, [], '{"error":"The resource does not exist."}');
616614
$model = $this->createModel();
617615

618616
$this->expectException(ClientException::class);
619-
$this->expectExceptionMessage(
620-
'Client error (404 Not Found): Request was rejected due to client-side issue - Not Found'
621-
);
617+
$this->expectExceptionMessage('Not Found (404) - The resource does not exist.');
622618

623619
$model->exposeThrowIfNotSuccessful($response);
624620
}

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleModelMetadataDirectoryTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function createModelMetadataStub(string $modelId)
8888
*/
8989
public function testSendListModelsRequestFailure(): void
9090
{
91-
$response = new Response(400, [], '{"error": "Bad Request"}');
91+
$response = new Response(400, [], '{"error": "Invalid parameter provided."}');
9292

9393
$this->mockRequestAuthentication
9494
->expects($this->once())
@@ -109,9 +109,7 @@ function (string $modelId) {
109109
);
110110

111111
$this->expectException(ClientException::class);
112-
$this->expectExceptionMessage(
113-
'Client error (400 Bad Request): Request was rejected due to client-side issue - Bad Request'
114-
);
112+
$this->expectExceptionMessage('Bad Request (400) - Invalid parameter provided.');
115113

116114
$directory->listModelMetadata();
117115
}

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModelTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function testGenerateTextResultSuccess(): void
146146
public function testGenerateTextResultApiFailure(): void
147147
{
148148
$prompt = [new Message(MessageRoleEnum::user(), [new MessagePart('Hello')])];
149-
$response = new Response(400, [], '{"error": "Bad Request"}');
149+
$response = new Response(400, [], '{"error": "Invalid parameter."}');
150150

151151
$this->mockRequestAuthentication
152152
->expects($this->once())
@@ -161,9 +161,7 @@ public function testGenerateTextResultApiFailure(): void
161161
$model = $this->createModel();
162162

163163
$this->expectException(ClientException::class);
164-
$this->expectExceptionMessage(
165-
'Client error (400 Bad Request): Request was rejected due to client-side issue - Bad Request'
166-
);
164+
$this->expectExceptionMessage('Bad Request (400) - Invalid parameter.');
167165

168166
$model->generateTextResult($prompt);
169167
}

0 commit comments

Comments
 (0)