diff --git a/ReactNativeClient/lib/JoplinServerApi.ts b/ReactNativeClient/lib/JoplinServerApi.ts index cb244a863..5c3c077aa 100644 --- a/ReactNativeClient/lib/JoplinServerApi.ts +++ b/ReactNativeClient/lib/JoplinServerApi.ts @@ -133,7 +133,11 @@ export default class JoplinServerApi { const loadResponseJson = async () => { if (!responseText) return null; if (responseJson_) return responseJson_; - return JSON.parse(responseText); + try { + return JSON.parse(responseText); + } catch (error) { + throw new Error(`Cannot parse JSON: ${responseText.substr(0, 8192)}`); + } }; const newError = (message:string, code:number = 0) => { @@ -145,11 +149,13 @@ export default class JoplinServerApi { try { json = await loadResponseJson(); } catch (error) { - throw newError(`Unknown error: ${responseText.substr(0, 4096)}`, response.status); + throw newError(`Unknown error: ${responseText.substr(0, 8192)}`, response.status); } const trace = json.stacktrace ? `\n${json.stacktrace}` : ''; - throw newError(json.error + trace, response.status); + let message = json.error; + if (!message) message = responseText.substr(0, 8192); + throw newError(message + trace, response.status); } const output = await loadResponseJson(); diff --git a/ReactNativeClient/lib/SyncTargetNextcloud.js b/ReactNativeClient/lib/SyncTargetNextcloud.js index 2e01b4741..091261e9b 100644 --- a/ReactNativeClient/lib/SyncTargetNextcloud.js +++ b/ReactNativeClient/lib/SyncTargetNextcloud.js @@ -50,18 +50,22 @@ class SyncTargetNextcloud extends BaseSyncTarget { return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType')); } - async appApi() { - if (!this.appApi_) { - this.appApi_ = new JoplinServerApi({ - baseUrl: () => JoplinServerApi.baseUrlFromNextcloudWebDavUrl(Setting.value('sync.5.path')), - username: () => Setting.value('sync.5.username'), - password: () => Setting.value('sync.5.password'), - }); + async appApi(settings = null) { + const useCache = !settings; - this.appApi_.setLogger(this.logger()); - } + if (this.appApi_ && useCache) return this.appApi_; - return this.appApi_; + const appApi = new JoplinServerApi({ + baseUrl: () => JoplinServerApi.baseUrlFromNextcloudWebDavUrl(settings ? settings['sync.5.path'] : Setting.value('sync.5.path')), + username: () => settings ? settings['sync.5.username'] : Setting.value('sync.5.username'), + password: () => settings ? settings['sync.5.password'] : Setting.value('sync.5.password'), + }); + + appApi.setLogger(this.logger()); + + if (useCache) this.appApi_ = appApi; + + return appApi; } } diff --git a/ReactNativeClient/lib/components/shared/config-shared.js b/ReactNativeClient/lib/components/shared/config-shared.js index 15f7cbc17..a1fd856a2 100644 --- a/ReactNativeClient/lib/components/shared/config-shared.js +++ b/ReactNativeClient/lib/components/shared/config-shared.js @@ -59,7 +59,7 @@ shared.checkNextcloudApp = async function(comp, settings) { comp.setState({ checkNextcloudAppResult: 'checking' }); let result = null; - const appApi = await reg.syncTargetNextcloud().appApi(); + const appApi = await reg.syncTargetNextcloud().appApi(settings); try { result = await appApi.setupSyncTarget(settings['sync.5.path']); @@ -79,14 +79,16 @@ shared.checkNextcloudApp = async function(comp, settings) { }; shared.updateSettingValue = function(comp, key, value) { - const settings = Object.assign({}, comp.state.settings); - const changedSettingKeys = comp.state.changedSettingKeys.slice(); - settings[key] = Setting.formatValue(key, value); - if (changedSettingKeys.indexOf(key) < 0) changedSettingKeys.push(key); + comp.setState(state => { + const settings = Object.assign({}, state.settings); + const changedSettingKeys = state.changedSettingKeys.slice(); + settings[key] = Setting.formatValue(key, value); + if (changedSettingKeys.indexOf(key) < 0) changedSettingKeys.push(key); - comp.setState({ - settings: settings, - changedSettingKeys: changedSettingKeys, + return { + settings: settings, + changedSettingKeys: changedSettingKeys, + }; }); };