Question

Am new to Slim framework and struggling to set up exception handling in slim, my requirement is to redirect to an error page when something unexpected happens in my code.

Tried this code

$smartView= new \Slim\Views\Smarty();
$app = new \Slim\Slim(array(
'debug' => false,
'view' => $smartView,
'templates.path' => '../templates/',
));
$app->error(function ( Exception $e ) use ($app) {
echo "my exception print here : " . $e;
});

in my index.php file, but slim slim still calling its default exception handler.

This is my router call

$app->get('/game', function () use ($app) {
   try{
        $facebook = new Facebook(array(
            'appId'  => appid,
            'secret' =>appsecret,
            'cookie' => true,
            'allowSignedRequest' => true
        ));

            $oStuff = new models\User ();
            $oStuff->fbLogin($facebook); // To get User details and game select
   }
    catch (\Exception $e) {
        //echo 'Caught exception: ',  $e->getMessage(), "\n";
       echo $e;
       echo "catch exception";
    }
});

this is my function having some errors

 public function fbLogin($facebook)
    { 
      $app = \Slim\Slim::getInstance(); 
      $user = $facebook->getUser() // here is syntax error so i need to get it in my  exception

 }

Please help me to solve this issue, thanks in advance

Was it helpful?

Solution

If you are catching yourself an Exception like you do with you catch statement you won't let Slim handle the Exception for you so you will never enter your custom error method.

You can see official statement about error handling here and also check the code source here at line 1405.

So you have 2 choices here :

1) not try/catch your exception and let all exceptions be handled by Slim Framework

2) try/catch and throw a new Exception in your catch ... (not sure if its very useful)

Also consider the debug flag when bootstrapping the app, if true you will have a complete stacktrace of your exception, if false you need to display something nice to user in your "error" method.

You also can write your own log write so you will log Exception by yourself. More infos here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top