mirror of
https://github.com/GitbookIO/gitbook.git
synced 2026-09-21 10:03:31 +00:00
Improve error messages around undefined site sections. (#3179)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"gitbook-v2": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improve error messages around undefined site sections.
|
||||||
@@ -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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user