1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

All: Improved handling of expired sessions when using Joplin Server

This commit is contained in:
Laurent Cozic 2021-10-25 19:46:45 +01:00
parent ace1118cf1
commit 33249ca05a

View File

@ -130,7 +130,7 @@ export default class JoplinServerApi {
return output.join(' ');
}
public async exec(method: string, path: string = '', query: Record<string, any> = null, body: any = null, headers: any = null, options: ExecOptions = null) {
private async exec_(method: string, path: string = '', query: Record<string, any> = null, body: any = null, headers: any = null, options: ExecOptions = null) {
if (headers === null) headers = {};
if (options === null) options = {};
if (!options.responseFormat) options.responseFormat = ExecOptionsResponseFormat.Json;
@ -258,12 +258,24 @@ export default class JoplinServerApi {
logger.warn(error);
}
if (error.code === 403) {
this.session_ = null;
error.message = 'Session has expired or is invalid, please try again.';
throw error;
}
}
public async exec(method: string, path: string = '', query: Record<string, any> = null, body: any = null, headers: any = null, options: ExecOptions = null) {
for (let i = 0; i < 2; i++) {
try {
const response = await this.exec_(method, path, query, body, headers, options);
return response;
} catch (error) {
if (error.code === 403 && i === 0) {
logger.info('Session expired or invalid - trying to login again', error);
this.session_ = null; // By setting it to null, the service will try to login again
} else {
throw error;
}
}
}
}
}