Log error instead of trigger_error in Drupal\Core\EventSubscriber\RedirectResponseSubscriber::checkRedirectUrl

Created on 7 February 2024, 10 months ago
Updated 21 March 2024, 8 months ago

Problem/Motivation

Spin off from 📌 [meta] Replace calls to ::expectError*() and ::expectWarning*() Active .

PHPUnit 9 deprecated ::expectError*() methods. They're removed from PHPUnit 10.

Drupal\Core\EventSubscriber\RedirectResponseSubscriber::checkRedirectUrl is triggering an E_USER_ERROR in some circumstances, and a test exists expecting that.

That test needs to remove the expectation, and the runtime code need to find a different strategy to raise the error if we want to keep it testable.

Proposed resolution

Replace trigger_error() with logging the error to the logger service.

Merge request link

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Fixed

Version

10.3

Component
PHPUnit 

Last updated about 11 hours ago

Created by

🇮🇹Italy mondrake 🇮🇹

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

Merge Requests

Comments & Activities

  • Issue created by @mondrake
  • 🇮🇹Italy mondrake 🇮🇹

    In this case, the user is anyway informed about the issue via the 400 response with the message. The point is whether we want to track the same message for site admin. If we use trigger_error(), the error will only be logged if the setting allows, which is not recommended in prod sites. So it'd be better log it directly via a service, if the container is available?

  • Merge request !6491Closes #3419693 → (Closed) created by mondrake
  • Pipeline finished with Failed
    10 months ago
    Total: 752s
    #89865
  • Pipeline finished with Failed
    10 months ago
    Total: 182s
    #89870
  • Pipeline finished with Failed
    10 months ago
    Total: 880s
    #89874
  • Pipeline finished with Canceled
    10 months ago
    Total: 586s
    #89992
  • Status changed to Needs review 10 months ago
  • Pipeline finished with Failed
    10 months ago
    Total: 480s
    #89995
  • Pipeline finished with Success
    10 months ago
    Total: 476s
    #91612
  • Status changed to Needs work 10 months ago
  • 🇺🇸United States smustgrave

    Took a look but the CR link throws a 404.

  • 🇮🇹Italy mondrake 🇮🇹

    Because it's just a plaholder ATM.

  • 🇮🇹Italy mondrake 🇮🇹
  • Pipeline finished with Success
    9 months ago
    Total: 549s
    #96588
  • Status changed to Needs review 9 months ago
  • 🇮🇹Italy mondrake 🇮🇹

    Added CR and reference in code.

  • Status changed to RTBC 9 months ago
  • 🇺🇸United States smustgrave

    Thanks @mondrake!

  • Status changed to Needs review 9 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    This feels so wrong. Our test infrastructure should not be stopping us from using trigger_error. There has to be a better way.

  • 🇮🇹Italy mondrake 🇮🇹

    @alexpott re #10 we could backfill the expect[Error|Warning]*() methods, but current position is NOT to do that and abide by PHPUnit 10 approach that simply makes trigger_error() untestable.

    Discussion in https://drupal.slack.com/archives/C1BMUQ9U6/p1707946428463489

  • Status changed to RTBC 9 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    Discussed a bit with @catch.

    I'm not a big fan of PHPUnit making us change run-time code - it feels the wrong way around. But in this specific case I think the changes are an improvement - and maybe that will turn out to be the case with all trigger_error stuff.

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    One point that @catch makes is that logger channels are not lazy. So we have to instantiate services in order to inject something that is rarely used.

  • Status changed to Needs work 9 months ago
  • 🇬🇧United Kingdom catch

    Yes the reason to not inject loggers everywhere is because if they're logging errors it's for a 1/10000 event, and they have dependencies that also then need to be instantiated like the database layer.

    Discussed this with @alexpott in slack and he mentioned service closures. There's a good example of that being used in 📌 Remove ContainerAwareTrait from session middleware Fixed , so I think we can do that.

    I'm personally not convinced that completely dropping expectError is going to help us, because phpunit has also dropped support for what symfony-phpunit bridge does for @trigger_error() too - so will we do a lot of work and still be stuck? Might need a meta issue. However, that doesn't mean we can't go ahead here as long as the service is lazily instantiated.

  • 🇮🇹Italy mondrake 🇮🇹

    @catch 📌 Upgrade PHPUnit to 10, drop Symfony PHPUnit-bridge dependency Fixed is taking care of replacing the bridge deprecation reporting features that will not be ported to PHPUnit 10. That is (mostly) working already. In there, we could easily backfill expect[Error|Warning]*() methods, and in fact earlier versions of the MR had that in place; lately though, that part was dropped based on the Slack discussion in #11.

  • 🇮🇹Italy mondrake 🇮🇹

    @alexpott @catch @longwave mind moving the discussion on what to do with expect[Error|Warning]*() to 📌 Upgrade PHPUnit to 10, drop Symfony PHPUnit-bridge dependency Fixed ?

  • Assigned to mondrake
  • 🇮🇹Italy mondrake 🇮🇹

    I'll give a try to service closures. On it.

  • Pipeline finished with Success
    9 months ago
    Total: 546s
    #107119
  • Pipeline finished with Failed
    9 months ago
    Total: 614s
    #107152
  • Pipeline finished with Failed
    9 months ago
    Total: 517s
    #107164
  • Pipeline finished with Failed
    9 months ago
    Total: 289s
    #107185
  • Pipeline finished with Canceled
    9 months ago
    Total: 50s
    #107192
  • Pipeline finished with Failed
    9 months ago
    Total: 484s
    #107194
  • Issue was unassigned.
  • Status changed to Needs review 9 months ago
  • 🇮🇹Italy mondrake 🇮🇹

    Here we go.

  • Pipeline finished with Success
    9 months ago
    Total: 3964s
    #107219
  • Pipeline finished with Success
    9 months ago
    Total: 528s
    #110498
  • Status changed to RTBC 9 months ago
  • 🇺🇸United States smustgrave

    Appears all feedback has been addressed so going to remark

  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    This fix is so elegant - we should make more use of service enclosures for logging.

  • Status changed to Fixed 9 months ago
  • 🇬🇧United Kingdom alexpott 🇪🇺🌍

    Committed and pushed 25697a17e8 to 11.x and 5959b69d70 to 10.3.x. Thanks!

    • alexpott committed 5959b69d on 10.3.x
      Issue #3419693 by mondrake, alexpott, smustgrave, catch: Log error...
    • alexpott committed 25697a17 on 11.x
      Issue #3419693 by mondrake, alexpott, smustgrave, catch: Log error...
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024