In ReactPhp, when using the Browser class, we do requests like this:
$browser
->post( $uri, $headers, $body )
->then
(
function (Psr\Http\Message\ResponseInterface $response)
{
var_dump((string)$response->getBody());
},
function (Exception $e)
{
echo 'Error: ' . $e->getMessage() . PHP_EOL;
}
);
Documented here https://github.com/reactphp/http#post
Nevertheless the Exception can be of multiple types. It can be at least of type React\Http\Message\ResponseException
in the case the network connection is okey, the host is reachable, the request has been accepted and the response has been a status different from 200 OK
(for example you get this ResponseException
if the server returns 400 Bad Request
).
In a usual try-catch block we may chain multiple exception types. If the first matches, it enters that catch block, otherwise if the next matches, it enters the second block and so on.
I'd like to to this in a cleaner way:
$browser->post( $uri, $headers, $body )
->then(
function( ResponseInterface $response )
{
// Process my OK response
},
function( \Exception $exception )
{
$exceptionClassName = get_class( $exception );
if( $exceptionClassName == ResponseException::class )
{
// Process ResponseException
}
else
{
// Process the rest of exceptions.
}
}
);
Can I chain multiple "failure" handler functions in a Promise in ReactPhp in a way the PHP or the ReactPhp engine recognizes which exception type should be processed similarly to a try-catch block with multiple catches?