diff --git a/README.md b/README.md index 75592fb..85071a9 100644 --- a/README.md +++ b/README.md @@ -129,9 +129,9 @@ $client->all(array('type' => 'library')); $client->all(array('vendor' => 'sylius')); ``` -#### Custom Packagist Repositories +#### Custom Packagist repositories -You can also set a custom Packagist Repository URL: +You can also set a custom Packagist repository URL: ```php setPackagistUrl('https://custom.packagist.site.org'); ``` +## Errors + +* A `Packagist\Api\PackageNotFoundException` will be thrown when the Packagist API returns a 404 response. +* An `\InvalidArgumentException` will be thrown when the respond from Packagist was not able to be parsed. + ## License `packagist-api` is licensed under the MIT License - see the LICENSE file for details. diff --git a/spec/Packagist/Api/ClientSpec.php b/spec/Packagist/Api/ClientSpec.php index 484a3c7..c4b590d 100644 --- a/spec/Packagist/Api/ClientSpec.php +++ b/spec/Packagist/Api/ClientSpec.php @@ -5,9 +5,12 @@ namespace spec\Packagist\Api; use GuzzleHttp\Client as HttpClient; +use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Psr7\Request; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Stream; use Packagist\Api\Client; +use Packagist\Api\PackageNotFoundException; use Packagist\Api\Result\Factory; use PhpSpec\ObjectBehavior; @@ -195,4 +198,17 @@ public function it_filters_package_names_by_vendor(HttpClient $client, Factory $ $this->all(['vendor' => 'sylius']); } + public function it_throws_exception_on_404s(HttpClient $client): void + { + $request = new Request('GET', 'https://packagist.org/packages/i-do/not-exist.json'); + $response = new Response(404, [], json_encode(['status' => 'error', 'message' => 'Package not found'])); + $exception = new ClientException('', $request, $response); + + $client->request('GET', 'https://packagist.org/packages/i-do/not-exist.json') + ->shouldBeCalled() + ->willThrow($exception); + + $this->shouldThrow(PackageNotFoundException::class) + ->during('get', ['i-do/not-exist']); + } } diff --git a/src/Packagist/Api/Client.php b/src/Packagist/Api/Client.php index d5992f0..83e990d 100644 --- a/src/Packagist/Api/Client.php +++ b/src/Packagist/Api/Client.php @@ -9,7 +9,6 @@ use GuzzleHttp\Exception\GuzzleException; use Packagist\Api\Result\Factory; use Packagist\Api\Result\Package; -use Psr\Http\Message\StreamInterface; /** * Packagist Api @@ -247,6 +246,8 @@ protected function multiRespond(string $url1, string $url2) * * @param string $url * @return string + * @throws PackageNotFoundException + * @throws GuzzleException */ protected function request(string $url): string { @@ -256,7 +257,10 @@ protected function request(string $url): string ->getBody() ->getContents(); } catch (GuzzleException $e) { - return json_encode([]); + if ($e->getCode() === 404) { + throw new PackageNotFoundException('The requested package was not found.', 404); + } + throw $e; } } diff --git a/src/Packagist/Api/PackageNotFoundException.php b/src/Packagist/Api/PackageNotFoundException.php new file mode 100644 index 0000000..cdb564d --- /dev/null +++ b/src/Packagist/Api/PackageNotFoundException.php @@ -0,0 +1,14 @@ + $package) { // Create an empty package, only contains versions - $createdPackage = array( + $createdPackage = [ 'versions' => [], - ); + ]; foreach ($package as $branch => $version) { $createdPackage['versions'][$branch] = $version; }