Handling Errors - set own status codes

Hi everybody,

is there a way to set error codes for ErrorAction from YII2? For example i save data with a model and the save command will returns false. Then i would display a error message for the user so he can see there is a database problem.

At the moment, if the save command returns false, Yii shows an error page 404. But i think it must be error code 500.

I hope you unterstand me and can help me.

I think you can throw an exception.

Thank you very much Tommy,

My code is now

if($model->save()) {
... do something if save is true
} else {
   throw new \yii\web\ServerErrorHttpException;

Yii shows now the error page with the correct title (Internal Server Errror (#500) but no message text will show. The variable message is empty.

I think you can do like the example here:

That’s what i searched. Thank you very much.