Skip to content

Commit 8b283e6

Browse files
authored
Handle oauth exceptions gracefully.
1 parent cf18f58 commit 8b283e6

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/XeroSiteConfigExtension.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace FullscreenInteractive\SilverStripeXero;
44

5+
use Exception;
56
use SilverStripe\Core\Environment;
67
use SilverStripe\Forms\DropdownField;
78
use SilverStripe\Forms\FieldList;
9+
use SilverStripe\Forms\LiteralField;
810
use SilverStripe\Forms\ReadonlyField;
911
use SilverStripe\ORM\DataExtension;
1012
use SilverStripe\Security\Permission;
@@ -39,19 +41,27 @@ public function updateCMSFields(FieldList $fields)
3941

4042
if ($this->owner->XeroAccessToken) {
4143
$tenants = [];
42-
$tenantRecords = XeroFactory::singleton()->getTenants($this->owner->XeroAccessToken);
4344

44-
foreach ($tenantRecords as $tenant) {
45-
$tenants[$tenant->tenantId] = $tenant->tenantName;
46-
}
45+
try {
46+
$tenantRecords = XeroFactory::singleton()->getTenants($this->owner->XeroAccessToken);
47+
48+
foreach ($tenantRecords as $tenant) {
49+
$tenants[$tenant->tenantId] = $tenant->tenantName;
50+
}
4751

48-
$fields->addFieldsToTab('Root.Xero', [
49-
DropdownField::create(
50-
'XeroTenantId',
51-
'Xero Tenant',
52-
$tenants
53-
)
54-
]);
52+
$fields->addFieldsToTab('Root.Xero', [
53+
DropdownField::create(
54+
'XeroTenantId',
55+
'Xero Tenant',
56+
$tenants
57+
)
58+
]);
59+
} catch (Exception $e) {
60+
$fields->addFieldsToTab('Root.Xero', LiteralField::create(
61+
'XeroError',
62+
'An error has occured connecting to xero: '. $e->getMessage()
63+
));
64+
}
5565
}
5666
}
5767
}

0 commit comments

Comments
 (0)