瀏覽代碼

tweaked behavior of ExceptionListener to display better error messages in case of an exception thrown during the handling of an exception

Fabien Potencier 14 年之前
父節點
當前提交
71d5be1d6b
共有 1 個文件被更改,包括 12 次插入3 次删除
  1. 12 3
      src/Symfony/Component/HttpKernel/Debug/ExceptionListener.php

+ 12 - 3
src/Symfony/Component/HttpKernel/Debug/ExceptionListener.php

@@ -48,10 +48,14 @@ class ExceptionListener
 
     public function handle(Event $event)
     {
-        if (HttpKernelInterface::MASTER_REQUEST !== $event->get('request_type')) {
+        static $handling;
+
+        if (true === $handling) {
             return false;
         }
 
+        $handling = true;
+
         $exception = $event->get('exception');
         $request = $event->get('request');
 
@@ -76,16 +80,21 @@ class ExceptionListener
         try {
             $response = $event->getSubject()->handle($request, HttpKernelInterface::SUB_REQUEST, true);
         } catch (\Exception $e) {
+            $message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage());
             if (null !== $this->logger) {
-                $this->logger->err(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()));
+                $this->logger->err($message);
+            } else {
+                error_log($message);
             }
 
             // re-throw the exception as this is a catch-all
-            throw new \RuntimeException('Exception thrown when handling an exception.', 0, $e);
+            throw $exception;
         }
 
         $event->setReturnValue($response);
 
+        $handling = false;
+
         return true;
     }
 }