@@ -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
269280async 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