Answer the question
In order to leave comments, you need to log in
Yii2 how to properly handle an exception?
Good afternoon.
How to properly handle exceptions, such as \SoapFault, so that the application continues to work as if nothing happened?
As I understand it now, error handlers are placed in the yii\base\ErrorHandler::register() method, which leads to undesirable results.
As a crutch, I found such a solution, and it only works for the controller of the heir \yii\web\Controller, for yii\console\Controller the exception could not be prevented at all. In general, I don’t like it at all, I’m missing something quite obvious.
try {
Yii::$app->errorHandler->unregister();
$this->_soapClient = new \SoapClient($this->wsdl,
array(
'trace' => 1,
'exceptions' => 1,
'encoding' => 'UTF-8',
'passphrase' => ''
)
);
}
catch(\SoapFault $e) {
Yii::$app->errorHandler->exception->type = null;
Yii::$app->errorHandler->register();
}
Answer the question
In order to leave comments, you need to log in
What does ErrorHandler have to do with it? ErrorHandler only fires on those exceptions that you yourself did not catch in try..catch.
If you handle an exception, you can ensure that the application continues as if nothing happened.
Here is an example:
try {
throw new \Exception('Error!');
} catch (\Exception $ex) {
}
//...Можно продолжать работу приложения, как будто не было никаких исключений
Write a function.
...
use yii\base\ViewNotFoundException;
...
try {
// 1) тут пишите то что хотите проверить
} catch (ViewNotFoundException $e) {
// 2) тут выводите "удобную" ошибку, чтобы не закрывать ошибкой 500 или той которая вылезает
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question