1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Improved Nextcloud API error handling

This commit is contained in:
Laurent Cozic 2019-12-18 15:32:19 +00:00
parent ce7671151c
commit 888a9ddaf4
3 changed files with 33 additions and 21 deletions

View File

@ -133,7 +133,11 @@ export default class JoplinServerApi {
const loadResponseJson = async () => { const loadResponseJson = async () => {
if (!responseText) return null; if (!responseText) return null;
if (responseJson_) return responseJson_; 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) => { const newError = (message:string, code:number = 0) => {
@ -145,11 +149,13 @@ export default class JoplinServerApi {
try { try {
json = await loadResponseJson(); json = await loadResponseJson();
} catch (error) { } 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}` : ''; 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(); const output = await loadResponseJson();

View File

@ -50,18 +50,22 @@ class SyncTargetNextcloud extends BaseSyncTarget {
return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType')); return new Synchronizer(this.db(), await this.fileApi(), Setting.value('appType'));
} }
async appApi() { async appApi(settings = null) {
if (!this.appApi_) { const useCache = !settings;
this.appApi_ = new JoplinServerApi({
baseUrl: () => JoplinServerApi.baseUrlFromNextcloudWebDavUrl(Setting.value('sync.5.path')),
username: () => Setting.value('sync.5.username'),
password: () => Setting.value('sync.5.password'),
});
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;
} }
} }

View File

@ -59,7 +59,7 @@ shared.checkNextcloudApp = async function(comp, settings) {
comp.setState({ checkNextcloudAppResult: 'checking' }); comp.setState({ checkNextcloudAppResult: 'checking' });
let result = null; let result = null;
const appApi = await reg.syncTargetNextcloud().appApi(); const appApi = await reg.syncTargetNextcloud().appApi(settings);
try { try {
result = await appApi.setupSyncTarget(settings['sync.5.path']); result = await appApi.setupSyncTarget(settings['sync.5.path']);
@ -79,14 +79,16 @@ shared.checkNextcloudApp = async function(comp, settings) {
}; };
shared.updateSettingValue = function(comp, key, value) { shared.updateSettingValue = function(comp, key, value) {
const settings = Object.assign({}, comp.state.settings); comp.setState(state => {
const changedSettingKeys = comp.state.changedSettingKeys.slice(); const settings = Object.assign({}, state.settings);
settings[key] = Setting.formatValue(key, value); const changedSettingKeys = state.changedSettingKeys.slice();
if (changedSettingKeys.indexOf(key) < 0) changedSettingKeys.push(key); settings[key] = Setting.formatValue(key, value);
if (changedSettingKeys.indexOf(key) < 0) changedSettingKeys.push(key);
comp.setState({ return {
settings: settings, settings: settings,
changedSettingKeys: changedSettingKeys, changedSettingKeys: changedSettingKeys,
};
}); });
}; };