S
S
Sergey Semenko2015-03-20 17:02:23
Yii
Sergey Semenko, 2015-03-20 17:02:23

How to throw an exception that will be handled by ErrorHandler?

Here I have created a filter

class FilterBannedIp extends ActionFilter {
    public function events() {
        return [Controller::EVENT_BEFORE_ACTION => 'beforeAction'];
    }

    public function beforeAction($event) {
        $bannedIp = BannedIp::findByIp(Yii::$app->request->getUserIP());

        if ($bannedIp && $bannedIp->isBanned(Yii::$app->request->getUserIP())) {
            throw new ForbiddenHttpException('Ваш IP заблокирован');
            return false;
        }

        return true;
    }
}

Here I screw it to the controller
public function init() {
    $this->attachBehavior('filterBannedIp', FilterBannedIp::className());
}

And here is what I get
exception 'yii\web\ForbiddenHttpException' with message 'Ваш IP заблокирован' in C:\OpenServer\domains\work2\basic\filters\FilterBannedIp.php:22
Stack trace:
#0 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\ActionFilter.php(71): app\filters\FilterBannedIp->beforeAction(Object(yii\web\ErrorAction))
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#2 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Component.php(538): call_user_func(Array, Object(yii\base\ActionEvent))
#3 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(259): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#4 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\Controller.php(108): yii\base\Controller->beforeAction(Object(yii\web\ErrorAction))
#5 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(149): yii\web\Controller->beforeAction(Object(yii\web\ErrorAction))
#6 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('error', Array)
#7 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\ErrorHandler.php(80): yii\base\Module->runAction('site/error')
#8 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\ErrorHandler.php(95): yii\web\ErrorHandler->renderException(Object(yii\web\ForbiddenHttpException))
#9 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\ForbiddenHttpException))
#10 {main}
Previous exception:
exception 'yii\web\ForbiddenHttpException' with message 'Ваш IP заблокирован' in C:\OpenServer\domains\work2\basic\filters\FilterBannedIp.php:22
Stack trace:
#0 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\ActionFilter.php(71): app\filters\FilterBannedIp->beforeAction(Object(yii\base\InlineAction))
#1 [internal function]: yii\base\ActionFilter->beforeFilter(Object(yii\base\ActionEvent))
#2 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Component.php(538): call_user_func(Array, Object(yii\base\ActionEvent))
#3 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(259): yii\base\Component->trigger('beforeAction', Object(yii\base\ActionEvent))
#4 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\Controller.php(108): yii\base\Controller->beforeAction(Object(yii\base\InlineAction))
#5 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Controller.php(149): yii\web\Controller->beforeAction(Object(yii\base\InlineAction))
#6 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Module.php(455): yii\base\Controller->runAction('index', Array)
#7 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\web\Application.php(83): yii\base\Module->runAction('site/index', Array)
#8 C:\OpenServer\domains\work2\basic\vendor\yiisoft\yii2\base\Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#9 C:\OpenServer\domains\work2\basic\web\index.php(12): yii\base\Application->run()
#10 {main}

How can I raise an exception that will be handled by the ErrorHandler and displayed on a special page? As I understand it, this is what happens to me, but then the ErrorAction also blocks? Then how to get around it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Makarov, 2015-03-23
@SamDark

`YII_DEBUG` = false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question