mirror of
https://github.com/dwaxweiler/connector-mobilizon
synced 2025-06-05 21:59:25 +02:00
add error handling
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
@ -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'];
|
||||
}
|
||||
|
||||
|
17
source/includes/exceptions/general-exception.php
Normal file
17
source/includes/exceptions/general-exception.php
Normal 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";
|
||||
}
|
||||
}
|
17
source/includes/exceptions/group-not-found-exception.php
Normal file
17
source/includes/exceptions/group-not-found-exception.php
Normal 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";
|
||||
}
|
||||
}
|
@ -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']));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user