Try/catch for PDO is being handled by Slim Error Handling - SOLVED


#1

I’m testing my code and my Try/Catch statements are being ignored by Slim 3. The default error handling is kicking in and the catch statement is not executed at all.

Here is the test code in the routes.php file.

$app->get(’/readall’, function ($request, $response, $args)
{
$this->logger->info("/readall ‘/’ route");

$memberDB = $this->db;
$queryMembers = $memberDB->prepare("select * from slm.member limit 10");
try
{
	$queryMembers->execute();
	$members = $queryMembers->fetchAll();
} catch (\Exeption $e)
{
	$members = $e;
}
$this->logger->info(serialize($members));

$body = $response->getBody();
$body->write(serialize($members));
return $response->withBody($body);

});

Here is my dependencies.php file:
// database - postgres
$container[‘db’] = function ($c)
{
$settings = $c->get(‘settings’)[‘db’];
$pdo = new PDO(‘pgsql:host=localhost;dbname=slmdb;port=5432’, ‘XXXXXXX’, ‘XXXXXXX’);
$pdo->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
};

I have slm.members changed to slm.member in the above code to test the catch. slm.member does not exist and slm.members does exist. slm.members version works and returns two rows.

I have tried the above code with the following variations.
} catch (\Exeption $e)
{
then change to
} catch (Exeption $e)
{
then changed to
} catch (\PDOExeption $e)
{
then changed to
} catch (PDOExeption $e)
{
None of the catch’s versions worked in debug or non-debug mode. I have also turned Slim framework error handling off with the following code and the catch still doesn’t work. In the dependencies.php file:
unset($app->getContainer()[‘errorHandler’]);
unset($app->getContainer()[‘phpErrorHandler’]);

What I’m I doing wrong? :frowning:


#2

Hello,

Just to be sure, are you sure the exception does not pop from your PDO instanciation or the serialize() ?


#3

The exception happens at the $queryMembers->execute(); line in the debugger and the standard Slim Framework error handling page is return to the browser.


#4

Spelling error in the catch was the reason.