Skip to content

Commit 7bbaadd

Browse files
author
Mohamed Khaled
committed
Remove unnecessary ClientException static methods and add status mapping
1 parent 01bafd9 commit 7bbaadd

File tree

5 files changed

+29
-59
lines changed

5 files changed

+29
-59
lines changed

src/Providers/Http/Exception/ClientException.php

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,57 +47,6 @@ public function getRequest(): Request
4747
return $this->request;
4848
}
4949

50-
/**
51-
* Creates a ClientException from a 400 Bad Request response.
52-
*
53-
* @since n.e.x.t
54-
*
55-
* @param string $errorDetail Details about what made the request bad.
56-
* @return self
57-
*/
58-
public static function fromBadRequestResponse(string $errorDetail = 'Invalid request parameters'): self
59-
{
60-
$message = sprintf('Bad request (400): %s', $errorDetail);
61-
return new self($message, 400);
62-
}
63-
64-
/**
65-
* Creates a ClientException from a bad request.
66-
*
67-
* @since n.e.x.t
68-
*
69-
* @param RequestInterface $psrRequest The PSR-7 request that failed.
70-
* @param string $errorDetail Details about what made the request bad.
71-
* @return self
72-
*/
73-
public static function fromBadRequest(
74-
RequestInterface $psrRequest,
75-
string $errorDetail = 'Invalid request parameters'
76-
): self {
77-
$request = Request::fromPsrRequest($psrRequest);
78-
$message = sprintf('Bad request to %s (400): %s', $request->getUri(), $errorDetail);
79-
80-
$exception = new self($message, 400);
81-
$exception->request = $request;
82-
return $exception;
83-
}
84-
85-
/**
86-
* Creates a ClientException from a bad request to a specific URI.
87-
*
88-
* @since n.e.x.t
89-
*
90-
* @param string $uri The URI that was requested.
91-
* @param string $errorDetail Details about what made the request bad.
92-
* @return self
93-
*
94-
* @deprecated Use fromBadRequest() with RequestInterface for better type safety
95-
*/
96-
public static function fromBadRequestToUri(string $uri, string $errorDetail = 'Invalid request parameters'): self
97-
{
98-
return new self(sprintf('Bad request to %s (400): %s', $uri, $errorDetail), 400);
99-
}
100-
10150
/**
10251
* Creates a ClientException from a client error response (4xx).
10352
*
@@ -111,9 +60,22 @@ public static function fromBadRequestToUri(string $uri, string $errorDetail = 'I
11160
*/
11261
public static function fromClientErrorResponse(Response $response): self
11362
{
63+
$statusCode = $response->getStatusCode();
64+
$statusTexts = [
65+
400 => 'Bad Request',
66+
401 => 'Unauthorized',
67+
403 => 'Forbidden',
68+
404 => 'Not Found',
69+
422 => 'Unprocessable Entity',
70+
429 => 'Too Many Requests',
71+
];
72+
73+
$statusText = $statusTexts[$statusCode] ?? 'Client Error';
74+
11475
$errorMessage = sprintf(
115-
'Client error (%d): Request was rejected due to client-side issue',
116-
$response->getStatusCode()
76+
'Client error (%d %s): Request was rejected due to client-side issue',
77+
$statusCode,
78+
$statusText
11779
);
11880

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

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

Lines changed: 2 additions & 2 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): Request was rejected due to client-side issue');
64+
$this->expectExceptionMessage('Client error (400 Bad Request): Request was rejected due to client-side issue');
6565

6666
ResponseUtil::throwIfNotSuccessful($response);
6767
}
@@ -88,7 +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 " .
91+
"/^Client error \\({$statusCode} [^)]+\\): Request was rejected due to " .
9292
"client-side issue( - {$expectedMessagePart})?$/"
9393
);
9494

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleImageGenerationModelTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ public function testGenerateImageResultApiFailure(): void
221221
$model = $this->createModel();
222222

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

226228
$model->generateImageResult($prompt);
227229
}
@@ -614,7 +616,9 @@ public function testThrowIfNotSuccessfulFailure(): void
614616
$model = $this->createModel();
615617

616618
$this->expectException(ClientException::class);
617-
$this->expectExceptionMessage('Client error (404): Request was rejected due to client-side issue - Not Found');
619+
$this->expectExceptionMessage(
620+
'Client error (404 Not Found): Request was rejected due to client-side issue - Not Found'
621+
);
618622

619623
$model->exposeThrowIfNotSuccessful($response);
620624
}

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleModelMetadataDirectoryTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ function (string $modelId) {
109109
);
110110

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

114116
$directory->listModelMetadata();
115117
}

tests/unit/Providers/OpenAiCompatibleImplementation/AbstractOpenAiCompatibleTextGenerationModelTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ public function testGenerateTextResultApiFailure(): void
161161
$model = $this->createModel();
162162

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

166168
$model->generateTextResult($prompt);
167169
}

0 commit comments

Comments
 (0)