We try to catch Drupal\simpletest\Exception instead of Exception. Obviously this doesn't quite succeed. I have no idea how to test this without the excepted error facility we filed for 3-4 years ago but wasnt well received...

CommentFileSizeAuthor
#2 1759026_2.patch2.54 KBchx
exception_broken.patch505 byteschx
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Berdir’s picture

Confirmed.

We also have a few others of these, here's my attempt at some grep magic, it looks for files that catch Exception, have a namespace declaration in it and do not contain "use Exception" (There are probably easier ways to do this, but it seems to work))

$ grep -Rl "catch (Exception" core | grep "lib" | grep -v "vendor" | xargs grep -l "namespace Drupal" | xargs  grep  -H -E -o -c  "use Exception" | grep :0\$
core/modules/simpletest/lib/Drupal/simpletest/TestBase.php:0
core/modules/entity/lib/Drupal/entity/DatabaseStorageController.php:0
core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php:0
core/modules/system/lib/Drupal/system/Tests/Cache/InstallTest.php:0
core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php:0

There is an issue for DatabaseStorageController already (#1634442: DatabaseStorageController can't catch exceptions, might need a re-roll now), not sure if we want to fix the others here as well or or in a follow-up issue. UpgradePathTestBase.php might be the same one as this, haven't checked.

chx’s picture

FileSize
2.54 KB

Thanks Berdir.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. I suggest to commit this and maybe open a follow-up to consider if we can add tests for those. Fixing them is trivial and rather urgent, adding proper tests might take a while and has held up the issue linked (which does add tests for the database storage case) above for weeks.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Well, first off, nice catch!

But it's really quite terrible that if test authors don't remember to add this weird use statement that their tests will fail to catch exceptions. :\ Is there anything that can be done about this? Why is TestBase.php doing it not enough?

In the meantime, committed and pushed this patch to 8.x to stop the bleeding... not sure I'd call this issue "fixed" though.

chx’s picture

Yes -- we shouldnt use the use statement for root classes, so just do catch (\Exception and be done. I guess too late to debate?

Berdir’s picture

Test writers don't need to deal with it unless they specifically are dealing with exceptions which is in most of the cases related to testing expected exceptions (so it would break if it wouldn't correctly catch the exception, see e.g. the tests in the database storage controller issue).

I'm not sure about \Exception, I'd be fine defining that it's ok to do that, especially for native PHP classes in the global namespace. It doesn't change that much, you still need to remember to do it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.