Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my exception thrown from a closure not caught?

I've written a PHPUnit test that checks if an exception is thrown from a closure when a method is invoked. The closure function is passed in as an argument to the method with an exception being thrown from it.

public function testExceptionThrownFromClosure()
{
    try {
        $this->_externalResourceTemplate->get(
            $this->_expectedUrl,
            $this->_paramsOne,
            function ($anything) {
                throw new Some_Exception('message');
            }
        );

        $this->fail("Expected exception has not been found");
    } catch (Some_Exception $e) {
        var_dump($e->getMessage()); die;
    }
}

The code for the get function specified on the ExternalResourceTemplate is

public function get($url, $params, $closure)
{
    try {
        $this->_getHttpClient()->setUri($url);
        foreach ($params as $key => $value) {
            $this->_getHttpClient()->setParameterGet($key, $value);
        }
        $response = $this->_getHttpClient()->request();
        return $closure($response->getBody());
    } catch (Exception $e) {
        //Log
        //Monitor
    }
}

Any ideas why the fail assert statement is called? Can you not catch exceptions thrown from closures in PHP or is there a specific way of dealing with them I don't know about.

For me the exception should just propagate out the return stack, but it doesn't appear to. Is this a bug? FYI I'm running PHP 5.3.3

like image 848
James Murphy Avatar asked Jun 21 '13 08:06

James Murphy


1 Answers

Thanks for the answers...

Managed to figure out the issue. It looks like the problem is that the try-catch block that's being invoked is the one where the closure is invoked. Which makes sense...

So the code above should be

public function get($url, $params, $closure)
{
    try {
        $this->_getHttpClient()->setUri($url);
        foreach ($params as $key => $value) {
            $this->_getHttpClient()->setParameterGet($key, $value);
        }
        $response = $this->_getHttpClient()->request();
        return $closure($response->getBody());
    } catch (Exception $e) {
        //Log
        //Monitor
        throw new Some_Specific_Exception("Exception is actually caught here");
    }
}

So it looks like PHP 5.3.3 doesn't have a bug after all which was mentioned. My mistake.

like image 77
James Murphy Avatar answered Nov 08 '22 00:11

James Murphy