thecodingmachine / safe

All PHP functions, rewritten to throw exceptions instead of returning false
MIT License
2.36k stars 143 forks source link

Suppressing errors #77

Open jakubvojacek opened 5 years ago

jakubvojacek commented 5 years ago

Hello,

we have a custom error handler

set_error_handler(static function ($severity, $message, $file, $line): void {
    if (error_reporting() === 0) {
        /// @ sign temporary disabled error reporting
        return;
    }

    throw new \ErrorException($message, 0, $severity, $file, $line);
});

and in combination with thecodingmachine/safe it does not work as I would expect. For example Safe\file_put_contents throws now ErrorException instead of FilesystemException because the warning is caught by my error handler. Adding @ to Safe\file_put_contents does resolve it. Or is there a better way how to have custom handler and still use thecodingmachine/safe fully?

Thank you Jakub

moufmouf commented 5 years ago

Hey @jakubvojacek ,

I've had a look at it the other way. Yes, your error handler will catch most of the errors (making Safe less useful). That's actually ok. I did not want Safe to break existing error handlers.

Also, there are errors that are caught by Safe and not caught by error handlers (like when using json_decode I think).

I tried to document it here: https://thecodingmachine.io/introducing-safe-php

Still, I might reconsider if other people think I should do it. I'll keep the issue open meanwhile.

mjanser commented 4 years ago

Hello,

I agree that adding @ to some internal function calls would make sense. The example of file_put_contents seems to be a bit related to the Symfony Filesystem component. There they use @ a lot and in some cases use a custom error handler during the function call to catch the last error. See https://github.com/symfony/filesystem/blob/master/Filesystem.php.

Additionally I would love to see better handling of XML errors using libxml_use_internal_errors. See https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Config/Util/XmlUtils.php#L53 as an example.

josefsabl commented 3 years ago

I also would appreciate suppressing these warnings very much, this is similar/same issue https://github.com/thecodingmachine/safe/issues/120

Note: We are not using any custom error handler but we are using tracy/tracy which catches all unhandled exceptions/errors and turns them into Error500 page, and logs the error. It obviously also stops execution of program.