Commit message:

Improve error handling of \Drupal class #2160655 by msonnabaum, chx

Too often we are hit by a fatal in Drupal.php if the container rebuild hits Drupal.php itself. We should change that to a friendly exception which links to the rebuild script.

Comments

Status: Needs review » Needs work

The last submitted patch, no_container.patch, failed testing.

chx’s picture

Status: Needs work » Needs review
StatusFileSize
new9.98 KB
chx’s picture

StatusFileSize
new10.02 KB
new548 bytes

I think flipping the logic around makes this easier to understand.

chx’s picture

Title: Change the no-container fatal to an exception » Improve error handling of \Drupal class
Issue summary: View changes
StatusFileSize
new639 bytes
new10.23 KB

Copied the better error message from #2084521: Improve error handling of \Drupal class

chx’s picture

StatusFileSize
new10.79 KB
new639 bytes

The last submitted patch, 5: 2160655_5.patch, failed testing.

chx’s picture

StatusFileSize
new10.79 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Someone should find the other issues which do the same.

I really like the patch.

xjm’s picture

7: 2160655_7.patch queued for re-testing.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 7: 2160655_7.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
StatusFileSize
new10.73 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 11: drupal-2160655-11.patch, failed testing.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new11.23 KB
new506 bytes

Fixed the test.

Status: Needs review » Needs work

The last submitted patch, 13: drupal-2160655-13.patch, failed testing.

jibran’s picture

13: drupal-2160655-13.patch queued for re-testing.

The last submitted patch, 13: drupal-2160655-13.patch, failed testing.

dawehner’s picture

13: drupal-2160655-13.patch queued for re-testing.

The last submitted patch, 13: drupal-2160655-13.patch, failed testing.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new1.63 KB
new12.86 KB

Fixed the tests.

Status: Needs review » Needs work

The last submitted patch, 19: drupal-2160655-19.patch, failed testing.

The last submitted patch, 19: drupal-2160655-19.patch, failed testing.

jibran’s picture

rmdir(sites/simpletest/353397): Directory not empty: drupal_rmdir('sites/simpletest/353397') file_unmanaged_delete_recursive('sites/simpletest/353397', Array) Drupal\simpletest\TestBase->restoreEnvironment() Drupal\simpletest\TestBase->run() simpletest_script_run_one_test('269', 'Drupal\field\Tests\reEnableModuleFieldTest') now what :S

jibran’s picture

Status: Needs work » Needs review

19: drupal-2160655-19.patch queued for re-testing.

ianthomas_uk’s picture

That's certainly a much more useful error message. The pattern seems a bit weird, but only because php should provide a better way (it doesn't AFAIK).

+++ b/core/lib/Drupal/Core/Plugin/PluginBase.php
@@ -103,11 +103,13 @@ public function __sleep() {
-    $container = \Drupal::getContainer();
-    foreach ($this->_serviceIds as $key => $service_id) {
-      $this->$key = $container->get($service_id);
+    if (\Drupal::hasContainer()) {
+      $container = \Drupal::getContainer();
+      foreach ($this->_serviceIds as $key => $service_id) {
+        $this->$key = $container->get($service_id);
+      }
+      unset($this->_serviceIds);
     }
-    unset($this->_serviceIds);

I don't understand these changes. Surely we need to put the services into the object, and if we can't do that because we haven't got a container yet, then we need to find a way of having a container? Won't just ignoring it cause us problems later?

+++ b/core/lib/Drupal.php
@@ -115,9 +115,28 @@ public static function setContainer(ContainerInterface $container) {
+      $backtrace[1]['class'] . $backtrace[1]['type'] . $backtrace[1]['function']

These might not be set depending on where getContainer is called from. But we're already killing the page, so do we care about a couple of notices?

It's probably beyond the scope of this issue, but I don't think getContainer() should be deprecated. protected-unless-youre-a-test would be more accurate.

ianthomas_uk’s picture

I have raised #2195725: Un-deprecate \Drupal()::getContainer() to look at whether getContainer() should be deprecated.

dawehner’s picture

It's probably beyond the scope of this issue, but I don't think getContainer() should be deprecated. protected-unless-youre-a-test would be more accurate.

Afaik the problem is that we have no proper semantics to tell people that they really should not do it that way unless there is no alternative solution.

jibran’s picture

jibran’s picture

Status: Needs review » Closed (duplicate)