From 446e0f73219a442ba47f49c1fe732a1ccde64a30 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Wed, 23 Apr 2025 12:30:43 +0100 Subject: [PATCH 1/3] Improve error messages around undefined site sections. --- packages/gitbook-v2/src/lib/context.ts | 41 ++++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/packages/gitbook-v2/src/lib/context.ts b/packages/gitbook-v2/src/lib/context.ts index d25dd1946e..84c6231b91 100644 --- a/packages/gitbook-v2/src/lib/context.ts +++ b/packages/gitbook-v2/src/lib/context.ts @@ -19,6 +19,7 @@ import { getDataOrNull, throwIfDataError, } from '@v2/lib/data'; +import assertNever from 'assert-never'; import { notFound } from 'next/navigation'; import { assert } from 'ts-essentials'; import { GITBOOK_URL } from './env'; @@ -242,19 +243,35 @@ export async function fetchSiteContextByIds( ? parseSiteSectionsAndGroups(siteStructure, ids.siteSection) : null; - const siteSpace = ( - siteStructure.type === 'siteSpaces' && siteStructure.structure - ? siteStructure.structure - : sections?.current.siteSpaces - )?.find((siteSpace) => siteSpace.id === ids.siteSpace); - if (!siteSpace) { - throw new Error('Site space not found'); - } + const { siteSpaces, siteSpace }: { siteSpaces: SiteSpace[]; siteSpace: SiteSpace } = (() => { + if (siteStructure.type === 'siteSpaces') { + const siteSpaces = siteStructure.structure; + const siteSpace = siteSpaces.find((siteSpace) => siteSpace.id === ids.siteSpace); + + if (!siteSpace) { + throw new Error(`Site space "${ids.siteSpace}" not found in structure type="siteSpaces"`); + } + + return { siteSpaces, siteSpace }; + } + + if (siteStructure.type === 'sections') { + assert(sections, `missing parsed sections siteStructure.type="sections" siteSection="${ids.siteSection}"`); - const siteSpaces = - siteStructure.type === 'siteSpaces' - ? siteStructure.structure - : (sections?.current.siteSpaces ?? []); + 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 = (() => { if (ids.siteSpace) { From 40916b852377310bbe0984f459e08c7fc3b39429 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Wed, 23 Apr 2025 12:33:45 +0100 Subject: [PATCH 2/3] Message --- packages/gitbook-v2/src/lib/context.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/gitbook-v2/src/lib/context.ts b/packages/gitbook-v2/src/lib/context.ts index 84c6231b91..7ef0f463b9 100644 --- a/packages/gitbook-v2/src/lib/context.ts +++ b/packages/gitbook-v2/src/lib/context.ts @@ -243,27 +243,37 @@ export async function fetchSiteContextByIds( ? parseSiteSectionsAndGroups(siteStructure, ids.siteSection) : null; + // Parse the current siteSpace and siteSpaces based on the site structure type. const { siteSpaces, siteSpace }: { siteSpaces: SiteSpace[]; siteSpace: SiteSpace } = (() => { if (siteStructure.type === 'siteSpaces') { const siteSpaces = siteStructure.structure; const siteSpace = siteSpaces.find((siteSpace) => siteSpace.id === ids.siteSpace); if (!siteSpace) { - throw new Error(`Site space "${ids.siteSpace}" not found in structure type="siteSpaces"`); + throw new Error( + `Site space "${ids.siteSpace}" not found in structure type="siteSpaces"` + ); } return { siteSpaces, siteSpace }; } if (siteStructure.type === 'sections') { - assert(sections, `missing parsed sections siteStructure.type="sections" siteSection="${ids.siteSection}"`); + 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); + 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}"`); + throw new Error( + `Site space "${ids.siteSpace}" not found in structure type="sections" currentSection="${currentSection.id}"` + ); } return { siteSpaces, siteSpace }; From 5bf99cfcf3d5dfe1c5bb9fb1621b4f5b143a8c13 Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Wed, 23 Apr 2025 12:34:08 +0100 Subject: [PATCH 3/3] changeset --- .changeset/soft-planes-talk.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/soft-planes-talk.md diff --git a/.changeset/soft-planes-talk.md b/.changeset/soft-planes-talk.md new file mode 100644 index 0000000000..d633d90dcd --- /dev/null +++ b/.changeset/soft-planes-talk.md @@ -0,0 +1,5 @@ +--- +"gitbook-v2": patch +--- + +Improve error messages around undefined site sections.