Follow-up to #2463285: Support PHP7 EngineExceptions in the error handler

Problem/Motivation

PHP 7 adds a custom error class, similar to exceptions.
Before this issue, we listened to BaseExceptions which was replaced by the Throwable interface in https://wiki.php.net/rfc/throwable-interface

Proposed resolution

Use Throwable.

Remaining tasks

User interface changes

N/A

API changes

N/A

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

stefan.r’s picture

Per the RFC at https://wiki.php.net/rfc/throwable-interface

Backwards Compatibility

Throwable, Error, TypeError, and ParseError will be built-in interfaces/classes and so it will no longer be possible for users to create classes with those exact names. It will still be possible for those names to be used within a non-global namespace.

So probably no need to rename the Error class, and just alias where necessary?

stefan.r’s picture

Status: Needs work » Needs review
FileSize
2.28 KB
stefan.r’s picture

FileSize
3.06 KB
stefan.r’s picture

FileSize
4.38 KB

I guess this would need to be postponed on the change actually getting into the PHP7 master branch.

tim.plunkett’s picture

  1. +++ b/core/includes/bootstrap.inc
    @@ -679,7 +679,7 @@ function _drupal_error_handler($error_level, $message, $filename, $line, $contex
    + * @param \Exception|\Error $exception
    

    Given the current RFC, couldn't this be @param \ThrowableInterface $exception?

  2. +++ b/core/includes/bootstrap.inc
    @@ -689,8 +689,8 @@ function _drupal_exception_handler($exception) {
    +  catch (Error $exception2) {
         _drupal_exception_handler_additional($exception, $exception2);
    

    Do we want to switch to $error from $exception2?

stefan.r’s picture

FileSize
2.79 KB
4.78 KB

Yes that makes sense, PHP5 doesn't have ThrowableInterface but we can probably live with that...

stefan.r’s picture

FileSize
463 bytes
4.79 KB
stefan.r’s picture

Seems this was merged last week: https://github.com/php/php-src/pull/1284

Could anyone do a test run on PHP7 master?

Berdir queued 7: 2505315-6.patch for re-testing.

Berdir’s picture

Status: Needs review » Needs work
  1. +++ b/core/lib/Drupal/Core/Utility/Error.php
    @@ -33,7 +33,7 @@ class Error {
        *
    -   * @param \Exception|\BaseException $exception
    +   * @param \ThrowableInterface $exception
    

    Not sure about this, since it only exists in PHP 7? Should we use \Exception|\ThrowableInterface maybe?

  2. +++ b/core/modules/system/src/Tests/System/ErrorHandlerTest.php
    @@ -52,8 +52,8 @@ function testErrorHandler() {
         if (version_compare(PHP_VERSION, '7.0.0-dev') >= 0)  {
    -      // In PHP 7, instead of a recoverable fatal error we get a TypeException.
    -      $fatal_error['%type'] = 'TypeException';
    +      // In PHP 7, instead of a recoverable fatal error we get a TypeError.
    +      $fatal_error['%type'] = 'TypeError';
           // The error message also changes in PHP 7.
    

    This fixes the test fail but there's still an exception because we don't unset an assertion anymore a bit below this. Change TypeException to TypeError there.

anavarre’s picture

Issue tags: +PHP 7.0 (duplicate)
stefan.r’s picture

Status: Needs work » Needs review
FileSize
5.87 KB
3.11 KB
dawehner’s picture

This IS is a little bit confusing. It talks about rename the error class, so is this something which still needs to be done?

On top of that I'm reading https://wiki.php.net/rfc/throwable-interface and ThrowableInterface is the base of both \Error and \Exception
I understant that we need to catch both \Exception and \Throwable due to the PHP 5 compatibility

+++ b/core/includes/bootstrap.inc
@@ -689,12 +689,12 @@ function _drupal_exception_handler($exception) {
+  catch (\Error $error) {

@@ -1098,12 +1098,12 @@ function _drupal_shutdown_function() {
+  catch (\Error $error) {

So those cases could catch the interface itself. Maybe new ones are introduced in the future, you never know.

stefan.r’s picture

Per the RFC the Error class shouldn't have to be renamed: https://wiki.php.net/rfc/throwable-interface

dawehner’s picture

Issue summary: View changes
Status: Needs review » Needs work

Per the RFC the Error class shouldn't have to be renamed:

Right, because its not a scalar type but rather a proper namespaced class.

This makes sense, updated the issue summary.

The code improvement suggestion in #13 is still true

stefan.r’s picture

Status: Needs work » Needs review
FileSize
1.09 KB
6.01 KB
stefan.r’s picture

Title: Catch PHP7 Error objects instead of BaseExceptions in the error handler » Catch PHP7 Throwable objects instead of BaseExceptions in the error handler
stefan.r’s picture

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Hehe, yeah that name is a bit confusing, but this is life.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

I ran the changed test in PHP7 - all looks good. Committed a8f8b33 and pushed to 8.0.x. Thanks!

  • alexpott committed a8f8b33 on 8.0.x
    Issue #2505315 by stefan.r, dawehner, tim.plunkett, Berdir: Catch PHP7...

Status: Fixed » Closed (fixed)

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