我们正在处理ZF2异常处理并将所有异常记录到日志文件中。
我们没有使用$e->getFile() and $e->getLine()
方法获取文件和行号的确切详细信息。
是否有其他方法可以获取确切的文件/控制器/操作/模型和行号? 我们将文件作为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 :(得分: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();
}