Skip to content

Bug: False error message with sopressed error @ on createFromFormat  #239

@BruceGitHub

Description

@BruceGitHub

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 :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions