diff --git a/src/components/DocumentView/Integration/IntegrationBlock.tsx b/src/components/DocumentView/Integration/IntegrationBlock.tsx index 31fda46af..41f985f52 100644 --- a/src/components/DocumentView/Integration/IntegrationBlock.tsx +++ b/src/components/DocumentView/Integration/IntegrationBlock.tsx @@ -17,7 +17,7 @@ import { import { ContentKitContext, DocumentBlockIntegration } from '@gitbook/api'; import { ContentKit, ContentKitOutput, ContentKitServerContext } from '@gitbook/react-contentkit'; -import { renderIntegrationUi } from '@/lib/api'; +import { ignoreAPIError, renderIntegrationUi } from '@/lib/api'; import { parseMarkdown } from '@/lib/markdown'; import { tcls } from '@/lib/tailwind'; @@ -72,7 +72,12 @@ export async function IntegrationBlock(props: BlockProps diff --git a/src/lib/api.ts b/src/lib/api.ts index 7a4b11d7c..d73e2d70e 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -643,6 +643,22 @@ export function userAgent(): string { return result; } +/** + * Ignore error for an API call. + */ +export async function ignoreAPIError(promise: Promise): Promise { + try { + return await promise; + } catch (error) { + const code = (error as GitBookAPIError).code; + if (code >= 400 && code < 500) { + return null; + } + + throw error; + } +} + /** * Iterate over a paginated API endpoint and return all the items. */ diff --git a/src/lib/references.ts b/src/lib/references.ts index e4a81af18..8d5829fa3 100644 --- a/src/lib/references.ts +++ b/src/lib/references.ts @@ -8,6 +8,7 @@ import { getRevisionPages, getSpace, getUserById, + ignoreAPIError, } from './api'; import { gitbookAppHref, pageHref, PageHrefContext } from './links'; import { resolvePageId } from './pages'; @@ -98,7 +99,7 @@ export async function resolveContentRef( const targetSpace = contentRef.space === space.id ? space - : await ignoreError(getSpace(contentRef.space)); + : await ignoreAPIError(getSpace(contentRef.space)); if (!targetSpace) { return { href: gitbookAppHref(`/s/${contentRef.space}`), @@ -136,7 +137,7 @@ export async function resolveContentRef( } case 'collection': { - const collection = await ignoreError(getCollection(contentRef.collection)); + const collection = await ignoreAPIError(getCollection(contentRef.collection)); if (!collection) { return { href: gitbookAppHref(`/s/${contentRef.collection}`), @@ -157,25 +158,14 @@ export async function resolveContentRef( } } -async function ignoreError(promise: Promise): Promise { - try { - return await promise; - } catch (error) { - const code = (error as GitBookAPIError).code; - if (code >= 400 && code < 500) { - return null; - } - - throw error; - } -} - async function resolveContentRefInSpace(spaceId: string, contentRef: ContentRef) { const pointer: ContentPointer = { spaceId, }; - const result = await ignoreError(Promise.all([getSpace(spaceId), getRevisionPages(pointer)])); + const result = await ignoreAPIError( + Promise.all([getSpace(spaceId), getRevisionPages(pointer)]), + ); if (!result) { return null; }