yaf icon indicating copy to clipboard operation
yaf copied to clipboard

Feature in controller class about exceptions

Open akDeveloper opened this issue 13 years ago • 1 comments

I was thinking for a feature in controller class, about exceptions.

Yaf can catch exceptions, by setting catchException to true in application.ini, and forwards to the Error controller.

It would be nice to give the developer the chance to rescue this exception through a controller method, without turning off the usefull in most cases catchException option.

For example, an apllication may not throws only error exceptions. There are 404 not found exceptions or 401 Unauthorized exceptions or 403 Forbidden exceptions.

A method in controller rescueFromException(Exception $e) would be usefull. By default would forward to ErrorController, if catchException is on, but developer could overload it and make some checks and forward other controllers, according to exceptions.

// This method would be triggered on exceptions
public function rescueFromException(Exception $e)
{
    if (get_class($e) == 'AccessDenied') {

        $this->forward('index', 'application', 'accessDenied'); 
        // forward to accessDeniedAction and renders a different page than the error page.

        header('HTTP/1.0 401 Unauthorized');
        return false;
    } else {
        parent::rescueFromException($e);
        // forward to ErrorController
    }
}

akDeveloper avatar Jul 15 '12 22:07 akDeveloper

请使用最新版 3.0.8+php7.3 即可

letwang avatar Jun 12 '19 02:06 letwang