I have a small app that serves a text file when a user clicks a download button. The problem is, if some fields in the database are missing, the download should fail, the file won't be sent and the user should receive an error message that explains what went wrong. My app is written using Yii2 but the specific framework does not matter. How can I show a nice pretty error message to the user? Right now I just throw a "404 not found exception". What are possible solutions to my problem, either server-side or client-side?
Just add a flash message on the top of your view. Refer to the following link to get an understanding. http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/
You can add a flash message by just adding any of these lines.
Yii::app()->user->setFlash('success', "Data1 saved!"); Yii::app()->user->setFlash('error', "Data2 failed!"); Yii::app()->user->setFlash('notice', "Data3 ignored.");
And you can call it in a preferred place of your view as follows.calling it at top is advisable.
<?php
foreach(Yii::app()->user->getFlashes() as $key => $message) {
echo '<div class="flash-' . $key . '">' . $message . "</div>
";
}
?>