Exception
ClientException
A request failed for a reason that is neither an invalid request nor a network failure.
Introduction#
It is a PSR-18 ClientExceptionInterface and a \RuntimeException. The code is cURL's error
number. This class itself is thrown for too many redirects and for a response body that cannot be
decoded; NetworkException and RequestException are the two more specific kinds.
Class synopsis#
class ClientException extends RuntimeException implements \Throwable, \Stringable, ClientExceptionInterface {
/* Methods */
public ClientException::__construct(RequestInterface $request, string $message, int $code)
public ClientException::getRequest(): RequestInterface
}Methods#
| Method | Description |
|---|---|
__construct | An exception about $request, which ClientException::getRequest returns. |
getRequest | Return the request that failed. |
Examples#
use phasync\HttpClient\ClientException;
use phasync\HttpClient\HttpClient;
use phasync\Psr\Request;
use Psr\Http\Client\ClientExceptionInterface;
$base = require __DIR__ . '/../server.php';
$client = new HttpClient(['maxRedirs' => 3]);
try {
$client->sendRequest(Request::create('GET', "$base/loop")); // redirects to itself
} catch (ClientException $e) {
echo get_class($e), "\n";
var_dump($e instanceof ClientExceptionInterface);
echo 'request: ', $e->getRequest()->getMethod(), ' ', $e->getRequest()->getUri()->getPath(), "\n";
}Output
phasync\HttpClient\ClientException bool(true) request: GET /loop
try {
$client->sendRequest($request);
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
echo $e->getMessage(), ' (cURL error ', $e->getCode(), ')';
}See also#
NetworkExceptionNo response arrived, or the response stopped arriving, because of a network failure.RequestExceptionThe request could not be sent because it is invalid.HttpClient::sendRequestSend a PSR-7 request with the client's default options.