Skip to content

ErrorException from error handler static utility method #18864

Open
@uuf6429

Description

@uuf6429

Description

  • since PHP is slowly transitioning from errors to exceptions
  • since it's common for devs to want to handle errors and exception in a uniform way
  • (also judging by such utility code in the php documentation)

It may be beneficial to implement a ErrorException::handleError() (or similar) static method, which can be fed directly to set_error_handler().

So essentially the example here would be built into the ErrorException class.

// internal code
class ErrorException extends Exception
{
    public static function handleError(int $errno, string $errstr, string $errfile, int $errline): void
    {
        if (!(error_reporting() & $errno)) {
            return;
        }

        if ($errno === E_DEPRECATED || $errno === E_USER_DEPRECATED) {
            return;
        }

        throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
    }
}

// userland code
set_error_handler(ErrorException::handleError(...)); // or array call or whatever

Implementation-wise I don't think this would cause any BC issues, nor should it be invasive.

Looking at Symfony's error handler, there's a lot more going on in there, so this general approach probably won't cut it for Symfony.

But there are otherwise many implementations that are similar to the example or simple/poorer: https://github.com/search?q=language%3APHP+set_error_handler+errorexception&type=code (essentially this is what I'd like to tackle mainly)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions