From d79a0cf9a612813c18a372ea45cfd4a23db4fcdc Mon Sep 17 00:00:00 2001 From: Daniel Waxweiler Date: Sun, 7 May 2023 21:48:33 +0200 Subject: [PATCH] add error handling --- source/connector-mobilizon.php | 2 ++ source/includes/events-list-shortcut.php | 31 ++++++++++++------- source/includes/events-list-widget.php | 29 ++++++++++------- .../includes/exceptions/general-exception.php | 17 ++++++++++ .../exceptions/group-not-found-exception.php | 17 ++++++++++ source/includes/graphql-client.php | 22 +++++++++++-- source/view/events-list-group-not-found.php | 9 ++++++ source/view/events-list-not-loaded.php | 9 ++++++ source/view/events-list.php | 4 +-- 9 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 source/includes/exceptions/general-exception.php create mode 100644 source/includes/exceptions/group-not-found-exception.php create mode 100644 source/view/events-list-group-not-found.php create mode 100644 source/view/events-list-not-loaded.php diff --git a/source/connector-mobilizon.php b/source/connector-mobilizon.php index 6deea60..4a8e2ef 100644 --- a/source/connector-mobilizon.php +++ b/source/connector-mobilizon.php @@ -10,6 +10,8 @@ * License: */ +require_once __DIR__ . '/includes/exceptions/general-exception.php'; +require_once __DIR__ . '/includes/exceptions/group-not-found-exception.php'; require_once __DIR__ . '/includes/constants.php'; require_once __DIR__ . '/includes/settings.php'; require_once __DIR__ . '/includes/date-time-wrapper.php'; diff --git a/source/includes/events-list-shortcut.php b/source/includes/events-list-shortcut.php index 1c3a674..2d0b450 100644 --- a/source/includes/events-list-shortcut.php +++ b/source/includes/events-list-shortcut.php @@ -28,19 +28,26 @@ class EventsListShortcut { $eventsCount = $atts_with_overriden_defaults['events-count']; $groupName = $atts_with_overriden_defaults['group-name']; - if ($groupName) { - $data = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName); - } else { - $data = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); - } - - $classNamePrefix = NAME; - $locale = get_locale(); - $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); - $timeZone = wp_timezone_string(); - ob_start(); - require dirname(__DIR__) . '/view/events-list.php'; + try { + if ($groupName) { + $data = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName); + } else { + $data = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); + } + $events = $data['data']['group']['organizedEvents']['elements']; + + $classNamePrefix = NAME; + $locale = get_locale(); + $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); + $timeZone = wp_timezone_string(); + + require dirname(__DIR__) . '/view/events-list.php'; + } catch (GeneralException $e) { + require dirname(__DIR__) . '/view/events-list-not-loaded.php'; + } catch (GroupNotFoundException $e) { + require dirname(__DIR__) . '/view/events-list-group-not-found.php'; + } $output = ob_get_clean(); return $output; } diff --git a/source/includes/events-list-widget.php b/source/includes/events-list-widget.php index 66a8101..fe38d91 100644 --- a/source/includes/events-list-widget.php +++ b/source/includes/events-list-widget.php @@ -29,19 +29,26 @@ class EventsListWidget extends \WP_Widget { $eventsCount = $options['eventsCount']; $groupName = isset($options['groupName']) ? $options['groupName'] : ''; - if ($groupName) { - $data = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName); - } else { - $data = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); + try { + if ($groupName) { + $data = GraphQlClient::get_upcoming_events_by_group_name($url, (int) $eventsCount, $groupName); + } else { + $data = GraphQlClient::get_upcoming_events($url, (int) $eventsCount); + } + $events = $data['data']['events']['elements']; + + $classNamePrefix = NAME; + $locale = get_locale(); + $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); + $timeZone = wp_timezone_string(); + + require dirname(__DIR__) . '/view/events-list.php'; + } catch (GeneralException $e) { + require dirname(__DIR__) . '/view/events-list-not-loaded.php'; + } catch (GroupNotFoundException $e) { + require dirname(__DIR__) . '/view/events-list-group-not-found.php'; } - $classNamePrefix = NAME; - $locale = get_locale(); - $isShortOffsetNameShown = Settings::isShortOffsetNameShown(); - $timeZone = wp_timezone_string(); - - require dirname(__DIR__) . '/view/events-list.php'; - echo $args['after_widget']; } diff --git a/source/includes/exceptions/general-exception.php b/source/includes/exceptions/general-exception.php new file mode 100644 index 0000000..b37cd09 --- /dev/null +++ b/source/includes/exceptions/general-exception.php @@ -0,0 +1,17 @@ +code}]: {$this->message}\n"; +} +} diff --git a/source/includes/exceptions/group-not-found-exception.php b/source/includes/exceptions/group-not-found-exception.php new file mode 100644 index 0000000..841841d --- /dev/null +++ b/source/includes/exceptions/group-not-found-exception.php @@ -0,0 +1,17 @@ +code}]: {$this->message}\n"; +} +} diff --git a/source/includes/graphql-client.php b/source/includes/graphql-client.php index 23915fd..1848fe1 100644 --- a/source/includes/graphql-client.php +++ b/source/includes/graphql-client.php @@ -62,13 +62,15 @@ final class GraphQlClient { // return Promise.resolve(dataInCache) // } $data = self::query($endpoint, $query, ['limit' => $limit]); - // SessionCache.add(sessionStorage, { url, query, variables: { limit } }, data) + self::checkData($data); + + // SessionCache.add(sessionStorage, { url, query, variables: { limit } }, data) return $data; } public static function get_upcoming_events_by_group_name(string $url, int $limit, string $groupName): array { $query = <<<'GRAPHQL' - query ($afterDatetime: DateTime, $groupName: String, $limit: Int) { + query ($afterDatetime: DateTime, $groupName: String!, $limit: Int) { group(preferredUsername: $groupName) { organizedEvents(afterDatetime: $afterDatetime, limit: $limit) { elements { @@ -100,7 +102,9 @@ final class GraphQlClient { // return Promise.resolve(dataInCache) // } $afterDatetime = date(\DateTime::ISO8601); - $data = self::query($endpoint, $query, ['afterDatetime'=> $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]); + $data = self::query($endpoint, $query, ['afterDatetime' => $afterDatetime, 'groupName' => $groupName, 'limit' => $limit]); + self::checkData($data); + // return request(url, query, { afterDatetime, groupName, limit }).then( // (data) => { // SessionCache.add( @@ -113,4 +117,16 @@ final class GraphQlClient { // ) return $data; } + + private static function checkData($data) { + if (isset($data['errors'])) { + if (count($data['errors']) > 0 && + isset($data['errors'][0]['code']) && + $data['errors'][0]['code'] === 'group_not_found') { + throw new GroupNotFoundException(serialize($data['errors'][0])); + } else { + throw new GeneralException(serialize($data['errors'])); + } + } + } } diff --git a/source/view/events-list-group-not-found.php b/source/view/events-list-group-not-found.php new file mode 100644 index 0000000..069f994 --- /dev/null +++ b/source/view/events-list-group-not-found.php @@ -0,0 +1,9 @@ + +
+ +
diff --git a/source/view/events-list-not-loaded.php b/source/view/events-list-not-loaded.php new file mode 100644 index 0000000..1b79be8 --- /dev/null +++ b/source/view/events-list-not-loaded.php @@ -0,0 +1,9 @@ + +
+ +
diff --git a/source/view/events-list.php b/source/view/events-list.php index 82bb81c..97678f0 100644 --- a/source/view/events-list.php +++ b/source/view/events-list.php @@ -5,10 +5,8 @@ if (!defined('ABSPATH')) { } ?>
- -
    - +