2012-07-26 103 views
0

我有這樣的錯誤:捕獲可變例外

[星期三年07月25 15點48分09秒2012] [錯誤] [客戶端50.xxx.xxx.xxx] PHP致命錯誤:調用一個成員函數的getSource()的非對象在/var/www/magento/app/code/core/Mage/Catalog/Model/Product.php線1389

在此功能上:

/** 
* Get attribute text by its code 
* 
* @param $attributeCode Code of the attribute 
* @return string 
*/ 
public function getAttributeText($attributeCode) 
{ 
    return $this->getResource() 
     ->getAttribute($attributeCode) 
      ->getSource() 
       ->getOptionText($this->getData($attributeCode)); 
} 

而我想在發生錯誤時記錄$ attributeCode的值。所以我改變了功能,這一點:

public function getAttributeText($attributeCode) 
{ 
    try { 
     return $this->getResource() 
      ->getAttribute($attributeCode) 
       ->getSource() 
        ->getOptionText($this->getData($attributeCode)); 
    } catch (Exception $e) { 
     Mage::log($attributeCode); 
     throw($e); 
    } 
} 

...並重新啓動Apache,但沒有在server.log中顯示出來。如果我將throw($ e)註釋掉,異常仍然會拋出。

./lib/Zend/Rest/Server.php has set_exception_handler(array($ this,「fault」));

但請告訴我一旦你設置了某個地方,php不會忽略所有的手動異常處理。因爲那會很瘋狂。

任何想法如何才能捕獲$ attributeCode只有當異常發生?

+1

Php無法捕獲try catch塊中的致命錯誤。您可以在set_exception_handler函數中爲uncatch異常定義php行爲,因此如果您想要「php可以忽略所有異常」,則此更改會影響全局範圍。 – mrok 2012-07-26 22:37:06

回答

1

嘗試

$attributes = $this->getResource()->getAttribute($attributeCode); 
if (!empty($attributes)) { 
    return $attributes->getSource()->getOptionText($this->getData($attributeCode)); 
} else { 
    Mage::log($attributeCode); 
    throw new InvalidArgumentException('bla bla'); 
} 

//我不知道是什麼的getAttribute($ attributeCode)返回;所以而不是empty(),你可以使用is_null(),isset()

0

僅用於進行調試,我這樣做:

if (!is_object($this->getResource()->getAttribute($attributeCode)) { 
    var_dump($attributeCode); 
    exit(); 
}