Add try-catch for error handling and improve logging (#4123)

This commit is contained in:
conico974
2026-03-17 17:42:34 +01:00
committed by GitHub
parent 4297de3571
commit 5f132bc979
2 changed files with 58 additions and 53 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ export async function ignoreDataThrownError<T>(promise: Promise<T>): Promise<T |
try {
return await promise;
} catch (error) {
getExposableError(error as Error);
console.warn('ignored Data error', getExposableError(error as Error));
return null;
}
}
+57 -52
View File
@@ -458,63 +458,68 @@ async function resolveContentRefInSpace(
contentRef: ContentRef,
options: ResolveContentRefOptions = {}
) {
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;
}
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;
}
}
/**