Improve error messages around undefined site sections. (#3179)

This commit is contained in:
Steven H
2025-04-23 12:47:28 +01:00
committed by GitHub
parent 2a23f1f003
commit 634e0b4302
2 changed files with 44 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"gitbook-v2": patch
---
Improve error messages around undefined site sections.
+39 -12
View File
@@ -19,6 +19,7 @@ import {
getDataOrNull, getDataOrNull,
throwIfDataError, throwIfDataError,
} from '@v2/lib/data'; } from '@v2/lib/data';
import assertNever from 'assert-never';
import { notFound } from 'next/navigation'; import { notFound } from 'next/navigation';
import { assert } from 'ts-essentials'; import { assert } from 'ts-essentials';
import { GITBOOK_URL } from './env'; import { GITBOOK_URL } from './env';
@@ -242,19 +243,45 @@ export async function fetchSiteContextByIds(
? parseSiteSectionsAndGroups(siteStructure, ids.siteSection) ? parseSiteSectionsAndGroups(siteStructure, ids.siteSection)
: null; : null;
const siteSpace = ( // Parse the current siteSpace and siteSpaces based on the site structure type.
siteStructure.type === 'siteSpaces' && siteStructure.structure const { siteSpaces, siteSpace }: { siteSpaces: SiteSpace[]; siteSpace: SiteSpace } = (() => {
? siteStructure.structure if (siteStructure.type === 'siteSpaces') {
: sections?.current.siteSpaces const siteSpaces = siteStructure.structure;
)?.find((siteSpace) => siteSpace.id === ids.siteSpace); const siteSpace = siteSpaces.find((siteSpace) => siteSpace.id === ids.siteSpace);
if (!siteSpace) {
throw new Error('Site space not found');
}
const siteSpaces = if (!siteSpace) {
siteStructure.type === 'siteSpaces' throw new Error(
? siteStructure.structure `Site space "${ids.siteSpace}" not found in structure type="siteSpaces"`
: (sections?.current.siteSpaces ?? []); );
}
return { siteSpaces, siteSpace };
}
if (siteStructure.type === 'sections') {
assert(
sections,
`cannot find site space "${ids.siteSpace}" because parsed sections are missing siteStructure.type="sections" siteSection="${ids.siteSection}"`
);
const currentSection = sections.current;
const siteSpaces = currentSection.siteSpaces;
const siteSpace = currentSection.siteSpaces.find(
(siteSpace) => siteSpace.id === ids.siteSpace
);
if (!siteSpace) {
throw new Error(
`Site space "${ids.siteSpace}" not found in structure type="sections" currentSection="${currentSection.id}"`
);
}
return { siteSpaces, siteSpace };
}
// @ts-expect-error
assertNever(siteStructure, `cannot handle site structure of type ${siteStructure.type}`);
})();
const customization = (() => { const customization = (() => {
if (ids.siteSpace) { if (ids.siteSpace) {