Skip to content

Commit c8e3b18

Browse files
author
Bardoe Besselaar
committed
[Enhancement]: Graceful offline support #590
When disconnected from the internet and Obsidian git still attempts to pull changes it throws some panicky error messages. Check the connectivity to githubstatus before attempting to pull/push changes
1 parent 00c9786 commit c8e3b18

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/main.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,36 @@ export default class ObsidianGit extends Plugin {
740740
return Platform.isDesktopApp;
741741
}
742742

743+
async apiEndpointError() {
744+
try {
745+
// none, minor, major, or critical
746+
return await fetch('https://www.githubstatus.com/api/v2/status.json')
747+
.then((response) => response.json())
748+
.then(({ status: { indicator } }) => indicator)
749+
.catch((error) => {
750+
console.warn('githubStatusError', error);
751+
return 'disconnected';
752+
});
753+
} catch (error) {
754+
console.warn('githubStatusError', error);
755+
return 'disconnected';
756+
};
757+
}
758+
759+
async isConnectionEstablished() {
760+
switch(await this.apiEndpointError()) {
761+
case "disconnected":
762+
new Notice('No Connectivity')
763+
return false;
764+
case "critical":
765+
new Notice('Github API Server Outage')
766+
case "major":
767+
console.warn('Github API Major Server Outage')
768+
default:
769+
return true;
770+
}
771+
}
772+
743773
async init(): Promise<void> {
744774
this.showNotices();
745775

@@ -926,6 +956,7 @@ export default class ObsidianGit extends Plugin {
926956
///Used for command
927957
async pullChangesFromRemote(): Promise<void> {
928958
if (!(await this.isAllInitialized())) return;
959+
if (!(await this.isConnectionEstablished())) return;
929960

930961
const filesUpdated = await this.pull();
931962
this.setUpAutoBackup();
@@ -956,10 +987,13 @@ export default class ObsidianGit extends Plugin {
956987
): Promise<void> {
957988
if (!(await this.isAllInitialized())) return;
958989

990+
const isConnected = await this.isConnectionEstablished();
991+
959992
if (
960993
this.settings.syncMethod == "reset" &&
961994
this.settings.pullBeforePush
962995
) {
996+
if (!isConnected) return;
963997
await this.pull();
964998
}
965999

@@ -975,6 +1009,7 @@ export default class ObsidianGit extends Plugin {
9751009
if (!this.settings.disablePush) {
9761010
// Prevent plugin to pull/push at every call of createBackup. Only if unpushed commits are present
9771011
if (await this.gitManager.canPush()) {
1012+
if (!isConnected) return;
9781013
if (
9791014
this.settings.syncMethod != "reset" &&
9801015
this.settings.pullBeforePush
@@ -1166,9 +1201,9 @@ export default class ObsidianGit extends Plugin {
11661201

11671202
async push(): Promise<boolean> {
11681203
if (!(await this.isAllInitialized())) return false;
1169-
if (!(await this.remotesAreSet())) {
1170-
return false;
1171-
}
1204+
if (!(await this.remotesAreSet())) return false;
1205+
if (!(await this.isConnectionEstablished())) return false;
1206+
11721207
const hadConflict = this.localStorage.getConflict() === "true";
11731208
if (this.gitManager instanceof SimpleGit)
11741209
await this.mayDeleteConflictFile();

0 commit comments

Comments
 (0)