1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-03-26 21:12:59 +02:00

All: Dropbox: Handle various error conditions

This commit is contained in:
Laurent Cozic 2018-03-27 00:05:39 +01:00
parent 3c2281dbf9
commit 6e994fd8b9
7 changed files with 60 additions and 33 deletions

View File

@ -92,7 +92,7 @@ class Command extends BaseCommand {
}
const response = await api.execAuthToken(authCode);
Setting.setValue('sync.' + this.syncTargetId_ + '.auth', JSON.stringify(response));
Setting.setValue('sync.' + this.syncTargetId_ + '.auth', response.access_token);
api.setAuthToken(response.access_token);
return true;
}

View File

@ -37,7 +37,7 @@ class DropboxLoginScreenComponent extends React.Component {
const api = await this.dropboxApi();
try {
const response = await api.execAuthToken(this.state.authCode);
Setting.setValue('sync.' + this.syncTargetId() + '.auth', JSON.stringify(response));
Setting.setValue('sync.' + this.syncTargetId() + '.auth', response.access_token);
api.setAuthToken(response.access_token);
bridge().showInfoMessageBox(_('The application has been authorised!'));
this.props.dispatch({ type: 'NAV_BACK' });

View File

@ -370,7 +370,7 @@ class NoteTextComponent extends React.Component {
webviewReady: true,
});
if (Setting.value('env') === 'dev') this.webview_.openDevTools();
// if (Setting.value('env') === 'dev') this.webview_.openDevTools();
}
webview_ref(element) {

View File

@ -3,6 +3,7 @@ const { shim } = require('lib/shim.js');
const JoplinError = require('lib/JoplinError');
const URL = require('url-parse');
const { time } = require('lib/time-utils');
const EventDispatcher = require('lib/EventDispatcher');
class DropboxApi {
@ -10,6 +11,7 @@ class DropboxApi {
this.logger_ = new Logger();
this.options_ = options;
this.authToken_ = null;
this.dispatcher_ = new EventDispatcher();
}
clientId() {
@ -32,8 +34,13 @@ class DropboxApi {
return this.authToken_; // Without the "Bearer " prefix
}
on(eventName, callback) {
return this.dispatcher_.on(eventName, callback);
}
setAuthToken(v) {
this.authToken_ = v;
this.dispatcher_.dispatch('authRefreshed', this.authToken());
}
loginUrl() {
@ -90,6 +97,12 @@ class DropboxApi {
return JSON.parse(responseText);
}
isTokenError(status, responseText) {
if (status === 401) return true;
if (responseText.indexOf('OAuth 2 access token is malformed') >= 0) return true;
return false;
}
async exec(method, path = '', body = null, headers = null, options = null) {
if (headers === null) headers = {};
if (options === null) options = {};
@ -124,8 +137,7 @@ class DropboxApi {
// console.info(this.requestToCurl_(url, fetchOptions));
const now = Date.now();
// console.info(now + ': ' + method + ' ' + url);
// console.info(method + ' ' + url);
if (options.source == 'file' && (method == 'POST' || method == 'PUT')) {
response = await shim.uploadBlob(url, fetchOptions);
@ -137,7 +149,7 @@ class DropboxApi {
const responseText = await response.text();
// console.info(now + ': Response: ' + responseText);
// console.info('Response: ' + responseText);
let responseJson_ = null;
const loadResponseJson = () => {
@ -162,10 +174,17 @@ class DropboxApi {
// 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.
const shortResponseText = (responseText + '').substr(0, 1024);
return new JoplinError(method + ' ' + path + ': ' + message + ' (' + response.status + '): ' + shortResponseText, code);
const error = new JoplinError(method + ' ' + path + ': ' + message + ' (' + response.status + '): ' + shortResponseText, code);
error.httpStatus = response.status;
return error;
}
if (!response.ok) {
const json = loadResponseJson();
if (this.isTokenError(response.status, responseText)) {
this.setAuthToken(null);
}
// When using fetchBlob we only get a string (not xml or json) back
if (options.target === 'file') throw newError('fetchBlob error');

View File

@ -32,4 +32,4 @@ class EventDispatcher {
}
module.exports = { EventDispatcher };
module.exports = EventDispatcher;

View File

@ -40,10 +40,6 @@ class SyncTargetDropbox extends BaseSyncTarget {
return fileApi.driver().api();
}
syncTargetId() {
return SyncTargetDropbox.id();
}
async initFileApi() {
const params = parameters().dropbox;
@ -52,15 +48,17 @@ class SyncTargetDropbox extends BaseSyncTarget {
secret: params.secret,
});
const authJson = Setting.value('sync.' + SyncTargetDropbox.id() + '.auth');
if (authJson) {
const auth = JSON.parse(authJson);
api.setAuthToken(auth.access_token);
}
api.on('authRefreshed', (auth) => {
this.logger().info('Saving updated OneDrive auth.');
Setting.setValue('sync.' + SyncTargetDropbox.id() + '.auth', auth ? auth : null);
});
const authToken = Setting.value('sync.' + SyncTargetDropbox.id() + '.auth');
api.setAuthToken(authToken);
const appDir = '';
const fileApi = new FileApi(appDir, new FileApiDriverDropbox(api));
fileApi.setSyncTargetId(this.syncTargetId());
fileApi.setSyncTargetId(SyncTargetDropbox.id());
fileApi.setLogger(this.logger());
return fileApi;
}

View File

@ -66,22 +66,32 @@ class FileApiDriverDropbox {
const context = options ? options.context : null;
let cursor = context ? context.cursor : null;
const urlPath = cursor ? 'files/list_folder/continue' : 'files/list_folder';
const body = cursor ? { cursor: cursor } : { path: this.makePath_(path), include_deleted: true };
const response = await this.api().exec('POST', urlPath, body);
const output = {
items: this.metadataToStats_(response.entries),
hasMore: response.has_more,
context: { cursor: response.cursor },
while (true) {
const urlPath = cursor ? 'files/list_folder/continue' : 'files/list_folder';
const body = cursor ? { cursor: cursor } : { path: this.makePath_(path), include_deleted: true };
try {
const response = await this.api().exec('POST', urlPath, body);
const output = {
items: this.metadataToStats_(response.entries),
hasMore: response.has_more,
context: { cursor: response.cursor },
}
return output;
} catch (error) {
// If there's an error related to an invalid cursor, clear the cursor and retry.
if (cursor) {
if (error.httpStatus === 400 || error.code.indexOf('reset') >= 0) {
// console.info('Clearing cursor and retrying', error);
cursor = null;
continue;
}
}
throw error;
}
}
return output;
// TODO: handle error - reset cursor
}
async list(path, options) {