Browse Source

also log previous exception message when available

Grégoire Paris 10 năm trước cách đây
mục cha
commit
b93160143b
2 tập tin đã thay đổi với 36 bổ sung33 xóa
  1. 14 4
      Controller/CRUDController.php
  2. 22 29
      Tests/Controller/CRUDControllerTest.php

+ 14 - 4
Controller/CRUDController.php

@@ -184,6 +184,15 @@ class CRUDController extends Controller
         return parent::render($view, $parameters, $response);
     }
 
+    private function logModelManagerException($e)
+    {
+        $context = array('exception' => $e);
+        if ($e->getPrevious()) {
+            $context['previous_exception_message'] = $e->getPrevious()->getMessage();
+        }
+        $this->getLogger()->error($e->getMessage(), $context);
+    }
+
     /**
      * List action
      *
@@ -231,7 +240,8 @@ class CRUDController extends Controller
             $modelManager->batchDelete($this->admin->getClass(), $query);
             $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
         } catch (ModelManagerException $e) {
-            $this->getLogger()->error($e->getMessage());
+
+            $this->logModelManagerException($e);
             $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
         }
 
@@ -285,7 +295,7 @@ class CRUDController extends Controller
                 );
 
             } catch (ModelManagerException $e) {
-                $this->getLogger()->error($e->getMessage());
+                $this->logModelManagerException($e);
 
                 if ($this->isXmlHttpRequest()) {
                     return $this->renderJson(array('result' => 'error'));
@@ -374,7 +384,7 @@ class CRUDController extends Controller
                     return $this->redirectTo($object);
 
                 } catch (ModelManagerException $e) {
-                    $this->getLogger()->error($e->getMessage());
+                    $this->logModelManagerException($e);
 
                     $isFormValid = false;
                 }
@@ -618,7 +628,7 @@ class CRUDController extends Controller
                     return $this->redirectTo($object);
 
                 } catch (ModelManagerException $e) {
-                    $this->getLogger()->error($e->getMessage());
+                    $this->logModelManagerException($e);
 
                     $isFormValid = false;
                 }

+ 22 - 29
Tests/Controller/CRUDControllerTest.php

@@ -660,20 +660,32 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('list?filter%5Bfoo%5D=bar', $result->getTargetUrl());
     }
 
-    public function testBatchActionDeleteWithModelManagerException()
+    private function assertLoggerLogsModelManagerException($subject, $method)
     {
-        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $exception = new ModelManagerException(
+            $message           = 'message',
+            1234,
+            new \Exception($previousExceptionMessage = 'very useful message')
+        );
 
-        $message = 'test message';
-        $modelManager->expects($this->once())
-            ->method('batchDelete')
-            ->will($this->returnCallback(function () use ($message) {
-                    throw new ModelManagerException($message);
+        $subject->expects($this->once())
+            ->method($method)
+            ->will($this->returnCallback(function () use ($exception) {
+                    throw $exception;
                 }));
 
         $this->logger->expects($this->once())
             ->method('error')
-            ->with($message);
+            ->with($message, array(
+                'exception' => $exception,
+                'previous_exception_message' => $previousExceptionMessage
+            ));
+    }
+
+    public function testBatchActionDeleteWithModelManagerException()
+    {
+        $modelManager = $this->getMock('Sonata\AdminBundle\Model\ModelManagerInterface');
+        $this->assertLoggerLogsModelManagerException($modelManager, 'batchDelete');
 
         $this->admin->expects($this->once())
             ->method('getModelManager')
@@ -934,16 +946,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
             ->with($this->equalTo('DELETE'))
             ->will($this->returnValue(true));
 
-        $message = 'test message';
-        $this->admin->expects($this->once())
-            ->method('delete')
-            ->will($this->returnCallback(function () use ($message) {
-                    throw new ModelManagerException($message);
-                }));
-
-        $this->logger->expects($this->once())
-            ->method('error')
-            ->with($message);
+        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
 
         $this->request->setMethod('DELETE');
 
@@ -1087,17 +1090,7 @@ class CRUDControllerTest extends \PHPUnit_Framework_TestCase
 
         $this->expectTranslate('flash_delete_error');
 
-        $message = 'test message';
-
-        $this->admin->expects($this->once())
-            ->method('delete')
-            ->will($this->returnCallback(function () use ($message){
-                throw new ModelManagerException($message);
-            }));
-
-        $this->logger->expects($this->once())
-            ->method('error')
-            ->with($message);
+        $this->assertLoggerLogsModelManagerException($this->admin, 'delete');
 
         $this->request->setMethod('DELETE');
         $this->request->request->set('_sonata_csrf_token', 'csrf-token-123_sonata.delete');