Make get_session() readonly and throw an exception if session cannot be found rather...
[ndcode_site.git] / _lib / navbar.jst
index 318c6f1..be95539 100644 (file)
@@ -1,4 +1,5 @@
 let assert = require('assert')
+let jst_server = (await import('@ndcode/jst_server')).default
 let XDate = require('xdate')
 
 let arrays_equal =
@@ -46,20 +47,42 @@ return async (env, head, body, scripts) => {
     copyright = await globals.get_json('copyright')
 
     let navigation = await root.get('navigation')
-
+    if (navigation === undefined)
+      throw new jst_server.Problem(
+        'Navigation error',
+        'Please import the navigation tree into the database.',
+        508
+      )
+
+    // this code is taken from get_navigation.jst and instrumented
     let p = navigation
     component_titles = [await p.get_json('title')] // Home
     for (let i = 0; i < component_names.length; ++i) {
       let children = await p.get('children')
       p = await children.get(component_names[i])
+      if (navigation === undefined)
+        throw new jst_server.Problem(
+          'Navigation error',
+          `Can't find the path "${
+            component_names.slice(0, i + 1).map(name => '/' + name).join('')
+          }" in the navigation tree.`,
+          508
+        )
       component_titles.push(await p.get_json('title'))
     }
 
+    // similar to above but walks the top level laterally (not deeply)
     menu_names = await navigation.get_json('menu')
     let children = await navigation.get('children')
     menu_titles = [await navigation.get_json('title')] // Home
     for (let i = 0; i < menu_names.length; ++i) {
       let child = await children.get(menu_names[i])
+      if (child === undefined)
+        throw new jst_server.Problem(
+          'Navigation error',
+          `Can't find the path "/${menu_names[i]}" in the navigation tree.`
+          508
+        )
       menu_titles.push(await child.get('title'))
     }