2121use ApiPlatform \State \ApiResource \Error ;
2222use ApiPlatform \State \Pagination \Pagination ;
2323use ApiPlatform \State \ProviderInterface ;
24+ use Elasticsearch \Client as V7Client ;
25+ use Elasticsearch \Common \Exceptions \Missing404Exception as V7Missing404Exception ;
2426use Elastic \Elasticsearch \Client ;
2527use Elastic \Elasticsearch \Exception \ClientResponseException ;
2628use Elastic \Elasticsearch \Response \Elasticsearch ;
@@ -37,7 +39,7 @@ final class CollectionProvider implements ProviderInterface
3739 /**
3840 * @param RequestBodySearchCollectionExtensionInterface[] $collectionExtensions
3941 */
40- public function __construct (private readonly Client $ client , private readonly ?DenormalizerInterface $ denormalizer = null , private readonly ?Pagination $ pagination = null , private readonly iterable $ collectionExtensions = [], private readonly ?InflectorInterface $ inflector = new Inflector ())
42+ public function __construct (private readonly V7Client | Client $ client , private readonly ?DenormalizerInterface $ denormalizer = null , private readonly ?Pagination $ pagination = null , private readonly iterable $ collectionExtensions = [], private readonly ?InflectorInterface $ inflector = new Inflector ())
4143 {
4244 }
4345
@@ -69,12 +71,14 @@ public function provide(Operation $operation, array $uriVariables = [], array $c
6971
7072 try {
7173 $ documents = $ this ->client ->search ($ params );
74+ } catch (V7Missing404Exception $ e ) {
75+ throw new Error (status: $ e ->getCode (), detail: $ e ->getMessage (), title: $ e ->getMessage (), originalTrace: $ e ->getTrace ());
7276 } catch (ClientResponseException $ e ) {
7377 $ response = $ e ->getResponse ();
7478 throw new Error (status: $ response ->getStatusCode (), detail: (string ) $ response ->getBody (), title: $ response ->getReasonPhrase (), originalTrace: $ e ->getTrace ());
7579 }
7680
77- if ($ documents instanceof Elasticsearch) {
81+ if ($ this -> client instanceof Client && $ documents instanceof Elasticsearch) {
7882 $ documents = $ documents ->asArray ();
7983 }
8084
0 commit comments