Answer the question
In order to leave comments, you need to log in
How to make try { action } catch ( Exception ) by default?
In the Yii2 project, the controllers that process POST requests are separately rendered.
Now, in each action of these API requests, there is a try-catch block, where any Exception is caught in catch and its message with the error key is given as an array.
The afterAction has the JSON response format set, all arrays are returned as json, in case of an exception there will be an error key.
Is it possible somehow by default to take all the actions of such controllers into a try-catch block, so as not to write inside each?
I tried to make the actionError () method, but exceptions are triggered in behaviors and the page is rendered in general .. incorrect behavior.
Answer the question
In order to leave comments, you need to log in
Maybe it makes sense to wrap init()?
the controller also has a runAction() method; it can also be wrapped, because yii2 calls this method.
public function runAction($id, $params = [])
{
try{
return parent::runAction($id, $params); // TODO: Change the autogenerated stub
}catch(Exception $e){
echo "hello world"
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question