« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/content-function.js10
-rw-r--r--src/content/dependencies/index.js18
2 files changed, 12 insertions, 16 deletions
diff --git a/src/content-function.js b/src/content-function.js
index 18092707..70e7be38 100644
--- a/src/content-function.js
+++ b/src/content-function.js
@@ -4,6 +4,8 @@ import {
   setIntersection,
 } from './util/sugar.js';
 
+export class ContentFunctionSpecError extends Error {}
+
 export default function contentFunction({
   contentDependencies = [],
   extraDependencies = [],
@@ -25,19 +27,19 @@ export default function contentFunction({
     setIntersection(expectedContentDependencyKeys, expectedExtraDependencyKeys);
 
   if (!empty(overlappingContentExtraDependencyKeys)) {
-    throw new Error(`Overlap in content and extra dependency keys: ${[...overlappingContentExtraDependencyKeys].join(', ')}`);
+    throw new ContentFunctionSpecError(`Overlap in content and extra dependency keys: ${[...overlappingContentExtraDependencyKeys].join(', ')}`);
   }
 
   if (!generate) {
-    throw new Error(`Expected generate function`);
+    throw new ContentFunctionSpecError(`Expected generate function`);
   }
 
   if (sprawl && !expectedExtraDependencyKeys.has('wikiData')) {
-    throw new Error(`Content functions which sprawl must specify wikiData in extraDependencies`);
+    throw new ContentFunctionSpecError(`Content functions which sprawl must specify wikiData in extraDependencies`);
   }
 
   if (slots && !expectedExtraDependencyKeys.has('html')) {
-    throw new Error(`Content functions with slots must specify html in extraDependencies`);
+    throw new ContentFunctionSpecError(`Content functions with slots must specify html in extraDependencies`);
   }
 
   // Pass all the details to expectDependencies, which will recursively build
diff --git a/src/content/dependencies/index.js b/src/content/dependencies/index.js
index e78bc94b..f7a0a659 100644
--- a/src/content/dependencies/index.js
+++ b/src/content/dependencies/index.js
@@ -6,7 +6,7 @@ import {readdir} from 'node:fs/promises';
 import * as path from 'node:path';
 import {fileURLToPath} from 'node:url';
 
-import contentFunction from '../../content-function.js';
+import contentFunction, {ContentFunctionSpecError} from '../../content-function.js';
 import {color, logWarn} from '../../util/cli.js';
 import {annotateFunction} from '../../util/sugar.js';
 
@@ -205,6 +205,8 @@ export function watchContentDependencies({
 
       if (typeof error === 'string') {
         console.error(color.yellow(error));
+      } else if (error instanceof ContentFunctionSpecError) {
+        console.error(color.yellow(error.message));
       } else {
         console.error(error);
       }
@@ -214,23 +216,15 @@ export function watchContentDependencies({
   }
 
   function processFunctionSpec(functionName, spec) {
-    if (typeof spec.data === 'function') {
+    if (typeof spec?.data === 'function') {
       annotateFunction(spec.data, {name: functionName, description: 'data'});
     }
 
-    if (typeof spec.generate === 'function') {
+    if (typeof spec?.generate === 'function') {
       annotateFunction(spec.generate, {name: functionName});
     }
 
-    let fn;
-    try {
-      fn = contentFunction(spec);
-    } catch (error) {
-      error.message = `Error loading spec: ${error.message}`;
-      throw error;
-    }
-
-    return fn;
+    return contentFunction(spec);
   }
 }