Skip to content

Commit 4a69461

Browse files
author
Mohamed Khaled
committed
Implement ClientException for 4xx responses in ResponseUtil
1 parent 7c742aa commit 4a69461

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WordPress\AiClient\Providers\Http\Exception;
6+
7+
/**
8+
* Exception thrown for 4xx HTTP client errors.
9+
*
10+
* This represents errors where the client request was malformed,
11+
* unauthorized, forbidden, or otherwise invalid.
12+
*
13+
* @since n.e.x.t
14+
*/
15+
class ClientException extends RequestException
16+
{
17+
/**
18+
* Creates a ClientException from a 400 Bad Request response.
19+
*
20+
* @since n.e.x.t
21+
*
22+
* @param string $errorDetail Details about what made the request bad.
23+
* @return self
24+
*/
25+
public static function fromBadRequestResponse(string $errorDetail = 'Invalid request parameters'): self
26+
{
27+
$message = sprintf('Bad request (400): %s', $errorDetail);
28+
return new self($message);
29+
}
30+
}

src/Providers/Http/HttpTransporter.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use WordPress\AiClient\Providers\Http\DTO\Request;
1717
use WordPress\AiClient\Providers\Http\DTO\Response;
1818
use WordPress\AiClient\Providers\Http\Exception\NetworkException;
19-
use WordPress\AiClient\Providers\Http\Exception\RequestException;
20-
2119
/**
2220
* HTTP transporter implementation using HTTPlug.
2321
*
@@ -89,13 +87,6 @@ public function send(Request $request): Response
8987
);
9088
}
9189

92-
// Check for 400 Bad Request responses indicating invalid request data
93-
if ($psr7Response->getStatusCode() === 400) {
94-
$body = (string) $psr7Response->getBody();
95-
$errorDetail = $body ? substr($body, 0, 200) : 'Invalid request parameters';
96-
throw RequestException::fromBadRequest($psr7Request, $errorDetail);
97-
}
98-
9990
return $this->convertFromPsr7Response($psr7Response);
10091
}
10192

src/Providers/Http/Util/ResponseUtil.php

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

77
use WordPress\AiClient\Providers\Http\DTO\Response;
8+
use WordPress\AiClient\Providers\Http\Exception\ClientException;
89
use WordPress\AiClient\Providers\Http\Exception\ResponseException;
910

1011
/**
@@ -33,6 +34,13 @@ public static function throwIfNotSuccessful(Response $response): void
3334
return;
3435
}
3536

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);
42+
}
43+
3644
throw ResponseException::fromBadResponse($response);
3745
}
3846
}

0 commit comments

Comments
 (0)