I posting this here because, although I have already found a solution, it might be worth to know whether some other people has faced the same problem and can provide a better explanation for this. The issue is as follows:
A function receives an array as parameter, and depending of the content of a given key-value pair, does one action or another:
if ($info['password'] != "") {
// action 1
}else{
// action 2
}
It also returns the value of this key at the end:
return array("user" => $user, "pwd" => $info['password']);
Therefore, if $info['password'] is not set, it should return this error:
ContextErrorException. Undefined index: password
This happens when I run my application locally in development mode, using app/console server:run, but it gets masked when deploying in production server or also when running server:run --env=prod: It simply does not show any error nor warning. Is this an expected behaviour?
Has this something to do with Symfony configuration or is it a php.ini related issue?
It makes sense from a practical point of view, the application returns an empty field but it does not crash and the user does not realize of the bug, but it might be very bad when trying to debug the application.
Related
Consider the following PHP template:
<?= var_dump($myVariable) ?>
This is rendered with the following controller code:
public function myAction() {
return $this->render("MyBundle::test.html.php");
}
Obiously, our variable $myVariable is not set in the template. When looking at this page in the dev environment we get an exception telling us that this variable is not defined. When looking at the page in the prod enviroment we get the output null.
I have debugged this for quite a bit but have not found the place where Symfony decides which variables need to be initialized with null. What happens behind the scenes is that Symfony calls export on the array of view variables (which is empty in our case) and then calls require on the view template itself. Funnily enough, when debugging the view, the variable is never defined, neither in dev nor in prod. But still there is different output for these two environments.
So my questions are:
How does Symfony do this?
Is this intended behavior?
Is this documented somewhere and I just missed it?
Is there a way to change this behavior so that it will also fail in prod and write an error to the log file?
I've only just started with PHP and Symfony, but it seems to me it goes something like this:
Symfony will call the require on the template
Your template starts executing
PHP tries to evaluate $myVariable
PHP will trigger E_NOTICE level error as described in PHP Manual
Here is where different environments come into play. Error level, which is defined under debug.error_handler.throw_at key in debug.xml is -1 (ALL) for dev and 0 (NONE) for prod. So for dev the ErrorHandler in Symfony will throw, for production it won't.
Having a weird issue. I have a portal I have developed which up until today has been working good in both the prod and dev environments. Today after running a composer update everything works great on dev environment but when I pushed it to production one of my pages stopped working. The page allows me to pull up a user account and modify certain fields(pretty straight forward).
As an example/test I have created a test function in the controller to spit out a specific users information:
public function getTest(){
$user = Sentry::findUserByLogin('XXXXX');
echo $user;
}
When I run this function on the dev side it shows all the information its suppose to. When on the production side I get this error:
production.ERROR: 500 - Method Cartalyst\Sentry\Users\Eloquent\User::__toString() must not throw an exception # /employees/admin/test
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Method Cartalyst\Sentry\Users\Eloquent\User::__toString() must not throw an exception' in E:\sites\yaya.com\portal\app\src\employees\controllers\admin.php:0
Stack trace:
[internal function]: Illuminate\Exception\Handler->handleShutdown()
{main} [] []
I have not modified anything inside of Sentry or Eloquent.
Any ideas on what would cause this error? On the production side if I do a var_dump(user) it wont throw the error and I can see everything inside the protected fields. Or if I do echo $user->username it also works just fine. Problem is I need to return the user information as well as the groups the user is in to my JS to display it on my screen depending on the drop down selected for the user.
Finally figured it out! Not sure what happened but I started comparing rows of data in the database and found a few discrepancies. Pulled up the design view and sure enough a couple fields were different on production then my dev enviroment. Instead of "datetime" they were set to "smalldatetime" not sure what would have caused that but changed it to "datetime" and it all started working.
Thanks for the help Werewolf!
The most general case when this error is produced is mistype, when, f.e. instead of
{{ $user->id }}
Someone types
{{ $user }}
So error clearly says that method toString was called.
I'm analyzing some PHP code which is running on a server I don't have full access to. I can read the phpinfo though. The code seems to run fine on the server. In my local environment I just can't get the code to run as I get a "Catchable Fatal Error" at some call of a method using type hinting.
someMethod(string $str) {
// Do something...
}
The error says the following: "Argument 1 passed to ... must be an instance of path\of\namespace\string, string given ...".
There is no use keyword with a string class nor can I find anything trough a grep command in the folders of the development environment.
Are there any PHP modules, extensions that can make such a type hinting work? The server and my development environment are using PHP 5.4.25.
What could the live system possibly provide to make such code run? Might it use some other programming language based on PHP like Hack? The rest of the code is pretty straight PHP!
You mention that there is no use statement or namespace declaration that alludes to a 'string' class in the code. Does the code use an Autoloader?
Two possible issues at hand here:
Paths - It is possible that the live environment has another path set up, and/or that a file/class is being loaded/searched for via this 'other path' (outside the code you may be looking at).
Error Handling - Another possible cause is if there is an error handler in the production environment that always returns true.
I had this exact issue where a type-hint wasn't resolving in development, but I didn't realize until we pushed it live and the error handler was no longer registered.
Obviously a fatal parse error can not be stopped, but it turns out a catch-able error can be ignored if the error handler returns true. And this is what I am putting my money on in your case.
Additionally, it is very important to note that there is no way to type-hint a scalar, as one user pointed out.
A simpler way to say it is "it is not possible to type-hint anything that can be represented by a string." This is due to the way in which PHP handles it's more primitive variables, they can all be type-cast to one another, and therefore (because 1, "1" and true can all be == 1, == '1' and == true) it is not actually possible for the interpreter as it is written to actually catch and enforce scalar type-hints.
Answer this question: Is that variable supposed to be $str = "something"; or $str = new string(); (ie, a string or an object)?
If it is supposed to be a string, remove the type-hint, as nothing in PHP allows this support (save for HHVM, but you would know this if you were using it).
Since you say you do not have access to the code, I suggest notifying someone who does.
I need to couple my application with its database. To do this, I've generated an entity with doctrine:generate:entity. It's produced an entity with appropriate annotations for mapping. I've also used doctrine:schema:update --force to actually create the schema on the database server, which I can confirm it has done with phpMyAdmin.
In my controller I'm trying to simply insert a row like so:
public function testAction() {
$file = new File();
$file->setTest('A Foo Bar');
$em = $this->getDoctrine()->getManager();
$em->persist($file);
$em->flush();
return new JsonResponse(array('foo' => 'bar'));
}
The entity only has one field called test which is string and of length 255. When I request this URL through an AJAX request in my application, it throws back a very uninformative 500 Internal Server Error:
Oops! An Error Occurred
The server returned a "500 Internal Server Error".
Something is broken. Please e-mail us at [email] and let us know what you were doing when this error occurred. We will fix it as soon
as possible. Sorry for any inconvenience caused.
The PHP error logs also don't have any error information. This controller action does work if I remove the database manipulation stuff. The application is also running in the dev environment.
Is there any way I can get a more descriptive error message to at least tell me what's wrong?
This is a default error page that you get in production. You can customize it - http://symfony.com/doc/current/cookbook/controller/error_pages.html
You can catch and read an error creating ExceptionListener - http://symfony.com/doc/current/cookbook/event_dispatcher/event_listener.html
Also you can switch to dev environment to show error text and log.
If you just want dev logs but prod environment, you can copy contents of monolog section app/config/config_dev.yml to app/config/config_prod.yml.
If anyone here comes across this issue and is lost, the cause in my case was that I had specified a new #ORM\ManyToOne relationship and was persisting instead of merging the entity.
This in the past has returned a valid error, but in this case the php script was not handling it at all.
This is probably a yii specific question, even though I wouldn't surprised if experienced non-Yii we developers will be also able to contribute.
I often encounter the following situation:
My application fails due to a fatal error on the php level. Something like $var->property when $var==null
I want to understand how $var came to be ==null.
I'd use logs for this, however, problem is that no logs are left when a request is ended due to php error.
edit: this only happens for fatal errors. For other php errors I have my logs back
An example:
For
$nonExistingVar->someProperty;
I do have my logs recorded, as it yields PHP Error Undefined variable: nonExistingVar
However, if I do define the variable and set it to null,
$tmp = null;
$tmp->prop;
Then I loose the logs, as it results in "Fatal error: Call to a member function hasErrors() on a non-object"
Does anybody understand why does it happen? And how can the logger be anyway used in this situations? I tried setting autoFlush=1, doesn't help
Thanks
Gidi
The below allowed me to have my logs even on fatal errors:
function yiiCorrectShutdown()
{
Yii::app()->end();
//the following line will work as well
//Yii::app()->log->processLogs(null);
}
register_shutdown_function('yiiCorrectShutdown');
I wrote an extension class that logs in real-time thus avoiding any need to modify flow paths. I posted it here on the Yii Wiki