File tree Expand file tree Collapse file tree 3 files changed +38
-9
lines changed Expand file tree Collapse file tree 3 files changed +38
-9
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 16
16
use WordPress \AiClient \Providers \Http \DTO \Request ;
17
17
use WordPress \AiClient \Providers \Http \DTO \Response ;
18
18
use WordPress \AiClient \Providers \Http \Exception \NetworkException ;
19
- use WordPress \AiClient \Providers \Http \Exception \RequestException ;
20
-
21
19
/**
22
20
* HTTP transporter implementation using HTTPlug.
23
21
*
@@ -89,13 +87,6 @@ public function send(Request $request): Response
89
87
);
90
88
}
91
89
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
-
99
90
return $ this ->convertFromPsr7Response ($ psr7Response );
100
91
}
101
92
Original file line number Diff line number Diff line change 5
5
namespace WordPress \AiClient \Providers \Http \Util ;
6
6
7
7
use WordPress \AiClient \Providers \Http \DTO \Response ;
8
+ use WordPress \AiClient \Providers \Http \Exception \ClientException ;
8
9
use WordPress \AiClient \Providers \Http \Exception \ResponseException ;
9
10
10
11
/**
@@ -33,6 +34,13 @@ public static function throwIfNotSuccessful(Response $response): void
33
34
return ;
34
35
}
35
36
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
+
36
44
throw ResponseException::fromBadResponse ($ response );
37
45
}
38
46
}
You can’t perform that action at this time.
0 commit comments