GuzzleHttp asynchronous request exception

I cannot figure out how I can make an exception from the Guzzle handler future response. Here is my code:

<?php
require 'vendor/autoload.php';

$client = new \GuzzleHttp\Client();

$req = $client->createRequest('GET', 'http://www.google.com', array(
    'future' => true,
));
echo "Sending request\n";
$response = $client->send($req);

try {
    $response->then(function ($data) {
        echo "Response is received\n";
        throw new Exception('Test');
    })->then(function () {
        // success handler
    }, function (Exception $exception) {
        echo "Error handler invoked\n";
        throw $exception;
    });
} catch (Exception $e) {
    echo "Exception catched\n";
}
echo "Finish\n";

In this case, the block catchwill never be reached.

+4
source share
2 answers

promises Guzzle. then() FutureResponse , , . , , , , then, . , , , then. , . , , , . , ( ).

React promises, Guzzle, promises: https://github.com/reactphp/promise#how-promise-forwarding-works. done(), , .

+6

, script , , . script , , .

catch, script, .

$response->getStatusCode(); 

, , .

0

All Articles