Fix: handle 401 error

This commit is contained in:
eight 2019-09-30 16:03:23 +08:00
parent 61bdb20203
commit 1d18a06e91

View File

@ -1,4 +1,4 @@
/* global dbToCloud styleManager chromeLocal prefs tokenManager loadScript */ /* global dbToCloud styleManager chromeLocal prefs tokenManager */
/* exported sync */ /* exported sync */
'use strict'; 'use strict';
@ -49,7 +49,7 @@ const sync = (() => {
chrome.alarms.onAlarm.addListener(info => { chrome.alarms.onAlarm.addListener(info => {
if (info.name === 'syncNow') { if (info.name === 'syncNow') {
ctrl.syncNow().catch(console.error); syncNow().catch(console.error);
} }
}); });
@ -58,15 +58,32 @@ const sync = (() => {
stop, stop,
put: ctrl.put, put: ctrl.put,
delete: ctrl.delete, delete: ctrl.delete,
syncNow: ctrl.syncNow syncNow
}; };
function syncNow() {
return ctrl.syncNow()
.catch(err => {
if (err.code === 401) {
return tokenManager.revokeToken(currentDrive.name)
.then(() => {
throw err;
});
}
throw err;
});
}
function start(name) { function start(name) {
return (currentDrive ? stop() : Promise.resolve()) return (currentDrive ? stop() : Promise.resolve())
.then(() => { .then(() => {
currentDrive = getDrive(name); currentDrive = getDrive(name);
ctrl.use(currentDrive); ctrl.use(currentDrive);
return ctrl.start(); return ctrl.start()
.catch(err => {
console.log(err.message);
throw err;
});
}) })
.then(() => { .then(() => {
chrome.alarms.create('syncNow', {periodInMinutes: 30}); chrome.alarms.create('syncNow', {periodInMinutes: 30});
@ -76,10 +93,7 @@ const sync = (() => {
function getDrive(name) { function getDrive(name) {
if (name === 'dropbox') { if (name === 'dropbox') {
return dbToCloud.drive.dropbox({ return dbToCloud.drive.dropbox({
getAccessToken: dbx => tokenManager.getToken(name, dbx), getAccessToken: () => tokenManager.getToken(name)
getDropbox: () => loadScript('/vendor/dropbox/dropbox-sdk.js')
.then(() => Dropbox.Dropbox), // eslint-disable-line no-undef
clientId: tokenManager.getClientId('dropbox')
}); });
} }