Skip to content

Commit bf8fe48

Browse files
author
Mathieu
authored
refactor(elasticsearch): ignore Missing404Exception instead of catching it (api-platform#5302)
1 parent e5f1be0 commit bf8fe48

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

src/Elasticsearch/State/ItemProvider.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use ApiPlatform\Metadata\Operation;
2020
use ApiPlatform\State\ProviderInterface;
2121
use Elasticsearch\Client;
22-
use Elasticsearch\Common\Exceptions\Missing404Exception;
2322
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2423
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2524

@@ -45,18 +44,18 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
4544
$resourceClass = $operation->getClass();
4645
$documentMetadata = $this->documentMetadataFactory->create($resourceClass);
4746

48-
try {
49-
$params = [
50-
'index' => $documentMetadata->getIndex(),
51-
'id' => (string) reset($uriVariables),
52-
];
47+
$params = [
48+
'client' => ['ignore' => 404],
49+
'index' => $documentMetadata->getIndex(),
50+
'id' => (string) reset($uriVariables),
51+
];
5352

54-
if (ElasticsearchVersion::supportsMappingType()) {
55-
$params['type'] = $documentMetadata->getType();
56-
}
53+
if (ElasticsearchVersion::supportsMappingType()) {
54+
$params['type'] = $documentMetadata->getType();
55+
}
5756

58-
$document = $this->client->get($params);
59-
} catch (Missing404Exception) {
57+
$document = $this->client->get($params);
58+
if (!$document['found']) {
6059
return null;
6160
}
6261

tests/Elasticsearch/State/ItemProviderTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use ApiPlatform\Metadata\Get;
2121
use ApiPlatform\Tests\Fixtures\TestBundle\Entity\Foo;
2222
use Elasticsearch\Client;
23-
use Elasticsearch\Common\Exceptions\Missing404Exception;
2423
use PHPUnit\Framework\TestCase;
2524
use Prophecy\PhpUnit\ProphecyTrait;
2625
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
@@ -55,7 +54,6 @@ public function testGetItem(): void
5554
'_index' => 'foo',
5655
'_type' => '_doc',
5756
'_id' => '1',
58-
'_version' => 1,
5957
'found' => true,
6058
'_source' => [
6159
'id' => 1,
@@ -69,7 +67,7 @@ public function testGetItem(): void
6967
$foo->setBar('erèinissor');
7068

7169
$clientProphecy = $this->prophesize(Client::class);
72-
$clientProphecy->get(['index' => 'foo', 'id' => '1'])->willReturn($document)->shouldBeCalled();
70+
$clientProphecy->get(['client' => ['ignore' => 404], 'index' => 'foo', 'id' => '1'])->willReturn($document)->shouldBeCalled();
7371

7472
$denormalizerProphecy = $this->prophesize(DenormalizerInterface::class);
7573
$denormalizerProphecy->denormalize($document, Foo::class, DocumentNormalizer::FORMAT, [AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true])->willReturn($foo)->shouldBeCalled();
@@ -79,13 +77,18 @@ public function testGetItem(): void
7977
self::assertSame($foo, $itemDataProvider->provide((new Get())->withClass(Foo::class), ['id' => 1]));
8078
}
8179

82-
public function testGetItemWithMissing404Exception(): void
80+
public function testGetInexistantItem(): void
8381
{
8482
$documentMetadataFactoryProphecy = $this->prophesize(DocumentMetadataFactoryInterface::class);
8583
$documentMetadataFactoryProphecy->create(Foo::class)->willReturn(new DocumentMetadata('foo'))->shouldBeCalled();
8684

8785
$clientProphecy = $this->prophesize(Client::class);
88-
$clientProphecy->get(['index' => 'foo', 'id' => '404'])->willThrow(new Missing404Exception())->shouldBeCalled();
86+
$clientProphecy->get(['client' => ['ignore' => 404], 'index' => 'foo', 'id' => '404'])->willReturn([
87+
'_index' => 'foo',
88+
'_type' => '_doc',
89+
'_id' => '404',
90+
'found' => false,
91+
])->shouldBeCalled();
8992

9093
$itemDataProvider = new ItemProvider($clientProphecy->reveal(), $documentMetadataFactoryProphecy->reveal(), $this->prophesize(DenormalizerInterface::class)->reveal());
9194

0 commit comments

Comments
 (0)