Skip to content

[HttpFoundation] When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error #54304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.3
Choose a base branch
from

Conversation

ArmCyber
Copy link

@ArmCyber ArmCyber commented Mar 15, 2024

When calling Symfony\Component\HttpFoundation\File\UploadedFile::getErrorMessage() for an uploaded file which has no error and is uploaded successfully, should not give any error message.
Before this changes it returned The file "%s" was not uploaded due to an unknown error..
After this changes, it will return an empty string.

Q A
Branch? 7.1
Bug fix? yes
New feature? no
Deprecations? no
Issues no
License MIT

@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.1 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@carsonbot carsonbot changed the title When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error [HttpFoundation] When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error Mar 15, 2024
@smnandre
Copy link
Member

I fear this contradict the implementation of the move() method

If this->isValid() returns false, with your change the method would throw an exception with no message.

    /**
     * Moves the file to a new location.
     *
     * @throws FileException if, for any reason, the file could not have been moved
     */
    public function move(string $directory, ?string $name = null): File
    {
        if ($this->isValid()) {

@nicolas-grekas
Copy link
Member

Not sure what you mean @smnandre, can you elaborate or share a test case?

@smnandre
Copy link
Member

smnandre commented Mar 18, 2024

public function testInvalidFile()
{
    $this->expectException(FileException::class);
    $this->expectExceptionMessage('The file "original.gif" was not uploaded due to an unknown error.');

    $file = new UploadedFile(
        __DIR__.'/Fixtures/test.gif',
        'original.gif',
        'image/gif',
    );

    $file->move(__DIR__.'/Fixtures/directory');
}
Testing Symfony\Component\HttpFoundation\Tests\File\UploadedFileTest
...................F..............                                34 / 34 (100%)

Time: 00:00.024, Memory: 12.00 MB

There was 1 failure:

1) Symfony\Component\HttpFoundation\Tests\File\UploadedFileTest::testInvalidFile
- Failed asserting that exception message '' contains 'The file "original.gif" was not uploaded due to an unknown error.'.

@smnandre
Copy link
Member

The error message is used as exception message, so it seems to me this code was a failsafe if "something went wrong".

@welcoMattic
Copy link
Member

I guess we should add a new case at the end of move method to handle \UPLOAD_ERR_OK error, in order to have an empty string from UploadedFile::getErrorMessage(), but a legit exception message thrown by move method on the use case provided by @smnandre. WDYT?

@smnandre
Copy link
Member

smnandre commented Apr 5, 2024

I'm wondering if we can handle this very-specific use case in another way, because your changes make sense and are easier to "read" than this special "no error but an error" case.

If no, your solution seems very sensible yes!

@ArmCyber
Copy link
Author

ArmCyber commented Apr 8, 2024

Thanks @smnandre for the review!

I'm considering renaming the original method "getErrorMessage()" to private "getExceptionMessage()" and creating a new "getErrorMessage()" method, which will check if "$this->error === \UPLOAD_ERR_OK", and if it does, it will return null (or an empty string to ensure backward compatibility); otherwise, it will internally call "getExceptionMessage()". Then, in the "move()" method, we can utilize the "getExceptionMessage()" method.

What do you think?

P.S. I'll also add your test case to the UploadedFileTest.php.

@smnandre
Copy link
Member

smnandre commented Apr 8, 2024

Seems perfect to me.

@nicolas-grekas ?

…rror and is uploaded successfully, it should not return an error
@ArmCyber
Copy link
Author

Thanks @smnandre!
@nicolas-grekas, what do you think about these changes?

@xabbuh xabbuh modified the milestones: 7.1, 5.4 May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants