Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] Root exception not logged on QuoteManagement::submitQuote #22037

Merged
merged 3 commits into from
Apr 5, 2019
Merged

[Backport] Root exception not logged on QuoteManagement::submitQuote #22037

merged 3 commits into from
Apr 5, 2019

Conversation

larsroettig
Copy link
Member

Original Pull Request

#21697

Description

If an exceptions happens in \Magento\Sales\Api\OrderManagementInterface::place or an sales_model_service_quote_submit_success observer, the catch block itself fires an event. As you can not know, what these observers do, it should be wrapped in a try catch block itself.

With the Magento core this will currently happen, for example if saving the order throws an exception. This causes a rollback in \Magento\Framework\Model\ResourceModel\Db\AbstractDb::save. The catch in the order management triggers via an observer the method \Magento\CatalogInventory\Model\ResourceModel\Stock::correctItemsQty. In this method the stock modification is secured in a transaction - as the connection is already in rollback mode, the exception with the message Rolled back transaction has not been completed correctly. is thrown.

The problem is, that only the second exception is logged in exception.log - the preceding exception is very hard to find.

To handle this I wrapped the catch block itself in a try catch and merge the information in a new exception. This new exception is handled as before and finds its way to the exception.log.

Fixed Issues

  1. Rolled back transaction has not been completed correctly" on Magento 2.1.15 #18752: Rolled back transaction has not been completed correctly" on Magento 2.1.15
  2. "Rolled back transaction has not been completed correctly" on Magento 2.2.3 #14926: "Rolled back transaction has not been completed correctly" on Magento 2.2.3

Manual testing scenarios

I do not know another (simple) way of reproducing this bug, than introducing an exception in placement of order. In a real life scenario this exception may happen because of some runtime conditions (invalid credit card data - depending on PSP). It has to be either a rollback triggered in the try block of \Magento\Sales\Api\OrderManagementInterface::place or an exception in a sales_model_service_quote_submit_failure observers.

  1. Add this to the method \Magento\Sales\Model\ResourceModel\Order::_beforeSave:
    throw new \Exception('root of all evil');
  2. watch the exception log: tail -f <magento_root>/var/log/excption.log
  3. add products to cart
  4. proceed to checkout
  5. try to place the order
    1. the message 'An error occurred. Try to place the order again.' shows up in store front
    2. the log entry contains the message of the consecutive exception ('Rolled back transaction has not been completed correctly')
    3. the log entry contains the message of the preceding exception ('root of all evil').

Example

Log message without fix

report.CRITICAL: Rolled back transaction has not been completed correctly. {"exception":"[object]
(Exception(code: 0): Rolled back transaction has not been completed correctly. at
/Users/fuehrd/sources/magento2ce/lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php:277)"} []

Log message with fix

report.CRITICAL: An exception occurred on 'sales_model_service_quote_submit_failure' event: Rolled
back transaction has not been completed correctly. {"exception":"[object] (Exception(code: 0): An
exception occurred on 'sales_model_service_quote_submit_failure' event: Rolled back transaction has
not been completed correctly. at
/Users/fuehrd/sources/magento2ce/app/code/Magento/Quote/Model/QuoteManagement.php:555,
Exception(code: 0): root of all evil at
/Users/fuehrd/sources/magento2ce/app/code/Magento/Sales/Model/ResourceModel/Order.php:144)"}

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

david-fuehr and others added 3 commits March 29, 2019 15:08
Github Issue: #18752

If an exceptions happens in orderManagement->place($order) or an
"sales_model_service_quote_submit_success" observer, the catch block
itself fires an event that currently fails for guest checkouts in
Magento\CatalogInventory\Model\ResourceModel\Stock->correctItemsQty().

This second exception hides the root exception and is logged to
the exception log with the message "Rolled back transaction has not
been completed correctly".

This is not bound for this observer, but may occur in every other
observer that is currently register or may be registered in the future.

Therefore the failure event is wrapped in a try-catch itself and throws
a combined exception that is logged in the exception.log.
- support masking of root cause in frontend
- remove stack trace for consecutive exception to comply to default behaviour
@m2-assistant
Copy link

m2-assistant bot commented Mar 29, 2019

Hi @larsroettig. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento-engcom-team give me test instance - deploy test instance based on PR changes
  • @magento-engcom-team give me 2.2-develop instance - deploy vanilla Magento instance

For more details, please, review the Magento Contributor Assistant documentation

@magento-engcom-team
Copy link
Contributor

Hi @sivaschenko, thank you for the review.
ENGCOM-4626 has been created to process this Pull Request

@soleksii
Copy link

soleksii commented Apr 4, 2019

✔️ QA Passed

Before:

before

After:

after

@m2-assistant
Copy link

m2-assistant bot commented Apr 5, 2019

Hi @larsroettig, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants