Add: handle 401 error

This commit is contained in:
eight 2019-09-30 16:20:10 +08:00
parent 1d18a06e91
commit ee6d2b805d

View File

@ -49,7 +49,9 @@ const sync = (() => {
chrome.alarms.onAlarm.addListener(info => { chrome.alarms.onAlarm.addListener(info => {
if (info.name === 'syncNow') { if (info.name === 'syncNow') {
syncNow().catch(console.error); ctrl.syncNow()
.catch(handle401Error)
.catch(console.error);
} }
}); });
@ -58,12 +60,10 @@ const sync = (() => {
stop, stop,
put: ctrl.put, put: ctrl.put,
delete: ctrl.delete, delete: ctrl.delete,
syncNow syncNow: () => ctrl.syncNow().then(handle401Error)
}; };
function syncNow() { function handle401Error(err) {
return ctrl.syncNow()
.catch(err => {
if (err.code === 401) { if (err.code === 401) {
return tokenManager.revokeToken(currentDrive.name) return tokenManager.revokeToken(currentDrive.name)
.then(() => { .then(() => {
@ -71,7 +71,6 @@ const sync = (() => {
}); });
} }
throw err; throw err;
});
} }
function start(name) { function start(name) {
@ -81,10 +80,14 @@ const sync = (() => {
ctrl.use(currentDrive); ctrl.use(currentDrive);
return ctrl.start() return ctrl.start()
.catch(err => { .catch(err => {
console.log(err.message); if (/Authorization page could not be loaded/i.test(err.message)) {
// FIXME: Chrome always fail at the first login so we try again
return ctrl.syncNow();
}
throw err; throw err;
}); });
}) })
.catch(handle401Error)
.then(() => { .then(() => {
chrome.alarms.create('syncNow', {periodInMinutes: 30}); chrome.alarms.create('syncNow', {periodInMinutes: 30});
}); });