Skip to content

Commit

Permalink
All: Dropbox: Handle various error conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent22 committed Mar 26, 2018
1 parent 3c2281d commit 6e994fd
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 33 deletions.
2 changes: 1 addition & 1 deletion CliClient/app/command-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion ElectronClient/app/gui/DropboxLoginScreen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down
2 changes: 1 addition & 1 deletion ElectronClient/app/gui/NoteText.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 23 additions & 4 deletions ReactNativeClient/lib/DropboxApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ 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 {

constructor(options) {
this.logger_ = new Logger();
this.options_ = options;
this.authToken_ = null;
this.dispatcher_ = new EventDispatcher();
}

clientId() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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);
Expand All @@ -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 = () => {
Expand All @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ class EventDispatcher {

}

module.exports = { EventDispatcher };
module.exports = EventDispatcher;
18 changes: 8 additions & 10 deletions ReactNativeClient/lib/SyncTargetDropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ class SyncTargetDropbox extends BaseSyncTarget {
return fileApi.driver().api();
}

syncTargetId() {
return SyncTargetDropbox.id();
}

async initFileApi() {
const params = parameters().dropbox;

Expand All @@ -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;
}
Expand Down
40 changes: 25 additions & 15 deletions ReactNativeClient/lib/file-api-driver-dropbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6e994fd

Please sign in to comment.