Hi Guys!
I understand that the errorHandler will catch errors like the below one with disabled debug mode…
But when you are in development environment with debug enabled,
how to ensure that - for example - $model->link() worked??
Lets say for example you have following code:
// $master is empty by purpose in this example
// because I want to generate an error for understanding my question
$master = new Master;
$slave = new Slave;
// some code to fill all attributes for slave except FK for example:
$slave->attributes = Yii::$app->request->post('Slave');
// now I link slave to master with
$slave->link('master', $master);
But how can I verify / catch excpection?
That it was successfully linked when in debug mode?
And how can I react when it was not linked?
For example I want something like:
if($slave->link('master', $master)){
Yii::$app->session->setFlash('success','This slave has now a master! YEEY!');
}
else{
Yii::$app->session->setFlash('error', 'This slave could not be saved.');
}
// redirect or whatever
Since “link” does not return true / false I can’t use IF.
And try - catch block only works with general php exception like:
try{
$slave->link('master', $master);
Yii::$app->session->setFlash('success','This slave has now a master! YEEY!');
}
catch(\Exception $e){
Yii::$app->session->setFlash('error', "$e");
}
// more code
But if I use "general exception" the errorHandler will not lead to site/error when debug is disabled.
What is the best way?
How you guys are doing this?
Best Regards