]> git.codecow.com Git - libnemo.git/commitdiff
Refactor vault host code error handling.
authorChris Duncan <chris@codecow.com>
Thu, 6 Aug 2026 05:47:57 +0000 (22:47 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 6 Aug 2026 05:47:57 +0000 (22:47 -0700)
src/lib/vault/index.ts

index 5d19bd4e31ea96e131ee8d3d8c5143469f25953b..943a743cb8a083275aeb7644f6ccacc37952528c 100644 (file)
@@ -46,6 +46,7 @@ export class Vault {
                        this.#worker = new Worker(this.#url, { type: 'module' })
                        this.#worker.addEventListener('message', this.#report)
                        this.#worker.addEventListener('error', this.terminate)
+                       this.#worker.addEventListener('messageerror', this.terminate)
                }
                NODE: {
                        this.#worker = new NodeWorker(vaultWorker, {
@@ -55,6 +56,8 @@ export class Vault {
                        })
                        this.#url = dec.toString(this.#worker.threadId)
                        this.#worker.on('message', listener)
+                       this.#worker.on('error', this.terminate)
+                       this.#worker.on('messageerror', this.terminate)
                }
        }
 
@@ -121,7 +124,7 @@ export class Vault {
                const { taskUrl, taskId, error, isLocked } = results
                if (taskUrl === this.#url) {
                        if (typeof taskId !== 'string') {
-                               throw new Error('Vault worker job invalid ID', { cause: typeof taskId })
+                               return this.terminate()
                        }
                        if (typeof isLocked === 'boolean' && this.#isLocked !== isLocked) {
                                this.#isLocked = isLocked
@@ -129,11 +132,13 @@ export class Vault {
                        }
                        if (taskId === 'autolock') return
                        const task = this.#tasks.get(taskId)
-                       this.#tasks.delete(taskId)
                        if (task == null) {
-                               throw new Error(`Vault worker returned results without an associated task for ID ${taskId}`)
+                               console.warn(`Vault worker returned results without an associated task for ID ${taskId}`)
+                               return
                        }
-                       const { resolve, reject } = task
+                       const { resolve, reject, timeout } = task
+                       this.#tasks.delete(taskId)
+                       clearTimeout(timeout)
                        if (error != null) {
                                reject(new Error(String(error)))
                        } else {