Problem/Motivation

When catching plugin exceptions it might come in handy to figure out for which plugin ID the exception was thrown.
I didn't check whether all usages of PluginException are in fact in the scope of a plugin ID, but I'm feeling like they might.

Proposed resolution

1. A getPluginId() method already existed in InvalidPluginDefinitionException so I moved it to PluginException alongside the protected var $pluginId.
2. Add a pluginId parameter to PluginException constructor and re-factor every instantiation of this class and its subclasses.

Remaining tasks

1. Is provided by patch in #5
2. Add a pluginId parameter to PluginException constructor and re-factor every instantiation of this class and its subclasses.

API changes

InvalidPluginException class will now requires a plugin id parameter.

Original report by @tstoeckler

 

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Reroll the patch if it no longer applies. Instructions
Add steps to reproduce the issue Novice Instructions
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions
CommentFileSizeAuthor
#73 interdiff_69-73.txt3.83 KBsahil.goyal
#73 interdiff_68-73.txt6.22 KBsahil.goyal
#73 2121349-73.patch20.08 KBsahil.goyal
#71 Screenshot 2023-03-24 at 13.27.24.png52.77 KBricardofaria
#69 2121349-69.patch17.77 KBamanshukla6158
#68 interdiff_67-68.txt2.1 KBAkram Khan
#68 2121349-68.patch19.78 KBAkram Khan
#67 2121349-67.patch20.29 KBNikhil_110
#58 2121349-58.patch18.34 KBquietone
#58 interdiff-57-58.txt1.18 KBquietone
#57 2121349-57.patch17.69 KBquietone
#57 interdff-54-57.txt3.43 KBquietone
#54 2121349-54.patch18.33 KBoknate
#54 interdiff-51-54.txt1006 bytesoknate
#51 2121349-51.patch17.88 KBquietone
#41 drupal_2121349_41.patch18.16 KBXano
#41 interdiff.txt1.76 KBXano
#40 drupal_2121349_40.patch18.15 KBXano
#39 pluginexception_should-2121349-39.patch18.15 KBcilefen
#37 interdiff.txt764 bytesclaudiu.cristea
#37 2121349-37.patch18.06 KBclaudiu.cristea
#35 interdiff.txt5.14 KBclaudiu.cristea
#35 2121349-35.patch18.12 KBclaudiu.cristea
#33 interdiff-30-33.txt2.06 KBnlisgo
#33 pluginexception_should-2121349-33.patch16.28 KBnlisgo
#30 core-pluginexception_getPluginId-2121349-30.patch16.26 KBrudins
#26 core-pluginexception_getPluginId-2121349-26.patch16.28 KBrudins
#18 core-pluginexception_should-2121349-18.patch16.48 KBgarphy
#13 core-pluginexception_should-2121349-13.patch16.54 KBjsobiecki
#8 interdiff.txt16.78 KBgarphy
#8 core-pluginexception_should-2121349-8.patch17.36 KBgarphy
#5 core-pluginexception_should-2121349-5.patch2.57 KBgarphy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Issue summary: View changes
Issue tags: +Novice

This is kinda of novice task.

garphy’s picture

Assigned: Unassigned » garphy
garphy’s picture

Base PluginException class doesn't define a specific constructor parameter for the pluginId. New instance creation are passed the pluginId via the $code parameter (from Exception base class).

On the other end, PluginNotFoundException and InvalidPluginDefinitionException define a $pluginId constructor parameter which is redundant with $code.

There is less instance creation of those two so it would be more straightforward to drop the specific $pluginId argument and use $code everywhere in an aim to unify those classes (getPluginId() would return $code).

On the other hand, is it the real purpose of the $code argument. Shouldn't we add a $pluginId constructor parameter to the PluginException class and fix all instance creation (and what value give to $code in that case?)

dawehner’s picture

On the other hand, is it the real purpose of the $code argument. Shouldn't we add a $pluginId constructor parameter to the PluginException class and fix all instance creation (and what value give to $code in that case?)

Yeah indeed we have to expand the exception class or provide one for cases where the plugin ID is known. It is always a better DX if you provide more context.

garphy’s picture

Status: Active » Needs review
FileSize
2.57 KB

This is a first attempt.

A getPluginId() method already existed in InvalidPluginDefinitionException so I moved it to PluginException alongside the protected var $pluginId.

Next step (if tests don't already broke at this step) : Add a pluginId parameter to PluginException constructor and re-factor every instantiation of this class and its subclasses.

(put to need reviews to trigger the bot but I don't think it's finished yet)

garphy’s picture

Status: Needs review » Needs work

Testbot is happy. Deeper refactoring can take place.

johnennew’s picture

Issue summary: View changes
Issue tags: +Amsterdam2014
garphy’s picture

Status: Needs work » Needs review
FileSize
17.36 KB
16.78 KB

I added a plugin_id argument in PluginException and re-factored every instantiation I could find.

jsobiecki’s picture

I'm work on review for a patch.

Status: Needs review » Needs work

The last submitted patch, 8: core-pluginexception_should-2121349-8.patch, failed testing.

jsobiecki’s picture

The patch looks OK for me.

I verified if all execution of PluginException hierarchy constructors:
- InvalidDeriverException
- InvalidPluginDefinitionException
- PluginNotFoundException

For all cases, constructor is updated.

My local phpunit execution complains about incomplete tests, but there are out of scope of this issue. It's related to issue #2299795, so it's different story. I'm still waiting for output from testbot.

jsobiecki’s picture

I got failing tests, I'm investigating that.

jsobiecki’s picture

I found source of problem. One of PluginNotFound exceptions has one argument too much and php complained about type of arguments. I fixed that, and attached patch should hopefully fix the issue.

jsobiecki’s picture

Status: Needs work » Needs review
mradcliffe’s picture

I will make this "needs work" when it comes back from test bot.

+++ b/core/lib/Drupal/Core/Menu/MenuTreeStorage.php
@@ -1153,7 +1153,8 @@ protected function ensureTableExists() {
+      // TODO why are we firing a PluginException here ?

This should be @todo per documentation standards: https://www.drupal.org/coding-standards/docs#todo

Usually we make a follow-up issue about this.

Can we find a follow-up issue for this that already exists or create a new one.

mradcliffe’s picture

Status: Needs review » Needs work

I'll mark this needs work right now anyway.

garphy’s picture

garphy’s picture

garphy’s picture

Status: Needs work » Needs review
garphy’s picture

jsobiecki’s picture

Latest changes (patch #18) removes single line of comment (TODO line). I don't see other changes, so it's still look ok for me.

jsobiecki’s picture

Issue tags: -Amsterdam2014 +dcwroc2014

Status: Needs review » Needs work

The last submitted patch, 18: core-pluginexception_should-2121349-18.patch, failed testing.

claudiu.cristea’s picture

Issue tags: +Needs reroll
  1. +++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidPluginDefinitionException.php
    @@ -13,13 +13,6 @@
       /**
    ...
        * Constructs a InvalidPluginDefinitionException.
        *
        * @param string $plugin_id
    
    @@ -28,18 +21,7 @@ class InvalidPluginDefinitionException extends PluginException {
        * @see \Exception for the remaining parameters.
        */
       public function __construct($plugin_id, $message = '', $code = 0, \Exception $previous = NULL) {
    ...
    +    parent::__construct($plugin_id, $message, $code, $previous);
       }
    

    Replace the docblock with:

      /**
       * {@inheritdoc}
       */
    

    What happens when the message is empty? Shouldn't we provide a default generic message?

        if (empty($message)) {
          $message = sprinf("Invalid plugin '%s' exception.', $plugin_id);
        }
        parent::__construct($message, $code, $previous);
    
  2. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +use Exception;
    

    Empty line between 'namespace' and 'use'.

  3. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +class PluginException extends \Exception implements ExceptionInterface {
    

    Just Exception, no name spacing while it has been already declared in 'use'.

  4. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +   * Creates a new instance of PluginException
    

    Empty line between description and @param bloc. Missed dot at the end.

  5. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +   *   the id of the plugin which relates to this exception
    

    Should start with upercase ("The...").

  6. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +   *   [optional] The Exception message to throw.
    ...
    +   *   [optional] The previous exception used for the exception chaining. Since 5.3.0
    

    "Optional" should be enclosed in normal/round parentheses. Also there's a end dot missed. When a parameter is optional you should also provide the the default value at the end of description. Example: (optional) The Exception code. Defaults tp 0.

  7. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +   */
    

    At the end of the docbloc ass also a reference to \Exception:

       *
       * @see \Exception
       */
    
  8. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +  public function __construct($plugin_id, $message = "", $code = 0, Exception $previous = NULL) {
    +    $this->pluginId = $plugin_id;
    

    What happens when the message is empty? Shouldn't we provide a default generic message?

        if (empty($message)) {
          $message = sprinf("Plugin '%s' exception.', $plugin_id);
        }
        parent::__construct($message, $code, $previous);
    
  9. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -5,9 +5,44 @@
    +    parent::__construct($message, $code, $previous);
    +  }
    +  /**
    

    Empty line between the 2 methods.

  10. Replace PluginNotFoundException::__construct() docblock with:
      /**
       * {@inheritdoc}
       */
    

EDIT: Add also an interdiff against the last patch.

rudins’s picture

Status: Needs work » Needs review
FileSize
16.28 KB

Did some update based on claudiu suggestions.

claudiu.cristea’s picture

@rudins, interdiff?

Status: Needs review » Needs work

The last submitted patch, 26: core-pluginexception_getPluginId-2121349-26.patch, failed testing.

claudiu.cristea’s picture

+++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
@@ -6,8 +6,51 @@
+class PluginException extends Exception implements ExceptionInterface {

My bad. It seems that this breaks. Revert it back.a

rudins’s picture

Status: Needs work » Needs review
FileSize
16.26 KB

Let's try this one.

Status: Needs review » Needs work

The last submitted patch, 30: core-pluginexception_getPluginId-2121349-30.patch, failed testing.

mradcliffe’s picture

Looks like some typos:

  1. +++ b/core/lib/Drupal/Component/Plugin/Exception/InvalidPluginDefinitionException.php
    @@ -13,33 +13,13 @@
    +      $message = sprinf("Invalid plugin '%s' exception.", $plugin_id);
    
  2. +++ b/core/lib/Drupal/Component/Plugin/Exception/PluginException.php
    @@ -6,8 +6,51 @@
    +      $message = sprinf("Plugin '%s' exception.", $plugin_id);
    
nlisgo’s picture

Status: Needs work » Needs review
FileSize
16.28 KB
2.06 KB

Addressed a few errors.

Status: Needs review » Needs work

The last submitted patch, 33: pluginexception_should-2121349-33.patch, failed testing.

claudiu.cristea’s picture

Assigned: garphy » Unassigned
Status: Needs work » Needs review
FileSize
18.12 KB
5.14 KB

Let's add an interface for this kind of exceptions.

Status: Needs review » Needs work

The last submitted patch, 35: 2121349-35.patch, failed testing.

claudiu.cristea’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
18.06 KB
764 bytes

Reverted PluginNotFoundException message.

Mile23’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
$ git apply 2121349-37.patch 
error: patch failed: core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php:124
error: core/lib/Drupal/Core/ImageToolkit/ImageToolkitOperationBase.php: patch does not apply
error: patch failed: core/lib/Drupal/Core/Menu/MenuLinkBase.php:177
error: core/lib/Drupal/Core/Menu/MenuLinkBase.php: patch does not apply
error: patch failed: core/lib/Drupal/Core/Menu/MenuLinkManager.php:281
error: core/lib/Drupal/Core/Menu/MenuLinkManager.php: patch does not apply
error: patch failed: core/lib/Drupal/Core/Menu/MenuTreeStorage.php:397
error: core/lib/Drupal/Core/Menu/MenuTreeStorage.php: patch does not apply
error: patch failed: core/modules/block/src/BlockPluginCollection.php:56
error: core/modules/block/src/BlockPluginCollection.php: patch does not apply
error: patch failed: core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php:138
error: core/modules/menu_link_content/src/Plugin/Menu/MenuLinkContent.php: patch does not apply
cilefen’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll +Needs beta evaluation
FileSize
18.15 KB

reroll

Xano’s picture

FileSize
18.15 KB

Re-roll.

Xano’s picture

FileSize
1.76 KB
18.16 KB

Some docs fixes.

PluginExceptionInterface extends ExceptionInterface isn't very self-descriptive, especially since they are both in exactly the same namespace. We should try to come up with a slightly better name.

jsobiecki’s picture

Issue tags: -dcwroc2014 +dcw05
jsobiecki’s picture

Hi, I found several places in code, where as id parameter empty string ('') is provided. I'm wondering, if it's valid behaviour and if it should be tolerated, ie:

      throw new PluginException('', $e->getMessage(), NULL, $e);

I'm not sure, but maybe we should provide some sort of failsafe value?

Xano’s picture

Status: Needs review » Needs work

It's not a problem, so no failsafe is needed, but it is definitely confusing, as in that particular context there simply is no plugin ID.

I think we should keep the old PluginException as it is, and introduce a child exception that should be used for problems with specific plugins, rather than any problem in the plugin system.

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.

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

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

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

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

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

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

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

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

quietone’s picture

Status: Needs work » Needs review
FileSize
17.88 KB

Just a reroll with an additional change to a new use of the plugin exception in /core/modules/layout_builder/src/SectionComponent.php.

oknate’s picture

It looks like there's an additional one in DefaultFactory.php that can be updated.

oknate’s picture

Version: 8.6.x-dev » 8.7.x-dev
oknate’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

quietone’s picture

That patch didn't include the new file, PluginExceptionInterface.php. This corrects that and uses sprintf instead of String::format in MenuLinkManager.php.

tim.plunkett’s picture

This was opened almost 7 years ago, long before the approach to BC was adopted.
In order to make this change now, we'd have to introduce a new exception class and deprecate the existing one, not change the constructor of the existing one.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Status: Needs review » Needs work

This needs work for #59. What should be name of the new exception class?

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Nikhil_110’s picture

Re-roll patch #58 for Drupal 10.x

Akram Khan’s picture

added updated patch and try to fixed CCF #67

amanshukla6158’s picture

Status: Needs work » Needs review
FileSize
17.77 KB

fixed phpcs issues.

Status: Needs review » Needs work

The last submitted patch, 69: 2121349-69.patch, failed testing. View results

ricardofaria’s picture

I tested the patch on #69 and it works for 10.1.x-dev.
SS attached.

joachim’s picture

Please don't post screenshots of applying a patch. It's a waste of diskspace and bandwidth. You can just say it applies. Also, the testbot will say if it doesn't apply.

Also, the patch is failing tests, so at this point it needs work anyway.

sahil.goyal’s picture

latest patchs are failing test, so updating the patch for resolving errors and try to fix these errors, also attaching Interdiff along with.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.