2012-07-26 99 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(); 
}