I have my own AppCotroller and using the beforeRender method to make changes to $this->viewPath based on the desired output format.
Is there a way I can check if Cake is currently outputting an error message? If I change the viewPath and then it's displaying an error (like can't load model, etc) it will error on the error :)
By the time Cake is displaying the error it should already be too late to do something about it. Not quite sure why you'd get an error about missing models when you change the viewPath, I hope that was just an example.
You might have some luck overriding or extending the ErrorHandler to intercept errors, but I wouldn't recommend doing that. Errors don't exist to be hidden, they're there to tell you something.
Creating a custom View might be a good idea depending on what you want to do (see the MediaView as an example of an alternative view).
The best thing to do though should be to avoid triggering errors by only allowing certain, predefined views to be set or making sure that a certain view file exists before trying to invoke it.
Related
We are using SocialEngine, produced using ZendFramework. This is not directly a question about SocialEngine, though it relates. This is not specifically a question about ZendFramework either; I imagine the lessons here are broadly applicable to most PhP MVC frameworks.
We have a problem whereby apparently under conditions of high load, some parts of the website stop showing up. For example, an entire widget's content will just fail to load without any apparent errors. The lack of errors to diagnose the problem is driving me crazy.
This is not at all a question about load or optimization.
I can reproduce the similar problem by calling a nonexistant class property from the ZendFramework view, model or controller, e.g.:
<?php echo $this->article->fubar; ?>
At that point the widget it is part of will fail to display without any apparent error. This is problematic when the widget itself is the main display part of the page.
I suspect these problems are ultimately caused by a class variable which has not been initialised but the codebase is too large to identify where without better error reporting. It could also be the result of a chained object failure, e.g. something like:
<?php $this->article->getCategory()->getTitle(); ?>
... could theoretically cause this behaviour if the getCategory() method returned null.
Furthermore, I believe the dearth of error reporting to be caused by 'magic' properties within the ZendFramework's data model, which allows for dynamic class properties such that if the data model has some field "blablabla", $this->article->blablabla is populated with the contents of that field. However, if the property to be accessed is not a database field this magical behaviour does not help very much, especially if it disables normal error reporting. This also explains why $this->article->fubar(); caused the widget to fail to load in the same manner.
How can I get accurate errors out of ZendFramework when faced with this behaviour?
Have you tried to catch the error by putting below code in controller's action or widget controller file?
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
It can be possible that some of your article do not associated with any category or associated category has been deleted from category table, so $this->article()->getCategory() is coming null. It should not happen if category is required field for the article and category is exist in the category table. You can avoid the error by putting the !empty()/instanceof check for such code.
I have an Symfony2 project and from time to time, I have PHP errors in my controller. In order to find out where is the error, I would like to debug the PHP code in the controller.
What do you think is the best practice to debug a controller in Symfony2?
The error page tells you many usefull things:
The error message - What is exactly going wrong? (and something: how can I solve this?)
The line number - On which line in the controller are things going wrong?
The exception name - Hover over the name to see the fully qualified namespace. It tells you on which component something is going wrong.
The trace - You can see a full trace path which tells you much usefull things: On which class/method is the exception thrown? Which methods are executed first?
Another good practise is to create tests for your controller, both functional and unit tests. Learn more about them in the documentation.
If you need more help, it is better to specify what error you get.
As shown in the php documentation is it possible to create your own exception handler. I see this as a reasonable way to handle my user generated errors and exceptions throughout the project I am working on. Through research I have made a decent amount of progress in implementing a specific one, just for one class, including things like using ErrorException (from the first answer) and making sure to return false for error levels that I can't handle.
I have however run into a wall with making it more generic. I don't want to have to write a separate handler for every class I write (especially because the vast majority of that code is going to be the same for every one). Yet it feels like terribly bad practice to have every single error for the entire project in the same handler.
The closest I have gotten to what feels like an acceptable solution is to write a subclass of ErrorException for every class I want to handle the errors for and store messages in each of those. But even with this I am probably going about it wrong (I don't think that would be the proper place to store default error messages). Is there a universally accepted way of doing this that I have been unable to find? or is one of these ways actually the way it's generally done? or are there multiple solutions that are scalable based on the size of the project?
EDIT: Just realized I can write a generic Exception_Handler with the reused code and extend it for each set of errors I have (real herp moment for me), but it still seems like I should handle all errors in the same place. If I'm completely wrong, let me know.
EDIT 2: Decided to go with a config file containing the error messages for each class that will throw errors, then the name of the class that throws the error defines what config file is loaded to get the list of messages associated with error number. This also allows me to easily define messages that should get logged as opposed to messages that should get sent to the user (essentially specific vs. generic messages).
I guess I'll mark this as answered or something, but if I'm doing something wrong feel free to let me know, help is always appreciated.
Decided to go with a config file containing the error messages for each class that will throw errors, then the name of the class that throws the error defines what config file is loaded to get the list of messages associated with error number. This also allows me to easily define messages that should get logged as opposed to messages that should get sent to the user (essentially specific vs. generic messages).
More specifically I'll probably use an ini file that defines an array of arrays where each number contains the array of error messages for that error number. Then use a foreach with the thrown error numbers to return the error message(s).
I have a question regarding error handling in Zend. I am fairly new to zend frame work.
I am new on this project that i am working on and the previous developers didn't handle service errors and the application is fairly large so I am trying to figure out an easy way to handle all the errors the service returns, and even handle the errors when service fails.
so when ever there is an error we need to alert the user that something is wrong and show the error.
now since i will be getting that in Model, how do I handle this in elegant way so that there is not much rework to be done.
Can i create a common class and extend it? I also need to alert the user in case of any error.
I want a better way because I have more than 150 controller files and about more than 100 model files.
Thanks
For a ZF app that's using Zend_Application, you should ensure that your application.ini file has this in it:
resources.frontController.throwExceptions = false
Then any Exception that is thrown will be trapped and the ErrorController's errorAction() method will be called. This gives you a centralised place to handle errors.
I have a problem with the error page 500 of symfony (apps/%app-name%/config/error/error.html.php).
I had to learn that symfony skip the standard way of page creation completely. I had to organize all the helpers myselve.
require_once dirname(__FILE__) . '/../../../../lib/vendor/symfony/lib/helper/HelperHelper.php';
use_helper('Tag', 'Asset', 'Url', 'I18N', 'Date', 'Partial');
But now I am missing the contents of the configuration. How can I manually trigger the autoloader mechanism, so that I get the information from view.yml and so on?
I don't know if it's smart to depend on the whole Symfony stack when you're displaying an error.
IMHO the error pages need to be designed as light-weight as possible, 99% static HTML, with only some php code to display a friendly error message.
Because, what if something is wrong with your Symfony stack, then you can't even present a decent error page to the user.
The 500 error page is also a "hard fail" page, it is the most generic error handler which is only shown if no action tried to catch the exception.
In the cases where you want to use the view and everything, I think it's better to catch the exceptions right there in the action, and present better error pages (return sfView::ERROR) with information the user can act on.