Add: show more errors on badge

This commit is contained in:
eight04 2021-02-09 14:44:19 +08:00
parent d33bb76938
commit 75a3752d28
3 changed files with 40 additions and 25 deletions

View File

@ -1530,6 +1530,10 @@
"message": "The value cannot be saved. Try reducing the amount of text.", "message": "The value cannot be saved. Try reducing the amount of text.",
"description": "Displayed when trying to save an excessively big value via storage.sync API" "description": "Displayed when trying to save an excessively big value via storage.sync API"
}, },
"syncError": {
"message": "Sync failed",
"description": "Tooltip for the toolbar icon"
},
"syncErrorRelogin": { "syncErrorRelogin": {
"message": "Sync failed.\nTry to re-login in Stylus options:\nclick 'disconnect' first, then 'connect'.", "message": "Sync failed.\nTry to re-login in Stylus options:\nclick 'disconnect' first, then 'connect'.",
"description": "Tooltip for the toolbar icon" "description": "Tooltip for the toolbar icon"

View File

@ -27,6 +27,7 @@ const syncMan = (() => {
errorMessage: null, errorMessage: null,
login: false, login: false,
}; };
let lastError = null;
let ctrl; let ctrl;
let currentDrive; let currentDrive;
/** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */ /** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */
@ -100,8 +101,10 @@ const syncMan = (() => {
} }
await syncMan.syncNow(); await syncMan.syncNow();
status.errorMessage = null; status.errorMessage = null;
lastError = null;
} catch (err) { } catch (err) {
status.errorMessage = err.message; status.errorMessage = err.message;
lastError = err;
// FIXME: should we move this logic to options.js? // FIXME: should we move this logic to options.js?
if (!fromPref) { if (!fromPref) {
console.error(err); console.error(err);
@ -139,8 +142,10 @@ const syncMan = (() => {
try { try {
await (ctrl.isInit() ? ctrl.syncNow() : ctrl.start()).catch(handle401Error); await (ctrl.isInit() ? ctrl.syncNow() : ctrl.start()).catch(handle401Error);
status.errorMessage = null; status.errorMessage = null;
lastError = null;
} catch (err) { } catch (err) {
status.errorMessage = err.message; status.errorMessage = err.message;
lastError = err;
} }
emitStatusChange(); emitStatusChange();
}, },
@ -188,27 +193,48 @@ const syncMan = (() => {
} }
async function handle401Error(err) { async function handle401Error(err) {
let emit; let authError = false;
if (err.code === 401) { if (err.code === 401) {
await tokenMan.revokeToken(currentDrive.name).catch(console.error); await tokenMan.revokeToken(currentDrive.name).catch(console.error);
emit = true; authError = true;
} else if (/User interaction required|Requires user interaction/i.test(err.message)) { } else if (/User interaction required|Requires user interaction/i.test(err.message)) {
emit = true; authError = true;
} }
if (emit) { if (authError) {
status.login = false; status.login = false;
emitStatusChange(); emitStatusChange();
} }
iconMan.overrideBadge(!emit ? {} : {
text: 'x',
color: '#F00',
title: chrome.i18n.getMessage('syncErrorRelogin'),
});
return Promise.reject(err); return Promise.reject(err);
} }
function emitStatusChange() { function emitStatusChange() {
msg.broadcastExtension({method: 'syncStatusUpdate', status}); msg.broadcastExtension({method: 'syncStatusUpdate', status});
if (status.state !== STATES.connected || !lastError || isNetworkError(lastError)) {
iconMan.overrideBadge({});
} else if (isGrantError(lastError)) {
iconMan.overrideBadge({
text: 'x',
color: '#F00',
title: chrome.i18n.getMessage('syncErrorRelogin'),
});
} else {
iconMan.overrideBadge({
text: 'x',
color: '#F00',
title: chrome.i18n.getMessage('syncError'),
});
}
}
function isNetworkError(err) {
return err.name === 'TypeError' && /networkerror|failed to fetch/i.test(err.message);
}
function isGrantError(err) {
if (err.code === 401) return true;
if (err.code === 400 && /invalid_grant/.test(err.message)) return true;
return false;
} }
function getDrive(name) { function getDrive(name) {

17
package-lock.json generated
View File

@ -645,7 +645,6 @@
"espree": "7.3.0", "espree": "7.3.0",
"esprima": "4.0.1", "esprima": "4.0.1",
"fluent-syntax": "0.13.0", "fluent-syntax": "0.13.0",
"fsevents": "2.2.1",
"glob": "7.1.6", "glob": "7.1.6",
"is-mergeable-object": "1.1.1", "is-mergeable-object": "1.1.1",
"jed": "1.1.1", "jed": "1.1.1",
@ -1855,12 +1854,6 @@
"engines": [ "engines": [
"node >=0.10.0" "node >=0.10.0"
], ],
"dependencies": {
"dtrace-provider": "~0.8",
"moment": "^2.19.3",
"mv": "~2",
"safe-json-stringify": "~1"
},
"bin": { "bin": {
"bunyan": "bin/bunyan" "bunyan": "bin/bunyan"
}, },
@ -2144,7 +2137,6 @@
"dependencies": { "dependencies": {
"anymatch": "~3.1.1", "anymatch": "~3.1.1",
"braces": "~3.0.2", "braces": "~3.0.2",
"fsevents": "~2.1.2",
"glob-parent": "~5.1.0", "glob-parent": "~5.1.0",
"is-binary-path": "~2.1.0", "is-binary-path": "~2.1.0",
"is-glob": "~4.0.1", "is-glob": "~4.0.1",
@ -4559,9 +4551,6 @@
"resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz",
"integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=",
"dev": true, "dev": true,
"dependencies": {
"graceful-fs": "^4.1.6"
},
"optionalDependencies": { "optionalDependencies": {
"graceful-fs": "^4.1.6" "graceful-fs": "^4.1.6"
} }
@ -6995,7 +6984,6 @@
"integrity": "sha512-ssHt0dkljEDaKmTgQ04DQgx2ag6G2gMPxA5hpcsoeTbfDgRf2fC2gNSRc6kISjD7ckCpHwwQvXxuTBK8402fXg==", "integrity": "sha512-ssHt0dkljEDaKmTgQ04DQgx2ag6G2gMPxA5hpcsoeTbfDgRf2fC2gNSRc6kISjD7ckCpHwwQvXxuTBK8402fXg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"encoding": "^0.1.12",
"minipass": "^3.1.0", "minipass": "^3.1.0",
"minipass-pipeline": "^1.2.2", "minipass-pipeline": "^1.2.2",
"minipass-sized": "^1.0.3", "minipass-sized": "^1.0.3",
@ -11284,10 +11272,8 @@
"integrity": "sha512-aWAgTW4MoSJzZPAicljkO1hsi1oKj/RRq/OJQh2PKI2UKL04c2Bs+MBOB+BBABHTXJpf9mCwHN7ANCvYsvY2sg==", "integrity": "sha512-aWAgTW4MoSJzZPAicljkO1hsi1oKj/RRq/OJQh2PKI2UKL04c2Bs+MBOB+BBABHTXJpf9mCwHN7ANCvYsvY2sg==",
"dev": true, "dev": true,
"dependencies": { "dependencies": {
"chokidar": "^3.4.1",
"graceful-fs": "^4.1.2", "graceful-fs": "^4.1.2",
"neo-async": "^2.5.0", "neo-async": "^2.5.0"
"watchpack-chokidar2": "^2.0.0"
}, },
"optionalDependencies": { "optionalDependencies": {
"chokidar": "^3.4.1", "chokidar": "^3.4.1",
@ -11371,7 +11357,6 @@
"anymatch": "^2.0.0", "anymatch": "^2.0.0",
"async-each": "^1.0.1", "async-each": "^1.0.1",
"braces": "^2.3.2", "braces": "^2.3.2",
"fsevents": "^1.2.7",
"glob-parent": "^3.1.0", "glob-parent": "^3.1.0",
"inherits": "^2.0.3", "inherits": "^2.0.3",
"is-binary-path": "^1.0.0", "is-binary-path": "^1.0.0",