Add to cart doesn't handle entity save exceptions.
The function commerce_cart_product_add() manipulate the line item entities, without checking for exceptions.
If for some reason an exception has been thrown, the site will die with fatal error.

Comments

skek created an issue. See original summary.

skek’s picture

br0ken’s picture

Status: Needs review » Needs work
mglaman’s picture

+++ b/modules/cart/commerce_cart.module
@@ -1368,31 +1368,37 @@ function commerce_cart_product_add($uid, $line_item, $combine = TRUE) {
+    drupal_set_message(t('Something goes wrong while trying to add a product into the basket!'), 'error');

I know many people refer to carts as baskets, now. However everywhere else we say cart.

br0ken’s picture

@mglaman, are you agreed with other things? By the way, why tests are failed?

br0ken’s picture

mglaman’s picture

I agree we should try and catch here, since there is a chance for an exception.

No idea why the tests are failing, though.

Status: Needs review » Needs work
br0ken’s picture

Status: Needs work » Needs review

@mglaman, the exceptions aren't caused by this patch. Could it be committed?

mglaman’s picture

Status: Needs review » Needs work

No, because if commit the patch then the branch tests will not pass.

It is currently passing: https://www.drupal.org/pift-ci-job/273471.

br0ken’s picture

But the last success was on May 4, 2016. Can we re-run tests on current state of branch?

mglaman’s picture

Good catch; re-queued to see. Other patches have been passing though, AFAIK. I'm tied up right now, but I'll investigate today.

br0ken’s picture

So, tests are failed with the same result and, I guess, this patch can be merged.

mglaman’s picture

BR0kEN first priority is to fix HEAD. And get the patch reviewed more. On a one off review I agree with it.

br0ken’s picture

Strange, I've run whole set of tests locally and they are worked as should.

BR0kEN@Macintosh:/var/www/drupal/drupal-test/sites/all/modules/commerce (7.x-1.x) $ php ../../../../scripts/run-tests.sh "Drupal Commerce"

Drupal test run
---------------

Tests to be run:
 - Commerce base (CommerceBaseTesterTestCase)
 - Shopping cart anonymous to authenticated (CommerceCartTestCaseAnonymousToAuthenticated)
 - Shopping cart attributes (CommerceCartTestCaseAttributes)
 - Shopping cart multiple (CommerceCartTestCaseMultiProducts)
 - Shopping cart (CommerceCartTestCaseSimpleProduct)
 - Checkout process (CommerceCheckoutTestProcess)
 - Customer user interface (CommerceCustomerUITest)
 - Order CRUD (CommerceOrderCRUDTestCase)
 - Order Rules (CommerceOrderRulesTest)
 - Order administration (CommerceOrderUIAdminTest)
 - Payment Offsite (CommercePaymentOffsiteTest)
 - Payment Rules (CommercePaymentRulesTest)
 - Payment user interface (CommercePaymentUITest)
 - Product CRUD (CommerceProductCRUDTestCase)
 - Product reference (CommerceProductReferenceAdminTest)
 - Product administration (CommerceProductUIAdminTest)
 - Tax administration (CommerceTaxUIAdminTest)

Test run started:
 Wednesday, June 29, 2016 - 19:13

Test summary
------------

Commerce base 216 passes, 0 fails, 2 exceptions, and 31 debug messages
Shopping cart anonymous to authenticated 34 passes, 0 fails, 2 exceptions, and 8 debug messages
Shopping cart attributes 49 passes, 0 fails, 2 exceptions, and 10 debug messages
Shopping cart multiple 94 passes, 0 fails, 2 exceptions, and 18 debug messages
Shopping cart 144 passes, 0 fails, 2 exceptions, and 32 debug messages
Checkout process 343 passes, 0 fails, 2 exceptions, and 88 debug messages
Customer user interface 421 passes, 0 fails, 2 exceptions, and 108 debug messages
Order CRUD 39 passes, 0 fails, and 5 exceptions
Order Rules 31 passes, 0 fails, and 2 exceptions
Order administration 500 passes, 0 fails, 2 exceptions, and 165 debug messages
Payment Offsite 36 passes, 0 fails, 2 exceptions, and 12 debug messages
Payment Rules 12 passes, 0 fails, and 2 exceptions
Payment user interface 183 passes, 0 fails, 2 exceptions, and 53 debug messages
Product CRUD 71 passes, 0 fails, and 5 exceptions
Product reference 293 passes, 0 fails, 2 exceptions, and 93 debug messages
Product administration 476 passes, 0 fails, 2 exceptions, and 107 debug messages
Tax administration 596 passes, 0 fails, 2 exceptions, and 143 debug messages

Test run duration: 53 min 14 sec
br0ken’s picture

I have improved the CommerceCustomerUITest test (see related issue) to get rid from PHP warning/notices and meet Drupal coding standards.

rszrama’s picture

Title: Add to cart doesn't handle entity save exceptions » Use a try / catch in the Add to Cart process
Category: Bug report » Task

Generally agreed on adding try / catch blocks around usages of the entity metadata wrapper. What's going on here, though? Are you just getting a bunk order object that doesn't have a list wrapper in place for the line items field? Or something else?

For the message, let's make it a little more plain: "The product could not be added to your cart. Please try again or contact us for support."

br0ken’s picture

Status: Needs work » Needs review
StatusFileSize
new2.49 KB
new529 bytes

@rszrama, things can happen during saving the order or invoking the rules, for instance.

Status: Needs review » Needs work
rszrama’s picture

Yes, but things like what? An order save or Rule evaluation shouldn't typically result in a fatal error. : P

In other words, what's the precise error message / what module or rule configuration is causing the fatal? It'd be worth fixing the actual root issue.

br0ken’s picture

Typically not, but I had an issue with other module (user_addressbook), when order couldn't be saved an, instead of error message, site gone down. Also, potential issues could be raised when an event will be added for custom rules.

br0ken’s picture

Status: Needs work » Needs review
mglaman’s picture

HEAD 1.x fixed :D retesting

br0ken’s picture

Haha, tests passed :)

br0ken’s picture

@rszrama, @mglaman what we will do with this issue?