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 () => {
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();

View File

@ -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;
}
}

View File

@ -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,
};
});
};