【问题标题】:How can I pass a error 500 message to my error template?如何将错误 500 消息传递给我的错误模板?
【发布时间】:2014-03-22 20:21:18
【问题描述】:

使用 Syfmony2,我创建了自定义错误模板,在我的例子中是

/app/Resources/TwigBundle/views/Exception/error.html.twig

看起来像这样

<html>
  </body>
    <h1>ERROR {{ status_code }}</h1>
    <p>{{ status_text }}</p>
  </body>
</html>

当我现在抛出带有消息的错误时:

throw new Exception('There is something wrong in the state of Denkmark.');

我希望消息显示在呈现的错误模板上。相反,它只显示一条标准消息:

内部服务器错误

但是,当我在开发模式下运行时,它会显示正确的消息(但在 Symfony2 标准错误模板上)。为什么 prod 模式下的消息被隐藏了?我在为谁写信息?对于开发日志?

(如何)我可以强制在 prod 模式下在我的模板上显示消息吗?

【问题讨论】:

  • “标准错误模板”是什么意思?进入日志?
  • 不,我的意思是这个带有甜美黑色怪物和绿色气泡的开发错误模板:i.stack.imgur.com/6TzhM.png

标签: php symfony exception error-handling


【解决方案1】:

这种行为是正确的,因为Exception 可能包含一些“内部”信息,并且在生产环境中不应显示这些信息。

你可以做的是定制 404 页面或者使用你自己的逻辑在异常发生时显示一些东西,但是你不能依赖 symfony2 标准逻辑,因为它与你的不兼容

对于自定义 404 页面,您应该通过在此处放置自己的错误页面来覆盖默认模板app/Resources/TwigBundle/views/Exception/error404.html.twig

根据您自己的逻辑:只需使用 event listener/subscriber 即可在某些异常情况下呈现页面

【讨论】:

  • 嗯,好吧,我已经假设了这样的事情。这意味着我应该创建自己的异常?可能是我自己的错误代码,例如在 460 年代不使用现有的并编写带有静态错误消息的 error460.html.twig?
  • 我不是在谈论 404 错误,我已经有一个 error404.html.twig。我正在寻找错误 500。
  • @GottliebNotschnabel:按照我之前说的实现你自己的逻辑;)
【解决方案2】:

您需要制作自定义模板 EventListener 并注册 EventListener:

// src/Acme/DemoBundle/EventListener/AcmeExceptionListener.php 
namespace Acme\DemoBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class AcmeExceptionListener
{
  public function onKernelException(GetResponseForExceptionEvent $event)
  {
     // You get the exception object from the received event
     $exception = $event->getException();
     $message = sprintf(
         'My Error says: %s with code: %s',
         $exception->getMessage(),
         $exception->getCode()
    );

    // Customize your response object to display the exception details
    $response = new Response();
    $response->setContent($message);

    // HttpExceptionInterface is a special type of exception that
    // holds status code and header details
    if ($exception instanceof HttpExceptionInterface) {
         $response->setStatusCode($exception->getStatusCode());
         $response->headers->replace($exception->getHeaders());
    } else {
         $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
    }

    // Send the modified response object to the event
    $event->setResponse($response);
  }
}

你需要注册监听器:

# app/config/config.yml
services:
    kernel.listener.your_listener_name:
        class: Acme\DemoBundle\EventListener\AcmeExceptionListener
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method:    
onKernelException }

来源: http://symfony.com/doc/current/cookbook/service_container/event_listener.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-27
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2017-07-10
    • 2022-01-15
    相关资源
    最近更新 更多