In the Magento\Checkout\Model\PaymentInformationManagement::savePaymentInformationAndPlaceOrder()
method all exceptions are caught and CouldNotSaveException
is created with a generic message.
I want to make this message user friendly as I am implementing a fairly complex restriction and the generic message would cause lots of confusion for the customer.
public function savePaymentInformationAndPlaceOrder(
$cartId,
\Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
\Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
$this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
try {
$orderId = $this->cartManagement->placeOrder($cartId);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__('An error occurred on the server. Please try to place the order again.'),
$e
);
}
return $orderId;
}
My idea to work around this issue was to create an around plugin so that I could catch the new Exception the throw the previous exception which I create via an observer level check.
public function aroundSavePaymentInformationAndPlaceOrder(
\Magento\Checkout\Model\PaymentInformationManagement $subject,
callable $proceed,
...$args
) {
try {
$result = $proceed(...$args);
} catch (\Magento\Framework\Exception\CouldNotSaveException $e) {
throw $e->getPrevious();
}
return $result;
}