Skip to content

Commit 6fbf8a3

Browse files
Merge pull request #83 from robbieaverill/bugfix/package-404
Requesting a package that results in a 404 now throws a PackageNotFoundException
2 parents acf5f57 + 7e8b332 commit 6fbf8a3

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,21 @@ $client->all(array('type' => 'library'));
129129
$client->all(array('vendor' => 'sylius'));
130130
```
131131

132-
#### Custom Packagist Repositories
132+
#### Custom Packagist repositories
133133

134-
You can also set a custom Packagist Repository URL:
134+
You can also set a custom Packagist repository URL:
135135

136136
```php
137137
<?php
138138

139139
$client->setPackagistUrl('https://custom.packagist.site.org');
140140
```
141141

142+
## Errors
143+
144+
* A `Packagist\Api\PackageNotFoundException` will be thrown when the Packagist API returns a 404 response.
145+
* An `\InvalidArgumentException` will be thrown when the respond from Packagist was not able to be parsed.
146+
142147
## License
143148

144149
`packagist-api` is licensed under the MIT License - see the LICENSE file for details.

spec/Packagist/Api/ClientSpec.php

+16
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
namespace spec\Packagist\Api;
66

77
use GuzzleHttp\Client as HttpClient;
8+
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Psr7\Request;
810
use GuzzleHttp\Psr7\Response;
911
use GuzzleHttp\Psr7\Stream;
1012
use Packagist\Api\Client;
13+
use Packagist\Api\PackageNotFoundException;
1114
use Packagist\Api\Result\Factory;
1215
use PhpSpec\ObjectBehavior;
1316

@@ -195,4 +198,17 @@ public function it_filters_package_names_by_vendor(HttpClient $client, Factory $
195198
$this->all(['vendor' => 'sylius']);
196199
}
197200

201+
public function it_throws_exception_on_404s(HttpClient $client): void
202+
{
203+
$request = new Request('GET', 'https://packagist.org/packages/i-do/not-exist.json');
204+
$response = new Response(404, [], json_encode(['status' => 'error', 'message' => 'Package not found']));
205+
$exception = new ClientException('', $request, $response);
206+
207+
$client->request('GET', 'https://packagist.org/packages/i-do/not-exist.json')
208+
->shouldBeCalled()
209+
->willThrow($exception);
210+
211+
$this->shouldThrow(PackageNotFoundException::class)
212+
->during('get', ['i-do/not-exist']);
213+
}
198214
}

src/Packagist/Api/Client.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use GuzzleHttp\Exception\GuzzleException;
1010
use Packagist\Api\Result\Factory;
1111
use Packagist\Api\Result\Package;
12-
use Psr\Http\Message\StreamInterface;
1312

1413
/**
1514
* Packagist Api
@@ -247,6 +246,8 @@ protected function multiRespond(string $url1, string $url2)
247246
*
248247
* @param string $url
249248
* @return string
249+
* @throws PackageNotFoundException
250+
* @throws GuzzleException
250251
*/
251252
protected function request(string $url): string
252253
{
@@ -256,7 +257,10 @@ protected function request(string $url): string
256257
->getBody()
257258
->getContents();
258259
} catch (GuzzleException $e) {
259-
return json_encode([]);
260+
if ($e->getCode() === 404) {
261+
throw new PackageNotFoundException('The requested package was not found.', 404);
262+
}
263+
throw $e;
260264
}
261265
}
262266

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Packagist\Api;
6+
7+
use InvalidArgumentException;
8+
9+
/**
10+
* Thrown when a requested package was not found in the Packagist API.
11+
*/
12+
class PackageNotFoundException extends InvalidArgumentException
13+
{
14+
}

src/Packagist/Api/Result/Factory.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ public function createSearchResults(array $results): array
7676
* @param array $packages
7777
* @return Package[]
7878
*/
79-
public function createComposerPackagesResults(array $packages)
79+
public function createComposerPackagesResults(array $packages): array
8080
{
81-
$created = array();
81+
$created = [];
8282

8383
foreach ($packages as $name => $package) {
8484
// Create an empty package, only contains versions
85-
$createdPackage = array(
85+
$createdPackage = [
8686
'versions' => [],
87-
);
87+
];
8888
foreach ($package as $branch => $version) {
8989
$createdPackage['versions'][$branch] = $version;
9090
}

0 commit comments

Comments
 (0)