phasync
HTTP client / ClientException

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#

MethodDescription
__constructAn exception about $request, which ClientException::getRequest returns.
getRequestReturn the request that failed.

Examples#

Example 1Too many redirects
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
Example 2
try {
    $client->sendRequest($request);
} catch (\Psr\Http\Client\ClientExceptionInterface $e) {
    echo $e->getMessage(), ' (cURL error ', $e->getCode(), ')';
}

See also#