M
M
Michael2017-05-30 16:56:54
Yii
Michael, 2017-05-30 16:56:54

How to handle exceptions in controller in Yii2?

I know that the question has already been raised many times. But no solutions have yet been found.
Let's take the standard controller from the example:

public function actionContact()
{
    $model = new ContactForm();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
        } else {
            Yii::$app->session->setFlash('error', 'There was an error sending your message.');
        }
        return $this->refresh();
    } else {
        return $this->render('contact', [
            'model' => $model,
        ]);
    }
}

At first glance, everything is fine. Suddenly, ContactForm may now throw an exception. By default, it is processed in a separate controller if the site is on prod and on a separate debug page if it is on dev.
All of a sudden I needed to display a message right on the error page. In this case /contact/.
This code comes to mind:
public function actionContact()
{
    $model = new ContactForm();

    try {
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
                Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
            } else {
                Yii::$app->session->setFlash('error', 'There was an error sending your message.');
            }
        }
    } catch (UserException $e) {
        Yii::$app->session->setFlash('error', $e->getMessage());
    } catch (\Exeption $e) {
        Yii::$app->session->setFlash('error', 'Something went wrong.');
    }

    return $this->render('contact', [
        'model' => $model,
    ]);
}

I haven't tested the code, but I think you get the idea. There is a problem with this code: I think it is not correct. Yes, you can move the exception handling to the base controller, but I want to know how to do it correctly from the point of view of yii.
Long work with magento 2 made me deal with exceptions. In it, they are done like this .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
Boris Korobkov, 2017-05-30
@BorisKorobkov

The config specifies the error display page for exception

'components' => [
            'errorHandler' => [
                'errorAction' => 'site/error',
            ],
]

But this is only for those cases when further operation of the application is impossible.
Form must not throw an exception. Validate() should return false in case of invalid input.
sendEmail() should also not throw exceptions. Only return true/false

A
Andrey K, 2017-06-01
@kuftachev

Or you somehow ambiguously ask a question, or not absolutely understand exceptions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question