samsonasik / ErrorHeroModule

:gem: A Hero for your Zend Framework/Laminas, and Expressive/Mezzio application to log ( DB and Mail ) and handle php errors & exceptions during Mvc process/between request and response
MIT License
50 stars 7 forks source link

Ignoring errors suppressed by @ #36

Closed anatolykhelmer closed 6 years ago

anatolykhelmer commented 6 years ago

What ZF application I'm using when issue happen ?

What PHP version you're using?

Expected behavior

Errors suppressed by @ should be ignored. PHP Manual: If you have set a custom error handler function with set_error_handler() then it will still get called, but this custom error handler can (and should) call error_reporting() which will return 0 when the call that triggered the error was preceded by an @.

Actual behavior

Errors suppressed by @ are being caught.

Steps/Codes to reproduce the behavior

Try to run @mkdir('somedir') on directory that already exists.

samsonasik commented 6 years ago

If you have an @ and want to ignore, you can actually ignore with Zend\Stdlib\ErrorHandler:

use Zend\Stdlib\ErrorHandler;

ErrorHandler::start();
@mkdir('test');
@mkdir('test');
ErrorHandler::stop();
anatolykhelmer commented 6 years ago

This is what I'm doing now when the code is mine. What if the code is not mine? For example, PHPUnit uses "@" sign when creating directories for code coverage report.

samsonasik commented 6 years ago

I'm not sure that can/should be handled via this module, using @ doesn't make error_reporting() return 0. User need to have error_reporting(0) before @ usage, so, for example:

error_reporting(0);
@mkdir('test');

which means, you need to touch the code.

Then we can handle it inside set_error_handler:

function phpErrorHandler(int $errorType, string $errorMessage, string $errorFile, int $errorLine) : void
{       
      if (!(error_reporting() & $errorType)) {
          return;
      }
      // ...
}
anatolykhelmer commented 6 years ago

Please, take a look at PHP manual: http://php.net/manual/en/language.operators.errorcontrol.php

call error_reporting() which will return 0 when the call that triggered the error was preceded by an @

So it should return 0.

samsonasik commented 6 years ago

please check this https://3v4l.org/IWOMt

anatolykhelmer commented 6 years ago

Ok. You should set handler BEFORE. Take a look here https://3v4l.org/f7ekM

samsonasik commented 6 years ago

oh yeah, I misplaced that when was checking it. Definitely a good catch ;). I created a commit https://github.com/samsonasik/ErrorHeroModule/commit/bddeec8f0a77d202d15b3d43cfc8f6a3acb08f32 . Please try use dev-master with registering:

"samsonasik/error-hero-module": "dev-master",

at composer.json and run composer update and verify it. If it fixes the issue, I will create new release.

samsonasik commented 6 years ago

@anatolykhelmer fixed at version 2.1.8, backported to version 1.9.6 ( 1.x.x version ). Thank you ;)