Stop passing E_USER_ERROR to trigger_error() on PHP 8.4

Created on 4 August 2024, about 1 month ago
Updated 12 September 2024, 4 days 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 1 hour 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
    about 1 month ago
    Total: 169s
    #244660
  • Status changed to Needs review about 1 month 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
    about 1 month 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
    about 1 month ago
    Total: 623s
    #246900
  • πŸ‡«πŸ‡·France andypost

    blocker for PHP 8.4 adoption

  • Pipeline finished with Success
    17 days ago
    Total: 411s
    #269900
  • Pipeline finished with Canceled
    11 days ago
    Total: 2738s
    #275195
  • Pipeline finished with Failed
    11 days ago
    Total: 546s
    #275208
  • Pipeline finished with Success
    11 days 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

Production build 0.71.5 2024