From 5f132bc979c58f1bc13864692db681d4c2172ea3 Mon Sep 17 00:00:00 2001 From: conico974 Date: Tue, 17 Mar 2026 17:42:34 +0100 Subject: [PATCH] Add try-catch for error handling and improve logging (#4123) --- packages/gitbook/src/lib/data/errors.ts | 2 +- packages/gitbook/src/lib/references.tsx | 109 +++++++++++++----------- 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/packages/gitbook/src/lib/data/errors.ts b/packages/gitbook/src/lib/data/errors.ts index 90e1ca9c2..686860713 100644 --- a/packages/gitbook/src/lib/data/errors.ts +++ b/packages/gitbook/src/lib/data/errors.ts @@ -59,7 +59,7 @@ export async function ignoreDataThrownError(promise: Promise): Promise { - // If the space is found in the current site, we use the current share key to generate links. - if ('site' in context) { - return findSiteSpaceBy( - context.structure, - (siteSpace) => siteSpace.space.id === spaceId - ) - ? context.shareKey - : undefined; - } + try { + const ctx = await createContextForSpace(spaceId, { + ...context, + shareKey: (() => { + // If the space is found in the current site, we use the current share key to generate links. + if ('site' in context) { + return findSiteSpaceBy( + context.structure, + (siteSpace) => siteSpace.space.id === spaceId + ) + ? context.shareKey + : undefined; + } - return context.space.id === spaceId ? context.shareKey : undefined; - })(), - }); + return context.space.id === spaceId ? context.shareKey : undefined; + })(), + }); - if (!ctx) { - return null; - } - - const resolved = await resolveContentRef(contentRef, ctx.spaceContext, options); - - if (!resolved) { - return null; - } - - // Prefer the variant title when available, then the section title, then fallback to the space title. - const ancestorLabel = (() => { - if ('site' in context) { - const currentLanguage = context.siteSpace.space.language; - const foundSiteSpace = findSiteSpaceBy( - context.structure, - (siteSpace) => siteSpace.space.id === spaceId - ); - if (foundSiteSpace?.siteSpace) { - return getLocalizedTitle(foundSiteSpace.siteSpace, currentLanguage); - } - if (foundSiteSpace?.siteSection) { - return getLocalizedTitle(foundSiteSpace.siteSection, currentLanguage); - } - return ctx.spaceContext.space.title; + if (!ctx) { + return null; } - return ctx.spaceContext.space.title; - })(); + const resolved = await resolveContentRef(contentRef, ctx.spaceContext, options); - return { - ...resolved, - ancestors: [ - { - label: ancestorLabel, - href: ctx.baseURL.toString(), - }, - ...(resolved.ancestors ?? []), - ].filter(filterOutNullable), - }; + if (!resolved) { + return null; + } + + // Prefer the variant title when available, then the section title, then fallback to the space title. + const ancestorLabel = (() => { + if ('site' in context) { + const currentLanguage = context.siteSpace.space.language; + const foundSiteSpace = findSiteSpaceBy( + context.structure, + (siteSpace) => siteSpace.space.id === spaceId + ); + if (foundSiteSpace?.siteSpace) { + return getLocalizedTitle(foundSiteSpace.siteSpace, currentLanguage); + } + if (foundSiteSpace?.siteSection) { + return getLocalizedTitle(foundSiteSpace.siteSection, currentLanguage); + } + return ctx.spaceContext.space.title; + } + + return ctx.spaceContext.space.title; + })(); + + return { + ...resolved, + ancestors: [ + { + label: ancestorLabel, + href: ctx.baseURL.toString(), + }, + ...(resolved.ancestors ?? []), + ].filter(filterOutNullable), + }; + } catch (error) { + console.warn(`Error resolving content ref in space ${spaceId}:`, error); + return null; + } } /**