Comments

tim.plunkett created an issue. See original summary.

tim.plunkett credited dpi.

tim.plunkett’s picture

jibran’s picture

Status: Active » Reviewed & tested by the community
Issue tags: +Minor release beta testing

Thanks, for creating the issue setting it RTBC as per original issue.

The last submitted patch, 4: 3046243-contexthandler-2-FAIL.patch, failed testing. View results

alexpott’s picture

Status: Reviewed & tested by the community » Needs review

@larowlan asked in slack if

+++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
@@ -110,19 +110,30 @@ public function applyContextMapping(ContextAwarePluginInterface $plugin, $contex
+      catch (\Exception $e) {

we can catch a more specific context. @tim.plunkett can we catch \Drupal\Component\Plugin\Exception\PluginException instead?

pandaski’s picture

Quick thought. why not update the getContext method then we can catch the '\Drupal\Component\Plugin\Exception\PluginException' from there.

  /**
   * {@inheritdoc}
   *
   * This code is identical to the Component in order to pick up a different
   * Context class.
   *
   * @return \Drupal\Core\Plugin\Context\ContextInterface
   *   The context object.
   */
  public function getContext($name) {
    // Check for a valid context value.
    if (!isset($this->context[$name])) {
      $this->context[$name] = new Context($this->getContextDefinition($name));
    }
    return $this->context[$name];
  }
pandaski’s picture

StatusFileSize
new2.39 KB
new3.91 KB

Based on thoughts in #8, check the $context is an instance of ContextInterface, otherwise, it is NULL

  /**
   * {@inheritdoc}
   *
   * This code is identical to the Component in order to pick up a different
   * Context class.
   *
   * @return \Drupal\Core\Plugin\Context\ContextInterface
   *   The context object.
   */
  public function getContext($name) {
    // Check for a valid context value.
    if (!isset($this->context[$name])) {
      $this->context[$name] = NULL;
      $context = new Context($this->getContextDefinition($name));
      if ($context instanceof ContextInterface) {
        $this->context[$name] = new Context($this->getContextDefinition($name));
      }
    }
    return $this->context[$name];
  }

Status: Needs review » Needs work

The last submitted patch, 9: core-context_hander-3046243-9.patch, failed testing. View results

pandaski’s picture

StatusFileSize
new2.39 KB
new3.92 KB
pandaski’s picture

StatusFileSize
new2.4 KB
new3.92 KB

'getContext' always return an instance of ContextInerface or NULL instead of any exception.

tim.plunkett’s picture

Status: Needs work » Needs review
StatusFileSize
new3.79 KB
new2.55 KB

@Joseph Zhao that's not true. And that's actually the mistake I made in the original issue.

#8 shows the code. If it is not set, it creates one and returns it. Therefore it always returns a context object... However \Drupal\Component\Plugin\ContextAwarePluginBase::getContextDefinition() can throw the exception.

It says it will throw \Drupal\Component\Plugin\Exception\PluginException
But it actually throws \Drupal\Component\Plugin\Exception\ContextException

It switched from PluginException to ContextException in #2281635: \Drupal\Core\Plugin\Context\Context should use a ContextDefinition class instead of arrays.

I opened #3046342: \Drupal\Component\Plugin\ContextAwarePluginInterface has incorrect @throws docs to fix the docs.
In the meantime, we should catch both (even though one is only going to happen).

Interdiff is from my last patch in #4.

alexpott’s picture

@tim.plunkett++ thanks the new patch looks much better than catching \Exception - if it is not rtbc tomorrow I'll do that otherwise I'll commit it if it is :)

jibran’s picture

Status: Needs review » Reviewed & tested by the community

I agree patch looks much better now. Thanks!

  • catch committed 2cceb4a on 8.8.x
    Issue #3046243 by tim.plunkett, jibran, dpi: Regression: Optional...

  • catch committed 500027a on 8.7.x
    Issue #3046243 by tim.plunkett, jibran, dpi: Regression: Optional...
catch’s picture

jibran’s picture

Correcting the tag.

Status: Fixed » Closed (fixed)

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