Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to catch this low level MySQL (?) error in PHP/Magento
    primarykey
    data
    text
    <p>When I'm executing the following statement in Magento with a really large <code>$sku</code>, the execution terminates without any errors thrown what so ever. There are no errors in either Magento's, Apache's or PHP's error logs.</p> <blockquote> <p>Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);</p> </blockquote> <p><strong>Question: How do I catch the error?</strong></p> <p>I've tried to set custom error handlers, and for testing purposes I've also managed to trigger error situations where each of the error handler functions are invoked. But when running the previously mentioned Magento code with a large <code>$sku</code>, none of the error handling functions are executed.</p> <pre><code>error_reporting( -1 ); set_error_handler( array( 'Error', 'captureNormal' ) ); set_exception_handler( array( 'Error', 'captureException' ) ); register_shutdown_function( array( 'Error', 'captureShutdown' ) ); </code></pre> <p>For completeness, this is the <code>$sku</code> I'm passing to <code>loadByAttribute()</code>. (The sku is invalid, but that is not the issue)</p> <blockquote> <p>1- 9685 0102046|1- 9685 1212100|1- 9685 1212092|1- 9685 1212096|1- 9685 1102100|1- 9685 1102108|1- 9685 1102112|1- 9685 1102092|1- 9685 0102048|1- 9685 0102054|1- 9685 0102056|1- 9685 0102058|1- 9685 1212104|1- 9685 1212108|1- 9685 0212058|1- 9685 0104050|1- 9685 0212050|1- 9685 0212056|1- 9685 0212044|1- 9685 0212048|1- 9685 0212052|1- 9685 0212054|1- 9685 1102104|1- 9685 1102124</p> </blockquote> <p>Any insight into this matter is much appreciated!</p> <p>Update: Upon further investigation, this is the exact point in the code where execution terminates. when the foreach is executed I guess Magento goes into MySQL world and starts loading up data from the database.</p> <p>\Mage\Catalog\Model\Abstract.php</p> <pre><code> public function loadByAttribute($attribute, $value, $additionalAttributes = '*') { $collection = $this-&gt;getResourceCollection() -&gt;addAttributeToSelect($additionalAttributes) -&gt;addAttributeToFilter($attribute, $value) -&gt;setPage(1,1); foreach ($collection as $object) { // &lt;--------------- HERE return $object; } return false; } </code></pre> <p><strong>Note, I'm ONLY interested in finding out how to properly CATCH these kinds of errors, not "fix" the logic</strong>. This is so that I can present a proper error message to the user. The example above with the malformed sku is contrived and I have no desire to make my Magento app work with those erroneous skus.</p> <p><strong>UPDATE: CAUSE OF ERROR</strong></p> <p>I traced the <code>load()</code> call that is triggered by the <code>foreach</code>, and it boils down to a bug Zend / PHP.</p> <p>I traced the call to \www\lib\Zend\Db\Statement.php <code>_stripQuoted($sql)</code>, and the following statement: <code>$sql = preg_replace("/$q($qe|\\\\{2}|[^$q])*$q/", '', $sql);</code> (on line 204 in Magento 1.7.0.2).</p> <p>When <code>preg_replace</code> is run with the SQL generated by the <code>load()</code> statement, the program just terminates. There is no trace in either apache's or php's log. The log works, because they get filled with other stuff.</p> <p><a href="http://framework.zend.com/issues/browse/ZF-5063" rel="nofollow">I found this bug report on the issue</a>. There they indicate it is a segmentation fault. I guess there is no way to recover from that (?).</p> <p>Since the question was explicitly about how to catch the error condition and a lot of the advice here was more about sanitizing the data I don't feel there is a clear cut "winner" for the bounty. But Franklin P Strube at least hinted at doing check deeper down the stack, so he gets it. </p> <p>Thank you for all you help guys. If you have any advice on how to handle segfaults please do share =).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload