Stop passing E_USER_ERROR to trigger_error() on PHP 8.4

Created on 4 August 2024, 3 months ago
Updated 17 September 2024, about 2 months ago

Problem/Motivation

RFC accepted https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_passing_e_user_e...

Steps to reproduce

core$ git grep E_USER_ERROR
core/includes/errors.inc:34:    E_USER_ERROR => ['User error', RfcLogLevel::ERROR],
core/includes/errors.inc:70:    $to_string = $error_level == E_USER_ERROR && str_ends_with($caller['function'], '__toString()');
core/lib/Drupal/Component/Diff/DiffFormatter.php:137:        trigger_error('Unknown edit type', E_USER_ERROR);
core/lib/Drupal/Component/Utility/ToStringTrait.php:20:      trigger_error(get_class($e) . ' thrown while calling __toString on a ' . static::class . ' object in ' . $e->getFile() . ' on line ' . $e->getLine() . ': ' . $e->getMessage(), E_USER_ERROR);
core/lib/Drupal/Component/Utility/ToStringTrait.php:22:      // E_USER_ERROR, we terminate execution. However, for test purposes allow
core/modules/big_pipe/src/Render/BigPipe.php:375:          trigger_error($e, E_USER_ERROR);
core/modules/big_pipe/src/Render/BigPipe.php:411:          trigger_error($e, E_USER_ERROR);
core/modules/big_pipe/src/Render/BigPipe.php:562:            trigger_error($e, E_USER_ERROR);
core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php:82:    $this->assertEquals(E_USER_ERROR, $this->lastErrorNumber);

Proposed resolution

clean-up usage or make it conditional

- ToStringTrait is deprecated as any exception still will be caught by error hadler
- BigPipe using it instead of properly logging errors

This technique is used to log errors mostly in core because Drupal does not stop execution for user-level errors, allowing the application to log and continue. Maybe it needs changes but it's out of scope of this issue

Remaining tasks

- file MR and change record
- commit

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ“Œ Task
Status

Needs review

Version

11.0 πŸ”₯

Component
BaseΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡«πŸ‡·France andypost

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

Merge Requests

Comments & Activities

  • Issue created by @andypost
  • πŸ‡¨πŸ‡­Switzerland berdir Switzerland

    ToStringTrait might be tricky, because exceptions and __toString() methods don't like each other, unless that has changed somehow?

  • πŸ‡«πŸ‡·France andypost

    And the suggestion in RFC is to use proper exceptions

    Using exceptions instead solves all the above problems, and allows catching the error outside the problematic code path.

    If the desired outcome is to terminate the program with no possible way to recover one should use the exit() function with a string argument.

    Therefore we propose to deprecate passing E_USER_ERROR to trigger_error()

  • πŸ‡¨πŸ‡­Switzerland berdir Switzerland

    Looks like exception handling in __toString() methods got fixed in PHP 7.4: https://3v4l.org/5ZJ2i. That means we can drop the try/catch and even consider to deprecate that trait? It's only used in two classes in core.

  • Merge request !9085Draft: Deprecate ToStringTrait #3465827 β†’ (Open) created by andypost
  • Pipeline finished with Failed
    3 months ago
    Total: 169s
    #244660
  • Status changed to Needs review 3 months ago
  • πŸ‡«πŸ‡·France andypost

    Yes, just 2 implementations to add, and deprecated, now needs to clean-up _drupal_error_handler_real(), maybe better to split out fixes for the remaining usage for scope

  • Pipeline finished with Failed
    3 months ago
    Total: 595s
    #244673
  • πŸ‡«πŸ‡·France andypost

    @Berdir thank you! it works and the only test needs to be adjusted

    Drupal\Tests\Core\StringTranslation\TranslatableMarkupTest::testToString
    Failed asserting that null matches expected 256.
    
    core/tests/Drupal/Tests/Core/StringTranslation/TranslatableMarkupTest.php:77
  • πŸ‡«πŸ‡·France andypost

    There's just 4 usages in contrib, so needs to improve message and decide with test, polish CR

  • Pipeline finished with Success
    3 months ago
    Total: 623s
    #246900
  • πŸ‡«πŸ‡·France andypost

    blocker for PHP 8.4 adoption

  • Pipeline finished with Success
    3 months ago
    Total: 411s
    #269900
  • Pipeline finished with Canceled
    2 months ago
    Total: 2738s
    #275195
  • Pipeline finished with Failed
    2 months ago
    Total: 546s
    #275208
  • Pipeline finished with Success
    2 months ago
    Total: 949s
    #275216
  • πŸ‡«πŸ‡·France andypost

    Probably deprecation of ToStringTrait better to move to separate issue or it needs second change record

  • πŸ‡«πŸ‡·France andypost

    Added to summary why most of time it needs to decide if code needs exception or logging instead of throwing user error as everyone thinks it should be fatal but there's exit() or exception re-thrown

    Drupal does not stop execution for user-level errors, allowing the application to log and continue

  • πŸ‡¬πŸ‡§United Kingdom catch

    I think for 10.4/11.1 we should change all these to E_USER_WARNING and then open follow-ups for each case to consider adding:

    1. an assert() so it fails hard during development
    2. An exception from 12.x onwards if that's appropriate

  • πŸ‡«πŸ‡·France andypost

    Related required for BigPipe fix

  • πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia

    kim.pepper β†’ made their first commit to this issue’s fork.

  • πŸ‡¦πŸ‡ΊAustralia kim.pepper πŸ„β€β™‚οΈπŸ‡¦πŸ‡ΊSydney, Australia

    Created an MR for #13

  • Pipeline finished with Failed
    23 days ago
    #318723
  • πŸ‡«πŸ‡·France andypost

    Some failures looks fixable

  • First commit to issue fork.
  • Pipeline finished with Failed
    16 days ago
    Total: 1184s
    #325072
  • Pipeline finished with Failed
    12 days ago
    Total: 117s
    #328170
  • Pipeline finished with Success
    12 days ago
    Total: 1237s
    #328175
  • Pipeline finished with Success
    11 days ago
    Total: 559s
    #329123
  • πŸ‡«πŸ‡·France andypost

    MR!9924 is green now, so it needs follow-up to deprecate ToStringTrait

Production build 0.71.5 2024