%%ErrorMessage%%
The above error happened when the server was processing your request.
If you think this is a server error, please contact the webmaster.
If we try to access the URL http://hostname/blog/index.php?page=EditPost&id=100, we will see the following error page because the post with ID 100 does not exist in our blog system yet. We would like to customize this error page so that it looks more consistent with the other blog pages in layout. We also want to log this kind of errors to study user behaviors. In this section, we will accomplish these two tasks.
PRADO implicitly loads a TErrorHandler module to handle errors. We would like to customize this module so that our blog system can display a customized page for errors caused by end-users. We thus modify the application configuration as follows:
The class BlogErrorHandler as specified in the above is a new error handler module that we will create next. It extends and replaces the default TErrorHandler module.
We create a file named protected/BlogErrorHandler.php as follows. The class BlogErrorHandler overrides two methods of TErrorHandler:
In the above, we specify that when a BlogException is thrown, we use a new template protected/error.html to display the error. Therefore, we need to create the BlogException class and replace all the occurances of THttpException in our code (such as EditUser and ReadPost pages). We also need to create the error template protected/error.html. The BlogException class extends THttpException and is empty. The class file is saved as protected/BlogException.php.
Below is the content in our error template protected/error.html. Note, the template is not a PRADO template because it only recognizes very limited number of tokens, such as %%ErrorMessage%%, %%ServerAdmin%%.
%%ErrorMessage%%
The above error happened when the server was processing your request.
If you think this is a server error, please contact the webmaster.
In the handleExternalError() method of BlogErrorHandler, we invoke Prado::log() to log the error if it is of type BlogException. The error is logged in memory. To save the logs into permanent medium such as file or database, we need to enable appropriate error logging routes. This is done in the application configuration as follows:
In the above, we add a log route that saves logs into a file. We also specify the category filter as BlogApplication such that only the log messages of the selected categories are saved. This helps reduce the size of the log file and also improves its readability.
To see how our blog systems reponds to an invalid user request, we test the URL http://hostname/blog/index.php?page=posts.ReadPost&id=100. We shall see the following error page which is different from what we saw earlier on.
If we search under the directory protected/runtime, we will find a file named prado.log. This is the log file that we just configured to save the error messages. The file may contain contents like the following,