Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Pass the SAPI file upload errors when throwing an exception #243

Merged
merged 7 commits into from
Aug 15, 2017
Merged

Pass the SAPI file upload errors when throwing an exception #243

merged 7 commits into from
Aug 15, 2017

Conversation

Erikvv
Copy link

@Erikvv Erikvv commented Apr 7, 2017

This will save people some time figuring out why the upload isn't working.

Can't think of any tests.

@@ -114,7 +126,9 @@ public function __construct($streamOrFile, $size, $errorStatus, $clientFilename
public function getStream()
{
if ($this->error !== UPLOAD_ERR_OK) {
throw new RuntimeException('Cannot retrieve stream due to upload error');
throw new RuntimeException(
'Cannot retrieve stream due to upload error: ' . self::$errorMessages[$this->error]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use sprintf to improve readability

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -147,7 +161,9 @@ public function moveTo($targetPath)
}

if ($this->error !== UPLOAD_ERR_OK) {
throw new RuntimeException('Cannot retrieve stream due to upload error');
throw new RuntimeException(
'Cannot retrieve stream due to upload error: ' . self::$errorMessages[$this->error]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if $this->error contains unsupported (in errorMessages) value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This invariant is already enforced in the constructor

        if (! is_int($errorStatus)
            || 0 > $errorStatus
            || 8 < $errorStatus
        ) {
            throw new InvalidArgumentException(
                'Invalid error status for UploadedFile; must be an UPLOAD_ERR_* constant'
            );
        }

@@ -16,6 +16,18 @@

class UploadedFile implements UploadedFileInterface
{
private static $errorMessages = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is static necessary?

Copy link
Author

@Erikvv Erikvv Apr 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good use case because it is immutable data. If it wasn't static it would be duplicated and it would increase the memory footprint of this class by a lot.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a constant instead; PHP has supported constant array values since 5.6, and they're a better fit here than a static member.

@Erikvv
Copy link
Author

Erikvv commented Jul 5, 2017

Can anyone please merge this?

@@ -16,6 +16,18 @@

class UploadedFile implements UploadedFileInterface
{
private static $errorMessages = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a constant instead; PHP has supported constant array values since 5.6, and they're a better fit here than a static member.

throw new RuntimeException(sprintf(
'Cannot retrieve stream due to upload error: %s',
self::$errorMessages[$this->error]
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be tested by passing a non-UPLOAD_ERR_OK value to the $errorStatus constructor argument, and then calling this method.

throw new RuntimeException(sprintf(
'Cannot retrieve stream due to upload error: %s',
self::$errorMessages[$this->error]
));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here.

@weierophinney weierophinney changed the base branch from develop to master August 15, 2017 21:40
Pass the SAPI file upload errors when throwing an exception
@weierophinney weierophinney changed the base branch from master to develop August 15, 2017 21:42
@weierophinney weierophinney added this to the 1.5.0 milestone Aug 15, 2017
@weierophinney
Copy link
Member

I've taken the liberty of making the requested changes and pushing them to your branch, @Erikvv .

@weierophinney weierophinney merged commit 0d45ce7 into zendframework:develop Aug 15, 2017
weierophinney added a commit that referenced this pull request Aug 15, 2017
weierophinney added a commit that referenced this pull request Aug 15, 2017
@weierophinney
Copy link
Member

Thanks, @Erikvv

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants