From c60b3a717d953c794795c5dfc9c1661dbc9599a6 Mon Sep 17 00:00:00 2001 From: Sergey Kolodyazhnyy Date: Sat, 14 Nov 2015 16:35:11 +0100 Subject: [PATCH] Fix Web API error masking in production mode --- .../Framework/Webapi/ErrorProcessor.php | 10 ++---- .../Exception/WebapiExceptionReport.php | 34 +++++++++++++++++++ .../Webapi/Test/Unit/ErrorProcessorTest.php | 16 +++++++++ 3 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 lib/internal/Magento/Framework/Webapi/Exception/WebapiExceptionReport.php diff --git a/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php b/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php index 75b9df3174ca2..a4ad96e6d0371 100644 --- a/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php +++ b/lib/internal/Magento/Framework/Webapi/ErrorProcessor.php @@ -189,14 +189,10 @@ public function renderException(\Exception $exception, $httpCode = self::DEFAULT */ protected function _critical(\Exception $exception) { - $exceptionClass = get_class($exception); $reportId = uniqid("webapi-"); - $exceptionForLog = new $exceptionClass( - /** Trace is added separately by critical. */ - "Report ID: {$reportId}; Message: {$exception->getMessage()}", - $exception->getCode() - ); - $this->_logger->critical($exceptionForLog); + + $this->_logger->critical(new WebapiException\WebapiExceptionReport($reportId, $exception)); + return $reportId; } diff --git a/lib/internal/Magento/Framework/Webapi/Exception/WebapiExceptionReport.php b/lib/internal/Magento/Framework/Webapi/Exception/WebapiExceptionReport.php new file mode 100644 index 0000000000000..96be210c9c3f9 --- /dev/null +++ b/lib/internal/Magento/Framework/Webapi/Exception/WebapiExceptionReport.php @@ -0,0 +1,34 @@ +reportId = $reportId; + + parent::__construct( + "Report ID: {$reportId}; Message: {$exception->getMessage()}", + $exception->getCode(), + $exception + ); + } + + /** + * @return string + */ + public function getReportId() + { + return $this->reportId; + } +} diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/ErrorProcessorTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/ErrorProcessorTest.php index ab378a49c0bdf..5bfa2dbfcbdb0 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/ErrorProcessorTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/ErrorProcessorTest.php @@ -225,6 +225,22 @@ public function testMaskException($exception, $expectedHttpCode, $expectedMessag ); } + /** + * Test logged exception is the same as the thrown one in production mode + */ + public function testCriticalExceptionStackTrace() + { + $thrownException = new \Exception('', 0); + + $this->_loggerMock->expects($this->once()) + ->method('critical') + ->will($this->returnCallback(function(\Exception $loggedException) use($thrownException) { + $this->assertSame($thrownException, $loggedException->getPrevious()); + })); + + $this->_errorProcessor->maskException($thrownException); + } + /** * @return array */