简体   繁体   English

在zend中显示所有异常消息,而不是显示“发生错误”?

[英]Display all exception messages in zend instead of “An error occurred”?

In Zend 2 & zend 3 I've experiencing this issue. 在Zend 2和zend 3中,我遇到了这个问题。 The following code generates "YourModule\\Controller\\ClassName" not found since i'm not imported the class ClassName. 以下代码生成未找到的“ YourModule \\ Controller \\ ClassName”,因为我没有导入ClassName。

<?php
public function indexAction() {
    $x = new ClassName() ; //I've not imported ClassName, which raise the  error.
}

When these type of some exception occurs the control goes to a catch section in onDistpach (DispatchListner). 当发生某种类型的异常时,控件将转到onDistpach(DispatchListner)中的catch部分。 If i echo $ex->getMessage() in this catch block it prints proper error message eg: "YourModule\\Controller\\ClassName" not found . 如果我在此catch块中回显$ ex-> getMessage(),它将打印正确的错误消息, 例如:“ YourModule \\ Controller \\ ClassName”未找到

public function onDispatch(MvcEvent $e)
{
    ....

} catch (\Throwable $ex) {
            $caughtException = $ex; //HERE
        //$ex->getMessage() ;
}

But the final output rendered by zend is following. 但是zend呈现的最终输出如下。 There is no information about the exception occurred. 没有有关发生异常的信息。

An error occurred An error occurred during execution; 发生错误执行期间发生错误。

please try again later. 请稍后再试。

No Exception available 没有例外

Most other cases it print proper stacktrace. 在其他大多数情况下,它会打印正确的stacktrace。 How can I configure zend to display these error messages without editing DispatchListner in Zend-Mvc ? 我如何配置zend以显示这些错误消息,而无需在Zend-Mvc中编辑DispatchListner?

Edit: I tried turning on error_reporting() & display_errors just before the exception. 编辑:我尝试在异常之前打开error_reporting()和display_errors。 Also tried try catch around the code which generate exception and still not works. 还尝试尝试捕获产生异常但仍然无法正常工作的代码。 Also my module.config. 还有我的module.config。

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions' => true,
...

Exceptions in dispatch and render can be tracked by attaching a listener to MvcEvent::EVENT_RENDER_ERROR and MvcEvent::EVENT_DISPATCH_ERROR. 通过将侦听器附加到MvcEvent :: EVENT_RENDER_ERROR和MvcEvent :: EVENT_DISPATCH_ERROR,可以跟踪分发和呈现中的异常。 This is ZF3 solution might work in ZF2 also. 这是ZF3解决方案也可能在ZF2中工作。

My Own error render function with a red title. 我自己的错误渲染功能带有红色标题。

public function exception($e) {
    echo "<span style='font-family: courier new; padding: 2px 5px; background:red; color: white;'> " . $e->getMessage() . '</span><br/>' ;
    echo "<pre>" . $e->getTraceAsString() . '</pre>' ;   
}

Attach listeners on bootstrap 在引导程序上附加侦听器

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    //Attach render errors
    $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, function($e)  {
        if ($e->getParam('exception')) {
            $this->exception( $e->getParam('exception') ) ; //Custom error render function.
        }
    } );
    //Attach dispatch errors
    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function($e)  {
        if ($e->getParam('exception')) {
            $this->exception( $e->getParam('exception') ) ;//Custom error render function.
        }
    } );
}

Set display_exceptions to true in your module.config.php file: module.config.php文件module.config.php display_exceptions设置为true

<?php
 'view_manager' => array(    
 'display_not_found_reason' => true,
 'display_exceptions'       => true, // SET TO true

Don't forget to set it back to false when your working in a production environment. 在生产环境中工作时,请不要忘记将其设置为false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM