Improve error handling and abstract download classes #52
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
Issue
When an add-on is unsupported or if a download URL can not be retrieved or resolved, an empty string is returned.
This has the unintended consequence of causing Composer to fail much later during the package installation routine resulting in the following error:
Example of the error "Failed to extract junaidbhura/ninja-forms-foobar"
To summarize this lengthy error: Composer is attempting to unzip a blank file downloaded from the empty string.
This error has confused many, including myself, as to the real cause. There are numerous issues about it.
Solution
The solution to this is to explicitly throw an exception during the processing of an add-on and the processing of the download URL to abort the package installation process sooner and provide a clear reason for what went wrong.
To simplify the addition of these error messages, I've abstracted common properties and methods into two base classes for greater consistency between plugin downloaders, especially EDD-distributed plugins.
I've chosen
UnexpectedValueException
since it seems like a good fit for most scenarios based on similar usage in Composer.There are currently four kinds of error messages thrown:
Example of the error "Could not find a matching package" (1)
Example of the error "Expected a valid download URL" (2)
Example of the error "Expected a valid download version number" (3)
Example of the error "Expected download version to match installed version" (4)
$ composer require junaidbhura/ninja-forms-foobar ./composer.json has been updated Running composer update junaidbhura/ninja-forms-foobar Gathering patches from patch file. Loading composer repositories with package information Updating dependencies Nothing to modify in lock file Installing dependencies from lock file (including require-dev) Package operations: 1 install, 0 updates, 0 removals In AbstractEddPlugin.php line 40: Expected download version (3.0.0) to match installed version (2.0.0) of package junaidbhura/ninja-forms-foobar
How has this been tested?
I'm testing this on a client project that uses Advanced Custom Fields Pro, ACF Extended Pro, and WPML.
Screenshots