Problem/Motivation

Entity API tries to validate that an entity field definition array is valid. However, it's error handling has a fatal error. This makes figuring out what the problem is quite difficult.

Proposed resolution

Fix the fatal error, and catch the exception so we can report what the problem was. Yay, DX! The code will still fatal on an invalid entity definition, which is probably a good thing, but now it shows you what you did wrong.

Remaining tasks

Commit the patch?

User interface changes

None.

API changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Crell’s picture

Status: Active » Needs review
FileSize
1.64 KB

And patch.

Status: Needs review » Needs work

The last submitted patch, 1: 2457427-entity-error.patch, failed testing.

Xano’s picture

Why don't we just let the exception bubble up?

Crell’s picture

Because currently if we do that the error message is not displayed at all. You just get a generic "something bad happened" error, which is even less useful for debugging that you messed up your field definitions.

And I've no idea why tests won't even run, that's bizarre...

Status: Needs work » Needs review

Crell queued 1: 2457427-entity-error.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 1: 2457427-entity-error.patch, failed testing.

Crell’s picture

Status: Needs work » Needs review
FileSize
798 bytes

Hm. OK then.

1) drupal_set_message() needed a \ prefix as it is in a namespace. However...

2) There are existing tests that are checking for a LogicException being thrown. Which does make sense after all, because...

3) LogicExceptions that bubble up DO get caught and a useful error displayed... IF you have error reporting turned up. Drupal now has it set to super-silent mode by default, which I didn't realize.

So, if we just fix the bug itself everything seems to work out in the end now. :-) (Not bothering with an interdiff as all I did was remove half of what I did before.)

Crell queued 7: 2457427-entity-error.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 7: 2457427-entity-error.patch, failed testing.

Crell’s picture

Status: Needs work » Needs review
FileSize
802 bytes

Rerolled.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Small things matter!

xjm’s picture

Status: Reviewed & tested by the community » Fixed
+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -476,7 +476,7 @@ protected function buildBaseFieldDefinitions($entity_type_id) {
       if (!isset($base_field_definitions[$field_name])) {
...
-          '@field' => $base_field_definitions[$field_name]->getLabel(),

Nice find. Kind of an entertaining bug actually -- if the thing doesn't exist, then get its label! :)

I don't really think there's a test to be added here for the reasons described in #7. This issue is a normal bug fix, and doesn't include any disruptive changes, so it is allowed per https://www.drupal.org/core/beta-changes. Committed and pushed to 8.0.x. Thanks!

  • xjm committed b03932b on 8.0.x
    Issue #2457427 by Crell: Bad error handling of invalid Entity definition
    
JulienF’s picture

Status: Fixed » Needs work
Issue tags: +Needs tests, +Novice

Hey there, participating at Drupal Con Sprint in LA, was checking out the issue #2481547: attempt to throw an exception for missing entity field crashes and noted that its been fixed here. Yet @alexpott mentioned that a test needs to be written here:

Nice find.

I think we should be able to test this in EntityManagerTest - see testGetBaseFieldDefinitionsTranslatableEntityTypeLangcode and testGetBaseFieldDefinitionsInvalidDefinition for clues as to how.

JulienF’s picture

Status: Needs work » Needs review
FileSize
1.19 KB

Here you go with a patch to the EntityManagerTest class for this case

lauriii’s picture

Status: Needs review » Needs work

Thanks for working on the issue! Keep up the good work :)

  1. +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
    @@ -539,6 +539,24 @@ public function testGetFieldDefinitions() {
    +  ¶
    ...
    +   * ¶
    ...
    +   * ¶
    ...
    +    ¶
    ...
    +    ¶
    

    These extra spaces needs to be removed.

  2. +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php
    @@ -539,6 +539,24 @@ public function testGetFieldDefinitions() {
    +   * Tests The buildBaseFieldDefinitions() method with an invalid key
    ...
    +    // Load the class object to remove its baseFieldDefinitions
    

    Comments needs to be ended with .

JulienF’s picture

Status: Needs work » Needs review
FileSize
1.19 KB

Is it good to go now ?

lauriii’s picture

There was still some extra spaces. I also did some cleaning for the documentation. You can use Dreditor browser plugin to review patches and see this kind of things. This test doesn't still test anything because there is not any assertions?

JulienF’s picture

well the "assertion" comes from the expected exception that should be thrown. It makes sure the exception gets thrown properly and don't crash

Thanks for the cleaning :-)

smccabe’s picture

To make this a proper test shouldn't there be a try/catch to catch the expected exception and then put an assertion on the catch. As a test this will currently just crash and then do nothing.

Crell’s picture

smccabe: The @ExpectedException annotation on the test means that PHPUnit will take care of that for us. It will catch any exception, and the test will fail if it doesn't catch the one we said it should catch.

smccabe’s picture

Oh cool! does it mean this code below is wrong/unnecessary? I was working with in on another issue so I assumed it was the way to do things. ModuleImplementsAlterTest.php

  function testModuleImplementsAlterNonExistingImplementation() {

    // Install the module_test module.
    \Drupal::service('module_installer')->install(array('module_test'));

    try {
      // Trigger hook discovery.
      \Drupal::moduleHandler()->getImplementations('unimplemented_test_hook');
      $this->fail('An exception should be thrown for the non-existing implementation.');
    }
    catch (\RuntimeException $e) {
      $this->pass('An exception should be thrown for the non-existing implementation.');
      $this->assertEqual($e->getMessage(), 'An invalid implementation module_test_unimplemented_test_hook was added by hook_module_implements_alter()');
    }
  }
Crell’s picture

Correct, that's unnecessary. And also a bit off topic for this thread, so if you've further questions please ask them in #drupal-contribute. Thanks. :-)

  • xjm committed b03932b on 8.1.x
    Issue #2457427 by Crell: Bad error handling of invalid Entity definition
    

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

stpaultim’s picture

Status: Needs review » Fixed

Seeing the commit and lack of subsequent activity, I'm thinking that this issue should have been marked fixed. Please, correct me if I'm wrong.

lauriii’s picture

I created a follow up to add the test coverage: #2724867: Create tests for invalid Entity definition error handling

Status: Fixed » Closed (fixed)

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