fix: crash if vfs is not loadable

Massimo Melina committed Dec 14, 2021 at 23:58 UTC 8a1fda563d0cb2963ee4f3ff439aef0f0c18aad7
2 files changed +12 -7
dev-notes.md
-1
@@ -9,7 +9,6 @@
9
10 # To do
11 - vfs: rename file in source folder
12 -- fix: crash if vfs is not loadable
12 - vfs: serve an html for a folder?
13 - "default" property for vfsNode?
14 - sticky breadcrumbs
src/vfs.ts
+12 -6
@@ -27,20 +27,26 @@ export class Vfs {
27
28 async load(path: string, watchFile:boolean=true) {
29 console.debug('loading vfs')
30 - this.watcher?.close()
31 - const data = await fs.readFile(path, 'utf8')
32 - this.root = yaml.parse(data)
30 + try {
31 + const data = await fs.readFile(path, 'utf8')
32 + this.root = yaml.parse(data)
33 + }
34 + catch(e){
35 + return
36 + }
37 if (!this.root)
34 - throw `Couldn't load ${path}`
38 + throw `Load failed for ${path}`
39 this.root.type = VfsNodeType.root
40 + this.watcher?.close()
41 + this.watcher = undefined
42 recur(this.root)
43 if (!watchFile) return
44 let doing = false
45 this.watcher = watch(path, async () => {
46 if (doing) return
47 doing = true
42 - await this.load(path)
43 - doing = false
48 + try { await this.load(path).catch() }
49 + finally { doing = false }
50 })
51
52 function recur(node:VfsNode) {