add error handling

This commit is contained in:
Daniel Waxweiler 2023-05-07 21:48:33 +02:00
parent 091317a532
commit d79a0cf9a6
9 changed files with 111 additions and 29 deletions

View File

@ -10,6 +10,8 @@
* License: <wordpress-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';

View File

@ -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;
}

View File

@ -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'];
}

View File

@ -0,0 +1,17 @@
<?php
namespace MobilizonConnector;
// Exit if this file is called directly.
if (!defined('ABSPATH')) {
exit;
}
class GeneralException extends \Exception {
public function __construct($message, $code = 0, Throwable $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace MobilizonConnector;
// Exit if this file is called directly.
if (!defined('ABSPATH')) {
exit;
}
class GroupNotFoundException extends \Exception {
public function __construct($message, $code = 0, Throwable $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}

View File

@ -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']));
}
}
}
}

View File

@ -0,0 +1,9 @@
<?php
// Exit if this file is called directly.
if (!defined('ABSPATH')) {
exit;
}
?>
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list-php">
<?php esc_html_e('The group could not be found!', 'connector-mobilizon'); ?>
</div>

View File

@ -0,0 +1,9 @@
<?php
// Exit if this file is called directly.
if (!defined('ABSPATH')) {
exit;
}
?>
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list-php">
<?php esc_html_e('The events could not be loaded!', 'connector-mobilizon'); ?>
</div>

View File

@ -5,10 +5,8 @@ if (!defined('ABSPATH')) {
}
?>
<div class="<?php echo esc_attr($classNamePrefix); ?>_events-list-php">
<div class="general-error" style="display: none;"><?php esc_html_e('The events could not be loaded!', 'connector-mobilizon'); ?></div>
<div class="group-not-found" style="display: none;"><?php esc_html_e('The group could not be found!', 'connector-mobilizon'); ?></div>
<ul>
<?php foreach($data['data']['events']['elements'] as $event) { ?>
<?php foreach($events as $event) { ?>
<li>
<a href="<?php echo esc_attr($event['url']); ?>"><?php echo esc_html_e($event['title']); ?></a>
<br>