safe icon indicating copy to clipboard operation
safe copied to clipboard

Bug: False error message with sopressed error @ on createFromFormat

Open BruceGitHub opened this issue 5 years ago • 0 comments

This code

    /** @test */
    public function it_fails_with_bad_message(): void
    {
        \Safe\DateTime::createFromFormat('U',$this->generate_error());
    }

    public function generate_error()
    {
        @include "non_exist_file.php";  //some soppred error 
        return (new \DateTime())->getTimestamp().'x';
    }

Produce this otput

 ✘ It fails with bad message  20 ms
   ┐
   ├ Safe\Exceptions\DatetimeException: include(): Failed opening 'non_exist_file.php' for inclusion (include_path='.:/usr/local/Cellar/php/7.4.7/share/php/pear')
   │

The probles it's with code

class DatetimeException extends \ErrorException implements SafeExceptionInterface
{
    public static function createFromPhpError(): self
    {
        $error = error_get_last(); <- keep the last error (from include) 
        return new self($error['message'] ?? 'An error occured', 0, $error['type'] ?? 1);
    }
}

It's possible resolve with this

class DatetimeException extends \ErrorException implements SafeExceptionInterface
{
    public static function createFromPhpError(): self
    {
        $error = \DateTime::getLastErrors();
        return new self($error['message'] ?? 'An error occured', 0, $error['type'] ?? 1);
    }
}

I think :-)

BruceGitHub avatar Sep 08 '20 20:09 BruceGitHub