2017-03-20 147 views
0

我們正在處理ZF2異常處理並將所有異常記錄到日誌文件中。 我們沒有使用$e->getFile() and $e->getLine()方法獲取文件和行號的確切詳細信息。Zend框架異常處理:異常詳細信息

是否有任何其他方法來獲得確切的文件/控制器/動作/模型和行號? 我們將文件作爲DBAdapter.php而不是確切型號名稱。

下面是代碼示例,其中試圖獲取錯誤文件和錯誤發生行的確切軌跡。 正在使用一個控制器插件,所有的異常都將記錄在錯誤日誌文件中。

Public function indexAction(){ 
      try{ 
       $model = $this->getServiceLocator()->get('Application\Model\SomeModel'); 
       $model ->wrongMethod()->toArray(); //worngMethod i.e method does not exists in model. 
      }catch(\Exception $e){ 
       return $this->LibraryPlugin()->showExceptionMessage($e, $this); 
      } 
     } 

    //LibraryPlugin – Controller Plugin 
     public function showExceptionMessage(\Exception $e, $current_obj) 
     { 
      //$e -> Exception Detials 
      $e->getMessage ->C:\xampp\htdocs\project\config\package\ZF2\library\Zend\Db\TableGateway\AbstractTableGateway.php 
      $e->getLine() -> 482 
     } 



- Exception Object 
[message:protected] => Invalid method (wrongMethod) called, caught by Zend\Db\TableGateway\AbstractTableGateway::__call() 
    [string:Exception:private] => 
    [code:protected] => 0 
    [file:protected] => C:\xampp\htdocs\project\config\package\ZF2\library\Zend\Db\TableGateway\AbstractTableGateway.php 
    [line:protected] => 482 

I can see within trace which has the exact details, but does not remain at the same index for all kinds of exceptions ie.( 
InvalidArgumentException -> $e->getTrace()[0] 
PDOException -> $e->getTrace()[2] 
InvalidQueryException-> $e->getTrace()[3] 
ServiceNotCreatedException -> $e->getTrace()[5] 
). 
Is there a way to get the exact error file name with line number, without explicitly unpacking and getting the details from trace object. 
Ex : getFile()-> SomeController.php, getLine()-> line in IndexController where the invalid method is being called. 

Thanks in advance. 
+0

顯示我們的代碼,你如何處理這些異常 – SzymonM

回答

0

這是可能的,你想追溯和獲得鏈條的例外,而不是鏈中的最後一個。

如:

$exception = $exception->getPrevious(); // grab previous exception. 

你可以抓住所有以前例外的是這樣:

while ($exception instanceof \Exception) { 
    $exceptions .= sprintf("Exception: %s\nTrace:\n%s\n", $exception->getMessage(), $exception->getTraceAsString()); 
    $exception = $exception->getPrevious(); 
}