From 93ac944ae7460f5b0288614c33dbfe08180e81a3 Mon Sep 17 00:00:00 2001
From: Roger Barnes
Date: Tue, 1 Dec 2026 19:40:37 +1100
Subject: [PATCH] Allow hosts to load valid map views when another definition
fails
---
apps/navigator/src/App.tsx | 12 ++++++---
apps/navigator/src/server/sctx-api.ts | 4 ++-
packages/map-views/src/node/index.ts | 10 +++++---
.../map-views/src/node/read-definitions.ts | 25 +++++++++++++------
.../map-views/tests/load-definitions.test.ts | 13 ++++++++++
5 files changed, 49 insertions(+), 15 deletions(-)
diff --git a/apps/navigator/src/App.tsx b/apps/navigator/src/App.tsx
index 764e3e3..ce6bdbf 100644
--- a/apps/navigator/src/App.tsx
+++ b/apps/navigator/src/App.tsx
@@ -16,6 +16,7 @@ import { useFetched } from './useFetched';
import { useUrlState } from './useUrlState';
type SpaceListItem = { name: string };
+type MapViewsResponse = { definitions: MapViewDefinition[]; errors: { path: string; message: string }[] };
export function App() {
const { spaceName, mapViewId, focus, values, setSpaceName, setMapViewId, setFocus, setValue } = useUrlState();
@@ -23,13 +24,13 @@ export function App() {
const [unattachedPanelOpen, setUnattachedPanelOpen] = useState(false);
const spaceList = useFetched('/api/spaces', fetchJson);
- const allDefinitions = useFetched('/api/map-views', fetchJson);
+ const allDefinitions = useFetched('/api/map-views', fetchJson);
// The whole space, unfiltered: gives the schema (to pick map views) and the nodes validation results attach to.
const fullSpace = useFetched(spaceName ? spaceUrl(spaceName) : undefined, fetchSpace);
const applicableDefinitions = useMemo(
- () => mapViewsForSchema(allDefinitions.data ?? [], fullSpace.data?.schemaId),
- [allDefinitions.data, fullSpace.data?.schemaId],
+ () => mapViewsForSchema(allDefinitions.data?.definitions ?? [], fullSpace.data?.schemaId),
+ [allDefinitions.data?.definitions, fullSpace.data?.schemaId],
);
const activeDefinition = applicableDefinitions.find((d) => d.id === mapViewId) ?? applicableDefinitions[0];
// Settle on the map view actually drawn, so the URL names it and a stale or missing `view` is replaced.
@@ -114,6 +115,11 @@ export function App() {
{error}
)}
+ {allDefinitions.data?.errors.map((e) => (
+
+ {e.message}
+
+ ))}
{fullSpace.data && !activeDefinition && (
No map view applies to this space's schema.
)}
diff --git a/apps/navigator/src/server/sctx-api.ts b/apps/navigator/src/server/sctx-api.ts
index 7921888..b57f280 100644
--- a/apps/navigator/src/server/sctx-api.ts
+++ b/apps/navigator/src/server/sctx-api.ts
@@ -21,7 +21,9 @@ export function sctxApi(): Plugin {
});
server.middlewares.use('/api/map-views', (_req, res) => {
try {
- sendJson(res, 200, loadMapViewDefinitions(loadConfig()));
+ const errors: { path: string; message: string }[] = [];
+ const definitions = loadMapViewDefinitions(loadConfig(), { onError: (e) => errors.push(e) });
+ sendJson(res, 200, { definitions, errors });
} catch (err) {
sendError(res, err);
}
diff --git a/packages/map-views/src/node/index.ts b/packages/map-views/src/node/index.ts
index 4973c5e..9affa83 100644
--- a/packages/map-views/src/node/index.ts
+++ b/packages/map-views/src/node/index.ts
@@ -15,11 +15,15 @@ const CONFIG_PLUGINS_DIR = 'plugins';
/**
* Every map view definition available with `config`, found as docs/definitions.md describes.
- * Throws on the first invalid definition, including one outside its namespace or with a duplicate `$id`.
+ * Throws on the first invalid definition unless `onError` is provided, in which case invalid
+ * definitions are reported to `onError` and skipped so valid definitions still load.
*
* `configDir` is the root of the main config file; it defaults to where structured-context finds it.
*/
-export function loadMapViewDefinitions(config: Config, options: { configDir?: string } = {}): MapViewDefinition[] {
+export function loadMapViewDefinitions(
+ config: Config,
+ options: { configDir?: string; onError?: (error: { path: string; message: string }) => void } = {},
+): MapViewDefinition[] {
const configDir = options.configDir ?? resolveConfigPath().root;
const configDirs = [...new Set([configDir, ...config.spaces.flatMap((s) => (s.sourceDir ? [s.sourceDir] : []))])];
@@ -34,7 +38,7 @@ export function loadMapViewDefinitions(config: Config, options: { configDir?: st
}),
];
- return readDefinitionFolders(folders);
+ return readDefinitionFolders(folders, { onError: options.onError });
}
/** Plugin short name → root directory, for plugins in a config root's plugins folder or named by the config. */
diff --git a/packages/map-views/src/node/read-definitions.ts b/packages/map-views/src/node/read-definitions.ts
index 113119d..6e7de06 100644
--- a/packages/map-views/src/node/read-definitions.ts
+++ b/packages/map-views/src/node/read-definitions.ts
@@ -10,18 +10,27 @@ export type DefinitionSource = { kind: 'core' } | { kind: 'plugin'; plugin: stri
/**
* Definitions in `folders` and this package's embedded ones, each checked against the namespace of its source.
- * Throws on the first invalid definition or a repeated id.
+ * Throws on the first invalid definition or a repeated id unless `onError` is provided, in which case
+ * invalid definitions are reported to `onError` and skipped so valid definitions still load.
*/
-export function readDefinitionFolders(folders: { dir: string; source: DefinitionSource }[]): MapViewDefinition[] {
+export function readDefinitionFolders(
+ folders: { dir: string; source: DefinitionSource }[],
+ options: { onError?: (error: { path: string; message: string }) => void } = {},
+): MapViewDefinition[] {
const foundAt = new Map();
const definitions: MapViewDefinition[] = [];
const add = (label: string, text: string, source: DefinitionSource) => {
- const definition = parseMapViewDefinition(label, text);
- assertIdNamespace(definition.id, source, label);
- const existing = foundAt.get(definition.id);
- if (existing) throw new Error(`Map view id "${definition.id}" is defined by both ${existing} and ${label}.`);
- foundAt.set(definition.id, label);
- definitions.push(definition);
+ try {
+ const definition = parseMapViewDefinition(label, text);
+ assertIdNamespace(definition.id, source, label);
+ const existing = foundAt.get(definition.id);
+ if (existing) throw new Error(`Map view id "${definition.id}" is defined by both ${existing} and ${label}.`);
+ foundAt.set(definition.id, label);
+ definitions.push(definition);
+ } catch (err) {
+ if (!options.onError) throw err;
+ options.onError({ path: label, message: err instanceof Error ? err.message : String(err) });
+ }
};
for (const [file, text] of Object.entries(BUNDLED_DEFINITIONS))
diff --git a/packages/map-views/tests/load-definitions.test.ts b/packages/map-views/tests/load-definitions.test.ts
index 655af37..891895f 100644
--- a/packages/map-views/tests/load-definitions.test.ts
+++ b/packages/map-views/tests/load-definitions.test.ts
@@ -77,4 +77,17 @@ describe('loadMapViewDefinitions', () => {
const config: Config = { spaces: [{ name: 's', path: '.', sourceDir: join(root, 'included') }] };
expect(() => ids(config)).toThrow(/"sctx-map-view:\/\/a" is defined by both/);
});
+
+ test('with onError, reports invalid definitions and still loads valid ones', () => {
+ write('config/.sctx/map-views/bad.json5', '{ $id: "sctx-map-view://bad", regions: [] }');
+ write('config/.sctx/map-views/good.json5', definition('sctx-map-view://good'));
+ const errors: { path: string; message: string }[] = [];
+ const loaded = loadMapViewDefinitions(noSpaces, {
+ configDir: join(root, 'config'),
+ onError: (e) => errors.push(e),
+ });
+ expect(loaded.map((d) => d.id)).toEqual([...bundled(), 'sctx-map-view://good']);
+ expect(errors).toHaveLength(1);
+ expect(errors[0]!.path).toEndWith('bad.json5');
+ });
});