trackUpdateOnDeletion throws exception but is never caught

Created on 3 February 2025, 19 days ago

Problem/Motivation

I found this when saving a node with an invalid uri in a link field:

The website encountered an unexpected error. Try again later.

Drupal\Core\Entity\EntityStorageException: The URI 'www.example.com' is invalid. You must use a valid URI scheme. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

Drupal\link\Plugin\Field\FieldType\LinkItem->getUrl() (Line: 166)
Drupal\link\Plugin\Field\FieldType\LinkItem->isExternal() (Line: 25)
Drupal\entity_usage\Plugin\EntityUsage\Track\Link->getTargetEntities() (Line: 175)
Drupal\entity_usage\EntityUsageTrackBase->trackOnEntityCreation() (Line: 202)
Drupal\entity_usage\EntityUsageTrackBase->trackOnEntityUpdate() (Line: 101)
Drupal\entity_usage\EntityUpdateManager->trackUpdateOnEdition() (Line: 44)
entity_usage_entity_update()

Steps to reproduce

I'm dealing with legacy migrated data so normally the field validation would stop the data from getting into this state.
However for a bit more resilience a try catch could be added.

Checking the EntityUpdateManagerInterface, trackUpdateOnDeletion() is marked as throwing an InvalidArgumentException, however the 3 places that call this method don't try, catch (unlike trackUpdateOnCreation where exceptions are caught)

Proposed resolution

Add try catch round each place trackUpdateOnDeletion is called.

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom dahousecat

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @dahousecat
  • πŸ‡¬πŸ‡§United Kingdom dahousecat
  • πŸ‡¬πŸ‡§United Kingdom dahousecat

    Huh, actually the exception was thrown via trackUpdateOnEdition, not trackUpdateOnDeletion.
    Updated patch to try catch that method too.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Nice find... the problem here is that \Drupal\entity_usage\Plugin\EntityUsage\Track\Link::getTargetEntities should not throw the exception. We should put the. try catch in there. Also I don't think we should be logging here either. I think we should just return an empty array and move on. That means other fields with entities will still be tracked for this entity.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Or perhaps even better would be to put the try catch inside field loop in \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityUpdate() and \Drupal\entity_usage\EntityUsageTrackBase::trackOnEntityCreation()... it's tricky - that'd be more robust but potentially more likely to catch something you would want to log - so if we do that we should log as well.

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I pushed an MR that starts on #5 - just need to add logging service in a BC compat way...

  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Added logging...

  • Pipeline finished with Failed
    19 days ago
    Total: 516s
    #413308
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ
  • Pipeline finished with Failed
    19 days ago
    Total: 249s
    #413341
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    Could do with adding an automated test here.

  • Pipeline finished with Success
    19 days ago
    Total: 1623s
    #413347
  • Pipeline finished with Failed
    19 days ago
    Total: 222s
    #414139
  • Pipeline finished with Failed
    18 days ago
    Total: 468s
    #414385
  • Pipeline finished with Failed
    18 days ago
    Total: 294s
    #414406
  • Pipeline finished with Canceled
    18 days ago
    Total: 94s
    #414417
  • Pipeline finished with Canceled
    18 days ago
    Total: 94s
    #414418
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ

    I think this is ready now. We log any exceptions thrown by track and have test coverage that the exceptions are logged.

  • Pipeline finished with Success
    18 days ago
    Total: 457s
    #414419
  • πŸ‡¬πŸ‡§United Kingdom alexpott πŸ‡ͺπŸ‡ΊπŸŒ
  • Pipeline finished with Success
    18 days ago
    Total: 297s
    #414429
  • Pipeline finished with Success
    18 days ago
    Total: 230s
    #414446
  • Pipeline finished with Skipped
    18 days ago
    #414480
  • First commit to issue fork.
  • πŸ‡ͺπŸ‡ΈSpain marcoscano Barcelona, Spain

    Thanks for jumping in and for the generic solution in the base class πŸ‘

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

Production build 0.71.5 2024