From 050553ebe26a5e1632b62775b74a33bbf401cca5 Mon Sep 17 00:00:00 2001 From: Itamar Shapira Bar-Lev Date: Tue, 24 Apr 2018 16:36:51 +0300 Subject: [PATCH] Suppress exceptions on viewMultiple in case some of the rows are visible --- .../CacheDecoratedDataProvider.php | 23 ++++++++++++------- .../DataProvider/DataProviderEntity.php | 23 ++++++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php b/src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php index 99ac5893..3fd9c719 100644 --- a/src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php +++ b/src/Plugin/resource/DataProvider/CacheDecoratedDataProvider.php @@ -193,22 +193,29 @@ public function view($identifier) { /** * {@inheritdoc} + * + * @throws \Exception + * In case there's no content to show, and some exceptions where thrown + * from view(), then the last exception will be thrown. */ public function viewMultiple(array $identifiers) { - $return = array(); - // If no IDs were requested, we should not throw an exception in case an - // entity is un-accessible by the user. + $rows = array(); + foreach ($identifiers as $identifier) { try { - $row = $this->view($identifier); + $rows[] = $this->view($identifier); } - catch (InaccessibleRecordException $e) { - $row = NULL; + catch (\Exception $e) { + // Ignore the exceptions in case some content is visible, to allow + // partial success. } - $return[] = $row; + } + if (!$rows && isset($e)) { + // Re-through the last exception in case no content is visible. + throw $e; } - return array_values(array_filter($return)); + return $rows; } /** diff --git a/src/Plugin/resource/DataProvider/DataProviderEntity.php b/src/Plugin/resource/DataProvider/DataProviderEntity.php index 8d61bd91..1c255969 100644 --- a/src/Plugin/resource/DataProvider/DataProviderEntity.php +++ b/src/Plugin/resource/DataProvider/DataProviderEntity.php @@ -269,22 +269,29 @@ public function view($identifier) { /** * {@inheritdoc} + * + * @throws \Exception + * In case there's no content to show, and some exceptions where thrown + * from view(), then the last exception will be thrown. */ public function viewMultiple(array $identifiers) { - $return = array(); - // If no IDs were requested, we should not throw an exception in case an - // entity is un-accessible by the user. + $rows = array(); + foreach ($identifiers as $identifier) { try { - $row = $this->view($identifier); + $rows[] = $this->view($identifier); } - catch (InaccessibleRecordException $e) { - $row = NULL; + catch (\Exception $e) { + // Ignore the exceptions in case some content is visible, to allow + // partial success. } - $return[] = $row; + } + if (!$rows && isset($e)) { + // Re-through the last exception in case no content is visible. + throw $e; } - return array_values(array_filter($return)); + return $rows; } /**