From 445e2fbfa5afed2f0b4417853b39728fb1cbe201 Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 8 Mar 2021 11:19:32 +1300 Subject: [PATCH 1/2] Catch exception when no keys --- src/Service/AlgoliaService.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Service/AlgoliaService.php b/src/Service/AlgoliaService.php index fc199ea..0b02c68 100644 --- a/src/Service/AlgoliaService.php +++ b/src/Service/AlgoliaService.php @@ -8,6 +8,8 @@ use SilverStripe\Control\Director; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Dev\Debug; +use SilverStripe\Security\Security; class AlgoliaService { @@ -30,6 +32,7 @@ public function getClient() { if (!$this->client) { if (!$this->adminApiKey) { + throw new Exception('No adminApiKey configured for '. self::class); } @@ -56,10 +59,30 @@ public function getClient() */ public function initIndexes($item = null) { + if (!Security::database_is_ready()) { + return []; + } + + try { + $client = $this->getClient(); + + if (!$client) { + return []; + } + } catch (Exception $e) { + Injector::inst()->create(LoggerInterface::class)->error($e); + + if (Director::isDev()) { + Debug::message($e->getMessage()); + } + + return []; + } + if (!$item) { return array_map( - function ($indexName) { - return $this->getClient()->initIndex($this->environmentizeIndex($indexName)); + function ($indexName) use ($client) { + return $client->initIndex($this->environmentizeIndex($indexName)); }, array_keys($this->indexes) ); @@ -90,7 +113,7 @@ function ($indexName) { $output = []; foreach ($matches as $index) { - $output[$index] = $this->getClient()->initIndex($this->environmentizeIndex($index)); + $output[$index] = $client->initIndex($this->environmentizeIndex($index)); } return $output; From 053fcc6ae3eb9d32a85b7e59a5b810d9c0538c1f Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 8 Mar 2021 11:31:07 +1300 Subject: [PATCH 2/2] Space --- src/Service/AlgoliaService.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Service/AlgoliaService.php b/src/Service/AlgoliaService.php index 0b02c68..a2a2c3e 100644 --- a/src/Service/AlgoliaService.php +++ b/src/Service/AlgoliaService.php @@ -32,7 +32,6 @@ public function getClient() { if (!$this->client) { if (!$this->adminApiKey) { - throw new Exception('No adminApiKey configured for '. self::class); }