Skip to content

[RFC][ErrorHandler] Add a simple error handler for the userland #32936

@vudaltsov

Description

@vudaltsov

Now that we have a separate component named ErrorHandler, I have an idea.

It's quite common to do this in the userland code:

$handle = @fopen('file.txt', 'rb');

if (false === $handle) {
    throw new FileNotFoundException();
}

I think it would be nice to have a DSL for better handling of such cases:

$handle = errorHandler()
    ->try(static function() {
        return fopen('file.txt', 'rb');
    })
    // or ->try('fopen', 'file.txt', 'rb')
    ->catchWarning(static function(string $message) {
        throw new FileNotFoundException();
    })
    // ->catchNotice()
;

Or with some reusability options:

$fopen = errorHandler('fopen')
    ->catchWarning(static function(string $message) {
        throw new FileNotFoundException();
    })
;

$fopen('file.txt', 'rb');
$fopen('another.xml', 'wb');

This mechanism could replace error handling in the Filesystem and other components across the framework.

@yceruto , WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    ErrorHandlerFeatureRFCRFC = Request For Comments (proposals about features that you want to be discussed)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions