Skip to content

Commit 245ed10

Browse files
author
Alexander Akimov
authored
Merge pull request #869 from magento-folks/exception_logger
[Folks] Exception logger
2 parents 45f2cae + 51b1ae8 commit 245ed10

File tree

6 files changed

+127
-18
lines changed

6 files changed

+127
-18
lines changed

app/code/Magento/Authorizenet/Controller/Directpost/Payment/Place.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Magento\Payment\Model\IframeConfigProvider;
1717
use Magento\Quote\Api\CartManagementInterface;
1818
use Magento\Framework\Exception\LocalizedException;
19+
use Psr\Log\LoggerInterface;
20+
use Magento\Framework\App\ObjectManager;
1921

2022
/**
2123
* Class Place
@@ -44,26 +46,36 @@ class Place extends Payment
4446
*/
4547
protected $jsonHelper;
4648

49+
/**
50+
* Logger for exception details
51+
*
52+
* @var LoggerInterface
53+
*/
54+
private $logger;
55+
4756
/**
4857
* @param Context $context
4958
* @param Registry $coreRegistry
5059
* @param DataFactory $dataFactory
5160
* @param CartManagementInterface $cartManagement
5261
* @param Onepage $onepageCheckout
5362
* @param JsonHelper $jsonHelper
63+
* @param LoggerInterface|null $logger
5464
*/
5565
public function __construct(
5666
Context $context,
5767
Registry $coreRegistry,
5868
DataFactory $dataFactory,
5969
CartManagementInterface $cartManagement,
6070
Onepage $onepageCheckout,
61-
JsonHelper $jsonHelper
71+
JsonHelper $jsonHelper,
72+
LoggerInterface $logger = null
6273
) {
6374
$this->eventManager = $context->getEventManager();
6475
$this->cartManagement = $cartManagement;
6576
$this->onepageCheckout = $onepageCheckout;
6677
$this->jsonHelper = $jsonHelper;
78+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
6779
parent::__construct($context, $coreRegistry, $dataFactory);
6880
}
6981

@@ -127,9 +139,11 @@ protected function placeCheckoutOrder()
127139
]
128140
);
129141
} catch (LocalizedException $exception) {
142+
$this->logger->critical($exception);
130143
$result->setData('error', true);
131144
$result->setData('error_messages', $exception->getMessage());
132145
} catch (\Exception $exception) {
146+
$this->logger->critical($exception);
133147
$result->setData('error', true);
134148
$result->setData(
135149
'error_messages',

app/code/Magento/Authorizenet/Test/Unit/Controller/Directpost/Payment/PlaceTest.php

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Magento\Payment\Model\IframeConfigProvider;
2121
use Magento\Quote\Api\CartManagementInterface;
2222
use Magento\Quote\Model\Quote;
23+
use Magento\Framework\Exception\LocalizedException;
2324

2425
/**
2526
* Class PlaceTest
@@ -94,6 +95,11 @@ class PlaceTest extends \PHPUnit_Framework_TestCase
9495
*/
9596
protected $quoteMock;
9697

98+
/**
99+
* @var \PHPUnit_Framework_MockObject_MockObject
100+
*/
101+
private $loggerMock;
102+
97103
/**
98104
* @var CheckoutSession|\PHPUnit_Framework_MockObject_MockObject
99105
*/
@@ -152,6 +158,9 @@ protected function setUp()
152158
->getMockBuilder(\Magento\Framework\App\Response\Http::class)
153159
->disableOriginalConstructor()
154160
->getMockForAbstractClass();
161+
$this->loggerMock = $this
162+
->getMockBuilder(\Psr\Log\LoggerInterface::class)
163+
->getMock();
155164

156165
$this->objectManager = new ObjectManager($this);
157166
$this->placeOrderController = $this->objectManager->getObject(
@@ -165,6 +174,7 @@ protected function setUp()
165174
'cartManagement' => $this->cartManagementMock,
166175
'onepageCheckout' => $this->onepageCheckout,
167176
'jsonHelper' => $this->jsonHelperMock,
177+
'logger' => $this->loggerMock,
168178
]
169179
);
170180
}
@@ -214,13 +224,15 @@ public function testExecute(
214224
* @param $controller
215225
* @param $quoteId
216226
* @param $result
227+
* @param \Exception $exception Exception to check
217228
* @dataProvider textExecuteFailedPlaceOrderDataProvider
218229
*/
219230
public function testExecuteFailedPlaceOrder(
220231
$paymentMethod,
221232
$controller,
222233
$quoteId,
223-
$result
234+
$result,
235+
$exception
224236
) {
225237
$this->requestMock->expects($this->at(0))
226238
->method('getParam')
@@ -238,7 +250,11 @@ public function testExecuteFailedPlaceOrder(
238250

239251
$this->cartManagementMock->expects($this->once())
240252
->method('placeOrder')
241-
->willThrowException(new \Exception());
253+
->willThrowException($exception);
254+
255+
$this->loggerMock->expects($this->once())
256+
->method('critical')
257+
->with($exception);
242258

243259
$this->jsonHelperMock->expects($this->any())
244260
->method('jsonEncode')
@@ -278,19 +294,35 @@ public function textExecuteDataProvider()
278294
*/
279295
public function textExecuteFailedPlaceOrderDataProvider()
280296
{
281-
$objectFailed = new \Magento\Framework\DataObject();
282-
$objectFailed->setData('error', true);
283-
$objectFailed->setData(
284-
'error_messages',
285-
__('An error occurred on the server. Please try to place the order again.')
297+
$objectFailed1 = new \Magento\Framework\DataObject(
298+
[
299+
'error' => true,
300+
'error_messages' => __('An error occurred on the server. Please try to place the order again.')
301+
]
302+
);
303+
$generalException = new \Exception('Exception logging will save the world!');
304+
$localizedException = new LocalizedException(__('Electronic payments save the trees.'));
305+
$objectFailed2 = new \Magento\Framework\DataObject(
306+
[
307+
'error' => true,
308+
'error_messages' => $localizedException->getMessage()
309+
]
286310
);
287311

288312
return [
289313
[
290314
['method' => 'authorizenet_directpost'],
291315
IframeConfigProvider::CHECKOUT_IDENTIFIER,
292316
1,
293-
$objectFailed
317+
$objectFailed1,
318+
$generalException,
319+
],
320+
[
321+
['method' => 'authorizenet_directpost'],
322+
IframeConfigProvider::CHECKOUT_IDENTIFIER,
323+
1,
324+
$objectFailed2,
325+
$localizedException,
294326
],
295327
];
296328
}

app/code/Magento/Braintree/Controller/Paypal/PlaceOrder.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Magento\Framework\Controller\ResultFactory;
1212
use Magento\Framework\Exception\LocalizedException;
1313
use Magento\Braintree\Gateway\Config\PayPal\Config;
14+
use Psr\Log\LoggerInterface;
15+
use Magento\Framework\App\ObjectManager;
1416

1517
/**
1618
* Class PlaceOrder
@@ -22,22 +24,32 @@ class PlaceOrder extends AbstractAction
2224
*/
2325
private $orderPlace;
2426

27+
/**
28+
* Logger for exception details
29+
*
30+
* @var LoggerInterface
31+
*/
32+
private $logger;
33+
2534
/**
2635
* Constructor
2736
*
2837
* @param Context $context
2938
* @param Config $config
3039
* @param Session $checkoutSession
3140
* @param Helper\OrderPlace $orderPlace
41+
* @param LoggerInterface|null $logger
3242
*/
3343
public function __construct(
3444
Context $context,
3545
Config $config,
3646
Session $checkoutSession,
37-
Helper\OrderPlace $orderPlace
47+
Helper\OrderPlace $orderPlace,
48+
LoggerInterface $logger = null
3849
) {
3950
parent::__construct($context, $config, $checkoutSession);
4051
$this->orderPlace = $orderPlace;
52+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
4153
}
4254

4355
/**
@@ -58,6 +70,7 @@ public function execute()
5870
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
5971
return $resultRedirect->setPath('checkout/onepage/success', ['_secure' => true]);
6072
} catch (\Exception $e) {
73+
$this->logger->critical($e);
6174
$this->messageManager->addExceptionMessage($e, $e->getMessage());
6275
}
6376

app/code/Magento/Braintree/Test/Unit/Controller/Paypal/PlaceOrderTest.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ class PlaceOrderTest extends \PHPUnit_Framework_TestCase
6060
*/
6161
private $placeOrder;
6262

63+
/**
64+
* @var \PHPUnit_Framework_MockObject_MockObject
65+
*/
66+
private $loggerMock;
67+
6368
protected function setUp()
6469
{
6570
/** @var Context|\PHPUnit_Framework_MockObject_MockObject $contextMock */
@@ -94,11 +99,15 @@ protected function setUp()
9499
->method('getMessageManager')
95100
->willReturn($this->messageManagerMock);
96101

102+
$this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
103+
->disableOriginalConstructor()
104+
->getMock();
97105
$this->placeOrder = new PlaceOrder(
98106
$contextMock,
99107
$this->configMock,
100108
$this->checkoutSessionMock,
101-
$this->orderPlaceMock
109+
$this->orderPlaceMock,
110+
$this->loggerMock
102111
);
103112
}
104113

app/code/Magento/Payment/Model/Method/Adapter.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Magento\Payment\Gateway\Data\PaymentDataObjectFactory;
1818
use Magento\Payment\Gateway\Config\ValueHandlerPoolInterface;
1919
use Magento\Payment\Gateway\Validator\ValidatorPoolInterface;
20+
use Psr\Log\LoggerInterface;
21+
use Magento\Framework\App\ObjectManager;
2022

2123
/**
2224
* Payment method facade. Abstract method adapter
@@ -81,16 +83,25 @@ class Adapter implements MethodInterface
8183
*/
8284
private $commandExecutor;
8385

86+
/**
87+
* Logger for exception details
88+
*
89+
* @var LoggerInterface
90+
*/
91+
private $logger;
92+
8493
/**
8594
* @param ManagerInterface $eventManager
8695
* @param ValueHandlerPoolInterface $valueHandlerPool
8796
* @param PaymentDataObjectFactory $paymentDataObjectFactory
8897
* @param string $code
8998
* @param string $formBlockType
9099
* @param string $infoBlockType
91-
* @param CommandPoolInterface $commandPool
92-
* @param ValidatorPoolInterface $validatorPool
93-
* @param CommandManagerInterface $commandExecutor
100+
* @param CommandPoolInterface|null $commandPool
101+
* @param ValidatorPoolInterface|null $validatorPool
102+
* @param CommandManagerInterface|null $commandExecutor
103+
* @param LoggerInterface|null $logger
104+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
94105
*/
95106
public function __construct(
96107
ManagerInterface $eventManager,
@@ -101,7 +112,8 @@ public function __construct(
101112
$infoBlockType,
102113
CommandPoolInterface $commandPool = null,
103114
ValidatorPoolInterface $validatorPool = null,
104-
CommandManagerInterface $commandExecutor = null
115+
CommandManagerInterface $commandExecutor = null,
116+
LoggerInterface $logger = null
105117
) {
106118
$this->valueHandlerPool = $valueHandlerPool;
107119
$this->validatorPool = $validatorPool;
@@ -112,6 +124,7 @@ public function __construct(
112124
$this->eventManager = $eventManager;
113125
$this->paymentDataObjectFactory = $paymentDataObjectFactory;
114126
$this->commandExecutor = $commandExecutor;
127+
$this->logger = $logger ?: ObjectManager::getInstance()->get(LoggerInterface::class);
115128
}
116129

117130
/**
@@ -382,6 +395,7 @@ public function validate()
382395
try {
383396
$validator = $this->getValidatorPool()->get('global');
384397
} catch (\Exception $e) {
398+
$this->logger->critical($e);
385399
return $this;
386400
}
387401

app/code/Magento/Payment/Test/Unit/Model/Method/AdapterTest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class AdapterTest extends \PHPUnit_Framework_TestCase
5050
*/
5151
private $paymentDataObjectFactory;
5252

53+
/**
54+
* @var MockObject
55+
*/
56+
private $logger;
57+
5358
/**
5459
* @var string
5560
*/
@@ -84,6 +89,8 @@ protected function setUp()
8489
$this->formBlockType = '\FormBlock';
8590
$this->infoBlockType = '\InfoBlock';
8691

92+
$this->logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
93+
->getMock();
8794
$this->adapter = new Adapter(
8895
$this->eventManager,
8996
$this->valueHandlerPool,
@@ -92,7 +99,9 @@ protected function setUp()
9299
$this->formBlockType,
93100
$this->infoBlockType,
94101
$this->commandPool,
95-
$this->validatorPool
102+
$this->validatorPool,
103+
null,
104+
$this->logger
96105
);
97106
}
98107

@@ -221,7 +230,8 @@ public function testExecuteCommandWithCommandExecutor()
221230
'\InfoBlock',
222231
null,
223232
null,
224-
$commandManager
233+
$commandManager,
234+
$this->logger
225235
);
226236

227237
$valueHandler = $this->getMock(ValueHandlerInterface::class);
@@ -274,7 +284,10 @@ public function testExecuteCommandWithCommandPool()
274284
'CODE',
275285
'\FormBlock',
276286
'\InfoBlock',
277-
$commandPool
287+
$commandPool,
288+
null,
289+
null,
290+
$this->logger
278291
);
279292

280293
$valueHandler = $this->getMock(ValueHandlerInterface::class);
@@ -305,4 +318,18 @@ public function testExecuteCommandWithCommandPool()
305318

306319
$adapter->authorize($paymentInfo, 10);
307320
}
321+
322+
public function testValidationExceptionLogged()
323+
{
324+
$exception = new \Exception('We can test exception logging!');
325+
326+
$this->validatorPool->expects(static::once())
327+
->method('get')
328+
->with('global')
329+
->willThrowException($exception);
330+
$this->logger->expects(static::once())
331+
->method('critical')
332+
->with($exception);
333+
$this->adapter->validate();
334+
}
308335
}

0 commit comments

Comments
 (0)