Skip to content

Commit 3e25fb1

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 3e25fb1

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/main.ts

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

743+
async apiEndpointError() {
744+
try {
745+
const response = await fetch('https://www.githubstatus.com/api/v2/status.json');
746+
if (!response.ok) {
747+
throw new Error('Network response was not ok');
748+
}
749+
750+
const { status: { indicator } } = await response.json();
751+
return indicator;
752+
} catch (error) {
753+
console.warn('githubStatusError', error);
754+
return 'disconnected';
755+
}
756+
}
757+
758+
async isConnectionEstablished() {
759+
const status = await this.apiEndpointError();
760+
console.debug(`Github API Status: ${status}`)
761+
762+
const errors: Record<string, string> = {
763+
disconnected: 'No Connectivity',
764+
critical: 'Github API Server Outage',
765+
};
766+
if (errors[status]) { new Notice(errors[status]) }
767+
768+
return status !== 'disconnected';
769+
}
770+
743771
async init(): Promise<void> {
744772
this.showNotices();
745773

@@ -926,6 +954,7 @@ export default class ObsidianGit extends Plugin {
926954
///Used for command
927955
async pullChangesFromRemote(): Promise<void> {
928956
if (!(await this.isAllInitialized())) return;
957+
if (!(await this.isConnectionEstablished())) return;
929958

930959
const filesUpdated = await this.pull();
931960
this.setUpAutoBackup();
@@ -956,10 +985,13 @@ export default class ObsidianGit extends Plugin {
956985
): Promise<void> {
957986
if (!(await this.isAllInitialized())) return;
958987

988+
const isConnected = await this.isConnectionEstablished();
989+
959990
if (
960991
this.settings.syncMethod == "reset" &&
961992
this.settings.pullBeforePush
962993
) {
994+
if (!isConnected) return;
963995
await this.pull();
964996
}
965997

@@ -975,6 +1007,7 @@ export default class ObsidianGit extends Plugin {
9751007
if (!this.settings.disablePush) {
9761008
// Prevent plugin to pull/push at every call of createBackup. Only if unpushed commits are present
9771009
if (await this.gitManager.canPush()) {
1010+
if (!isConnected) return;
9781011
if (
9791012
this.settings.syncMethod != "reset" &&
9801013
this.settings.pullBeforePush
@@ -1166,9 +1199,9 @@ export default class ObsidianGit extends Plugin {
11661199

11671200
async push(): Promise<boolean> {
11681201
if (!(await this.isAllInitialized())) return false;
1169-
if (!(await this.remotesAreSet())) {
1170-
return false;
1171-
}
1202+
if (!(await this.remotesAreSet())) return false;
1203+
if (!(await this.isConnectionEstablished())) return false;
1204+
11721205
const hadConflict = this.localStorage.getConflict() === "true";
11731206
if (this.gitManager instanceof SimpleGit)
11741207
await this.mayDeleteConflictFile();

0 commit comments

Comments
 (0)