Question

In my page (with CakePHP 2.4), I added a functionality for when there is an ajax request, if the user is not logged in, then throws a 401 Unauthorized HTTP error so ajax can catch it and knows that has to redirect to login. But the problem is that ErrorHandler doesn't catch it, and it shows it as Uncaught exception:

PHP Fatal error: Uncaught exception 'UnauthorizedException' with message 'Su sesión ha terminado. Por favor ingrese nuevamente' in D:\wamp\www\app\Controller\AppController.php:121 

The fact is that I modified some values to implement my own handler, but then I changed my mind and I reverted back to defaults, so probably I missed something. And now doesn't catch any kind of error, even 404 ones, and from ajax and normal http requests.

Here's my AppController::beforeFilter code:

public function beforeFilter()
{
    if($this->Session->check('login.language'))
    {
        Configure::write('Config.language', $this->Session->read('login.language'));
    }
    else
    {
        Configure::write('Config.language', "spa");
    }
    parent::beforeFilter();
    //Here is the login check part
    if(!$this->Auth->user('id') && $this->request->is('ajax') && !(in_array($this->request->params['action'], array('login', 'cambiar_idioma'))))
    {
        throw new UnauthorizedException(__("Su sesión ha terminado. Por favor ingrese nuevamente"));
    }

}

The handlers config in core.php:

Configure::write('Error', array(
    'handler' => 'ErrorHandler::handleError',
    'level' => E_ALL & ~E_DEPRECATED & ~E_STRICT,
    'trace' => true
));

Configure::write('Exception', array(
    'handler' => 'ErrorHandler::handleException',
    'renderer' => 'ExceptionRenderer',
    'log' => true
));

Configure::write('debug', 2);

So, any ideas why is it not catching any exceptions?

Was it helpful?

Solution

Finally got the answer. After reinstalling Cake, moving everything on my page file by file, I found that the jpGraph library was conflicting with Cake error handling. As I had the library importing at the top of the file, it was messing on every request, so I moved it to its respective function and worked as intended.

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