safe icon indicating copy to clipboard operation
safe copied to clipboard

Suppressing errors

Open jakubvojacek opened this issue 6 years ago • 3 comments

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

jakubvojacek avatar Jan 13 '19 10:01 jakubvojacek

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.

moufmouf avatar Feb 02 '19 14:02 moufmouf

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.

mjanser avatar Jan 31 '20 09:01 mjanser

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.

josefsabl avatar Nov 18 '20 08:11 josefsabl