1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-24 23:26:50 +02:00

Desktop: Improved Joplin Server error handling

This commit is contained in:
Laurent Cozic
2021-06-08 01:34:33 +02:00
parent de7579a14e
commit 95d7ccccea
4 changed files with 34 additions and 14 deletions

View File

@ -766,9 +766,10 @@ export default class BaseApplication {
} }
if (Setting.value('env') === Env.Dev) { if (Setting.value('env') === Env.Dev) {
// Setting.setValue('sync.10.path', 'https://api.joplincloud.com'); Setting.setValue('sync.10.path', 'https://api.joplincloud.com');
Setting.setValue('sync.10.path', 'http://api-joplincloud.local:22300'); Setting.setValue('sync.10.userContentPath', 'https://joplinusercontent.com');
Setting.setValue('sync.10.userContentPath', 'http://joplinusercontent.local:22300'); // Setting.setValue('sync.10.path', 'http://api-joplincloud.local:22300');
// Setting.setValue('sync.10.userContentPath', 'http://joplinusercontent.local:22300');
} }
// For now always disable fuzzy search due to performance issues: // For now always disable fuzzy search due to performance issues:

View File

@ -48,7 +48,7 @@ export default class JoplinServerApi {
this.options_ = options; this.options_ = options;
if (options.env === Env.Dev) { if (options.env === Env.Dev) {
this.debugRequests_ = true; // this.debugRequests_ = true;
} }
} }
@ -175,13 +175,16 @@ export default class JoplinServerApi {
logger.debug('Response', Date.now() - startTime, options.responseFormat, responseText); logger.debug('Response', Date.now() - startTime, options.responseFormat, responseText);
} }
const shortResponseText = () => {
return (`${responseText}`).substr(0, 1024);
};
// Creates an error object with as much data as possible as it will appear in the log, which will make debugging easier // Creates an error object with as much data as possible as it will appear in the log, which will make debugging easier
const newError = (message: string, code: number = 0) => { const newError = (message: string, code: number = 0) => {
// Gives a shorter response for error messages. Useful for cases where a full HTML page is accidentally loaded instead of // Gives a shorter response for error messages. Useful for cases where a full HTML page is accidentally loaded instead of
// JSON. That way the error message will still show there's a problem but without filling up the log or screen. // JSON. That way the error message will still show there's a problem but without filling up the log or screen.
const shortResponseText = (`${responseText}`).substr(0, 1024);
// return new JoplinError(`${method} ${path}: ${message} (${code}): ${shortResponseText}`, code); // return new JoplinError(`${method} ${path}: ${message} (${code}): ${shortResponseText}`, code);
return new JoplinError(message, code, `${method} ${path}: ${message} (${code}): ${shortResponseText}`); return new JoplinError(message, code, `${method} ${path}: ${message} (${code}): ${shortResponseText()}`);
}; };
let responseJson_: any = null; let responseJson_: any = null;
@ -207,7 +210,21 @@ export default class JoplinServerApi {
throw newError(`${json.error}`, json.code ? json.code : response.status); throw newError(`${json.error}`, json.code ? json.code : response.status);
} }
throw newError('Unknown error', response.status); // "Unknown error" means it probably wasn't generated by the
// application but for example by the Nginx or Apache reverse
// proxy. So in that case we attach the response content to the
// error message so that it shows up in logs. It might be for
// example an error returned by the Nginx or Apache reverse
// proxy. For example:
//
// <html>
// <head><title>413 Request Entity Too Large</title></head>
// <body>
// <center><h1>413 Request Entity Too Large</h1></center>
// <hr><center>nginx/1.18.0 (Ubuntu)</center>
// </body>
// </html>
throw newError(`Unknown error: ${shortResponseText()}`, response.status);
} }
if (options.responseFormat === 'text') return responseText; if (options.responseFormat === 'text') return responseText;

View File

@ -41,11 +41,11 @@ export default class SyncTargetJoplinCloud extends BaseSyncTarget {
public static async checkConfig(options: FileApiOptions) { public static async checkConfig(options: FileApiOptions) {
return SyncTargetJoplinServer.checkConfig({ return SyncTargetJoplinServer.checkConfig({
...options, ...options,
}); }, SyncTargetJoplinCloud.id());
} }
protected async initFileApi() { protected async initFileApi() {
return initFileApi(this.logger(), { return initFileApi(SyncTargetJoplinCloud.id(), this.logger(), {
path: () => Setting.value('sync.10.path'), path: () => Setting.value('sync.10.path'),
userContentPath: () => Setting.value('sync.10.userContentPath'), userContentPath: () => Setting.value('sync.10.userContentPath'),
username: () => Setting.value('sync.10.username'), username: () => Setting.value('sync.10.username'),

View File

@ -31,8 +31,8 @@ export async function newFileApi(id: number, options: FileApiOptions) {
return fileApi; return fileApi;
} }
export async function initFileApi(logger: Logger, options: FileApiOptions) { export async function initFileApi(syncTargetId: number, logger: Logger, options: FileApiOptions) {
const fileApi = await newFileApi(SyncTargetJoplinServer.id(), options); const fileApi = await newFileApi(syncTargetId, options);
fileApi.setLogger(logger); fileApi.setLogger(logger);
return fileApi; return fileApi;
} }
@ -63,14 +63,16 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
return super.fileApi(); return super.fileApi();
} }
public static async checkConfig(options: FileApiOptions) { public static async checkConfig(options: FileApiOptions, syncTargetId: number = null) {
const output = { const output = {
ok: false, ok: false,
errorMessage: '', errorMessage: '',
}; };
syncTargetId = syncTargetId === null ? SyncTargetJoplinServer.id() : syncTargetId;
try { try {
const fileApi = await newFileApi(SyncTargetJoplinServer.id(), options); const fileApi = await newFileApi(syncTargetId, options);
fileApi.requestRepeatCount_ = 0; fileApi.requestRepeatCount_ = 0;
await fileApi.put('testing.txt', 'testing'); await fileApi.put('testing.txt', 'testing');
@ -87,7 +89,7 @@ export default class SyncTargetJoplinServer extends BaseSyncTarget {
} }
protected async initFileApi() { protected async initFileApi() {
return initFileApi(this.logger(), { return initFileApi(SyncTargetJoplinServer.id(), this.logger(), {
path: () => Setting.value('sync.9.path'), path: () => Setting.value('sync.9.path'),
userContentPath: () => Setting.value('sync.9.userContentPath'), userContentPath: () => Setting.value('sync.9.userContentPath'),
username: () => Setting.value('sync.9.username'), username: () => Setting.value('sync.9.username'),