📜 ⬆️ ⬇️

Debugging subtleties or How to (not) kill half a day with debug

Yesterday I spent half a day trying to figure out the reason for the exception in the PHP code, and in the morning it occurred to me that the reason was myself. Rather, my use of the debugger in IDE PhpStorm to trace the progress of the code.


image


Conditions of the situation - under the cut.


To begin with, the error message:


Warning: Magento\Ui\TemplateEngine\Xhtml\Result::__toString(): Not yet implemented in /.../Xhtml/Result.php on line 97 

Then the line of code number 97 mentioned in the message:


 if ('noNamespaceSchemaLocation' === $name) 

As if everything is transparent - when comparing, $name converted to a string, which leads to an exception (the idea that in this case the __toString() method for $name come to mind a little later).


Here is the method itself:


 public function __toString() { try { $templateRootElement = $this->getDocumentElement(); foreach ($templateRootElement->attributes as $name => $attribute) { if ('noNamespaceSchemaLocation' === $name) { $this->getDocumentElement()->removeAttributeNode($attribute); break; } } $templateRootElement->removeAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi'); $this->compiler->compile($templateRootElement, $this->component, $this->component); $this->appendLayoutConfiguration(); $result = $this->compiler->postprocessing($this->template->__toString()); } catch (\Exception $e) { $this->logger->critical($e->getMessage()); $result = $e->getMessage(); } return $result; } 

There is already a bit more curly - there is a try ... catch block that catches exceptions. But there is no crime either: everything that works - turns into a string by the compiler ( $result = $this->compiler->postprocessing(...) ), which does not work - turns into an error message ( $result = $e->getMessage() ).


Under the debugger in this code, I’m if ('noNamespaceSchemaLocation' === $name) to if ('noNamespaceSchemaLocation' === $name) , then $this->logger->critical($e->getMessage()) exception and $this->logger->critical($e->getMessage()) , then inexplicably jumping to \Magento\Backend\Model\Session\Interceptor::writeClose , after which the application is terminated.


Everything. All ends in water - the execution thread ends, in the browser "HTTP ERROR 500", in the logs of the server Result::__toString(): Not yet implemented .


Moreover, it is enough to simply enter the __toString() method under the debugger - and the execution of the application ends in such a bizarre way. If you skip it step over, then this piece of code works correctly. It also does not fail in it when running without a debugger.


I performed debugging in PhpStorm 2017.1.3. IDE displays a lot of useful information about the application (stacktrace, woking vars, watchers):


image


And, of course, interacts with the running application.


The reasons why there is a meeting in this case, I have not figured out. I'm still a web developer, not an IDE or interpreter developer.


“ Many things are incomprehensible to us, not because our concepts are weak, but because these things are not in the circle of our concepts. ” Kozma Prutkov (c)


In my attempts to find the source of the error, I completely ignored the influence of the IDE on the progress of the code, which resulted in half a day stomping around.


PS
This is for all those to whom the case described above now seems obvious, and who has shared his experience, if not in Habre, but on another "Internet".


Pps
While writing the article, the application under the debugger began to work out, as is expected of it. At what point it happened - it is not clear (yesterday was a working machine, today - a laptop, on which I reproduced the situation). But this does not change the essence of the article itself - “ when debugging, consider the impact of the IDE on the application’s work, especially if the behavior of the application becomes inexplicable ”.


')

Source: https://habr.com/ru/post/329428/


All Articles