From 4f3e8803daa65767dada0656c568368d38b47ebc Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Mon, 21 Mar 2022 23:04:56 +0800 Subject: [PATCH] more verbose reason for conn fail --- src/remote.ts | 17 ++++++++++++----- src/remoteForDropbox.ts | 11 ++++++++--- src/remoteForOnedrive.ts | 9 ++++++++- src/remoteForS3.ts | 12 +++++++++++- src/remoteForWebdav.ts | 14 +++++++++++++- src/settings.ts | 24 ++++++++++++++++++++---- 6 files changed, 72 insertions(+), 15 deletions(-) diff --git a/src/remote.ts b/src/remote.ts index 678af64..44dc5b8 100644 --- a/src/remote.ts +++ b/src/remote.ts @@ -271,15 +271,22 @@ export class RemoteClient { } }; - checkConnectivity = async () => { + checkConnectivity = async (callbackFunc?: any) => { if (this.serviceType === "s3") { - return await s3.checkConnectivity(this.s3Client, this.s3Config); + return await s3.checkConnectivity( + this.s3Client, + this.s3Config, + callbackFunc + ); } else if (this.serviceType === "webdav") { - return await webdav.checkConnectivity(this.webdavClient); + return await webdav.checkConnectivity(this.webdavClient, callbackFunc); } else if (this.serviceType === "dropbox") { - return await dropbox.checkConnectivity(this.dropboxClient); + return await dropbox.checkConnectivity(this.dropboxClient, callbackFunc); } else if (this.serviceType === "onedrive") { - return await onedrive.checkConnectivity(this.onedriveClient); + return await onedrive.checkConnectivity( + this.onedriveClient, + callbackFunc + ); } else { throw Error(`not supported service type ${this.serviceType}`); } diff --git a/src/remoteForDropbox.ts b/src/remoteForDropbox.ts index 33154a2..7f8256b 100644 --- a/src/remoteForDropbox.ts +++ b/src/remoteForDropbox.ts @@ -636,7 +636,10 @@ export const deleteFromRemote = async ( } }; -export const checkConnectivity = async (client: WrappedDropboxClient) => { +export const checkConnectivity = async ( + client: WrappedDropboxClient, + callbackFunc?: any +) => { try { const results = await getRemoteMeta(client, "/"); if (results === undefined) { @@ -644,8 +647,10 @@ export const checkConnectivity = async (client: WrappedDropboxClient) => { } return true; } catch (err) { - console.error("dropbox connectivity error:"); - console.error(err); + log.debug(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } return false; } }; diff --git a/src/remoteForOnedrive.ts b/src/remoteForOnedrive.ts index 21fd3f5..ca919e4 100644 --- a/src/remoteForOnedrive.ts +++ b/src/remoteForOnedrive.ts @@ -866,11 +866,18 @@ export const deleteFromRemote = async ( await client.deleteJson(remoteFileName); }; -export const checkConnectivity = async (client: WrappedOnedriveClient) => { +export const checkConnectivity = async ( + client: WrappedOnedriveClient, + callbackFunc?: any +) => { try { const k = await getUserDisplayName(client); return k !== ""; } catch (err) { + log.debug(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } return false; } }; diff --git a/src/remoteForS3.ts b/src/remoteForS3.ts index 6e4f4a6..ba56cbc 100644 --- a/src/remoteForS3.ts +++ b/src/remoteForS3.ts @@ -498,7 +498,8 @@ export const deleteFromRemote = async ( */ export const checkConnectivity = async ( s3Client: S3Client, - s3Config: S3Config + s3Config: S3Config, + callbackFunc?: any ) => { try { const results = await s3Client.send( @@ -509,10 +510,19 @@ export const checkConnectivity = async ( results.$metadata === undefined || results.$metadata.httpStatusCode === undefined ) { + const err = "results or $metadata or httStatusCode is undefined"; + log.debug(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } return false; } return results.$metadata.httpStatusCode === 200; } catch (err) { + log.debug(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } return false; } }; diff --git a/src/remoteForWebdav.ts b/src/remoteForWebdav.ts index 9e948df..341b911 100644 --- a/src/remoteForWebdav.ts +++ b/src/remoteForWebdav.ts @@ -523,15 +523,27 @@ export const deleteFromRemote = async ( } }; -export const checkConnectivity = async (client: WrappedWebdavClient) => { +export const checkConnectivity = async ( + client: WrappedWebdavClient, + callbackFunc?: any +) => { try { await client.init(); const results = await getRemoteMeta(client, "/"); if (results === undefined) { + const err = "results is undefined"; + log.debug(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } return false; } return true; } catch (err) { + log.debug(err); + if (callbackFunc !== undefined) { + callbackFunc(err); + } return false; } }; diff --git a/src/settings.ts b/src/settings.ts index 32f217c..8aa1011 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -826,11 +826,15 @@ export class RemotelySaveSettingTab extends PluginSettingTab { button.onClick(async () => { new Notice(t("settings_checkonnectivity_checking")); const client = new RemoteClient("s3", this.plugin.settings.s3); - const res = await client.checkConnectivity(); + const errors = { msg: "" }; + const res = await client.checkConnectivity((err: any) => { + errors.msg = err; + }); if (res) { new Notice(t("settings_s3_connect_succ")); } else { new Notice(t("settings_s3_connect_fail")); + new Notice(errors.msg); } }); }); @@ -959,11 +963,15 @@ export class RemotelySaveSettingTab extends PluginSettingTab { () => self.plugin.saveSettings() ); - const res = await client.checkConnectivity(); + const errors = { msg: "" }; + const res = await client.checkConnectivity((err: any) => { + errors.msg = `${err}`; + }); if (res) { new Notice(t("settings_dropbox_connect_succ")); } else { new Notice(t("settings_dropbox_connect_fail")); + new Notice(errors.msg); } }); }); @@ -1074,11 +1082,15 @@ export class RemotelySaveSettingTab extends PluginSettingTab { () => self.plugin.saveSettings() ); - const res = await client.checkConnectivity(); + const errors = { msg: "" }; + const res = await client.checkConnectivity((err: any) => { + errors.msg = `${err}`; + }); if (res) { new Notice(t("settings_onedrive_connect_succ")); } else { new Notice(t("settings_onedrive_connect_fail")); + new Notice(errors.msg); } }); }); @@ -1230,7 +1242,10 @@ export class RemotelySaveSettingTab extends PluginSettingTab { this.app.vault.getName(), () => self.plugin.saveSettings() ); - const res = await client.checkConnectivity(); + const errors = { msg: "" }; + const res = await client.checkConnectivity((err: any) => { + errors.msg = `${err}`; + }); if (res) { new Notice(t("settings_webdav_connect_succ")); } else { @@ -1244,6 +1259,7 @@ export class RemotelySaveSettingTab extends PluginSettingTab { corsErrMsg: corsErrMsg, }) ); + new Notice(errors.msg); } }); });