Skip to content

Commit a8807e1

Browse files
committed
fix(options): display errors for adding / removing instance
If there is an error when adding or removing a self-hosted instance, a status message is displayed.
1 parent 3114bfd commit a8807e1

4 files changed

Lines changed: 41 additions & 7 deletions

File tree

‎_locales/en/messages.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
"message": "Permission to access this instance was denied",
8888
"description": "Status message when the host permission request is denied"
8989
},
90+
"optionsInstanceError": {
91+
"message": "Something went wrong. Please try again.",
92+
"description": "Status message when adding or removing a self-hosted instance fails unexpectedly"
93+
},
9094
"optionsReviewUrlPlaceholder": {
9195
"message": "review.example.com (review server URL)",
9296
"description": "Placeholder for the review server URL input of a Gerrit instance"

‎_locales/fr/messages.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
"message": "L'autorisation d'accéder à cette instance a été refusée",
8888
"description": "Message affiché quand la demande d'autorisation d'hôte est refusée"
8989
},
90+
"optionsInstanceError": {
91+
"message": "Une erreur est survenue. Veuillez réessayer.",
92+
"description": "Message affiché quand l'ajout ou la suppression d'une instance auto-hébergée échoue de façon inattendue"
93+
},
9094
"optionsReviewUrlPlaceholder": {
9195
"message": "review.exemple.com (URL du serveur de revue)",
9296
"description": "Texte indicatif du champ d'URL du serveur de revue d'une instance Gerrit"

‎_locales/template.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
"message": "",
8888
"description": ""
8989
},
90+
"optionsInstanceError": {
91+
"message": "",
92+
"description": ""
93+
},
9094
"optionsReviewUrlPlaceholder": {
9195
"message": "",
9296
"description": ""

‎options/options.js‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ async function addInstance() {
232232
instance.mirrorType = document.getElementById("instance-mirror-type").value
233233
}
234234

235+
// critical path: request the permission and persist the instance. Any
236+
// failure here means the instance was not added, so surface it.
235237
try {
236238
// the extension may only call the queried API (the review server for
237239
// a Gerrit instance) once the user grants access to its origin.
@@ -254,11 +256,20 @@ async function addInstance() {
254256

255257
instances.push(instance)
256258
await saveInstances(instances)
257-
document.getElementById("instance-hostname").value = ""
258-
document.getElementById("instance-review-url").value = ""
259+
} catch (error) {
260+
console.error(`Error: ${error}`)
261+
showStatus("optionsInstanceError", "instance-status")
262+
return
263+
}
264+
265+
// the instance is saved; clearing the inputs and refreshing the list is
266+
// display-only, so a failure here must not report the add as failed
267+
document.getElementById("instance-hostname").value = ""
268+
document.getElementById("instance-review-url").value = ""
269+
showStatus("optionsInstanceAdded", "instance-status")
270+
try {
259271
await renderInstances()
260272
await loadTokens()
261-
showStatus("optionsInstanceAdded", "instance-status")
262273
} catch (error) {
263274
console.error(`Error: ${error}`)
264275
}
@@ -267,19 +278,30 @@ async function addInstance() {
267278
// remove a self-hosted instance: drop it from storage, forget its token and
268279
// give back the queried host's permission we no longer need
269280
async function removeInstance(hostname) {
281+
// critical path: drop the instance from storage. A failure here means it
282+
// was not removed, so surface it.
283+
let removed
270284
try {
271285
const instances = await loadInstances()
272-
const removed = instances.find(i => i.hostname === hostname)
273-
286+
removed = instances.find(i => i.hostname === hostname)
274287
await saveInstances(instances.filter(i => i.hostname !== hostname))
288+
} catch (error) {
289+
console.error(`Error: ${error}`)
290+
showStatus("optionsInstanceError", "instance-status")
291+
return
292+
}
293+
294+
// the instance is gone; forgetting its token, releasing the host
295+
// permission and refreshing the list are best-effort cleanup, so a
296+
// failure here must not report the removal as failed
297+
showStatus("optionsInstanceRemoved", "instance-status")
298+
try {
275299
await browser.storage.local.remove(selfHostedTokenKey(hostname))
276300
if (removed) {
277301
await browser.permissions.remove(
278302
{ origins: [`*://${permissionHostname(removed)}/*`] })
279303
}
280-
281304
await renderInstances()
282-
showStatus("optionsInstanceRemoved", "instance-status")
283305
} catch (error) {
284306
console.error(`Error: ${error}`)
285307
}

0 commit comments

Comments
 (0)