Skip to content

Stacking error handlers #10646

Open
Open
@derrabus

Description

I'd like to register a temporary error handler that decorates my global error handler. I usually do that this way:

$previous = null;
$previous = set_error_handler(static function () use (&$previous): bool {
    // Special error handling here.
    
    // Fall back to the global error handler
    return is_callable($previous) && $previous(...func_get_args());
});

try {
    // Do something
} finally {
    restore_error_handler();
}

If you know a more elegant way to do this, please enlighten me. Psalm however does not like at all what I'm doing here. 🙈

Minimal version:

<?php

$previous = null;
$previous = set_error_handler(static function () use (&$previous): bool {
    return is_callable($previous) && $previous(...func_get_args());
});

https://psalm.dev/r/8d2ca309f8

ERROR: TypeDoesNotContainType - 5:12 - Type null for $previous is never callable

ERROR: InvalidFunctionCall - 5:38 - Cannot treat type never as callable

INFO: UnusedVariable - 4:1 - $previous is never referenced or the value is not used

All three errors are false positives imo.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions