Prevent crash when integration associated to a block is no longer installed (#213)

* Prevent crash when integration is not installed for a block

* Format
This commit is contained in:
Samy Pessé
2024-03-05 19:24:51 +00:00
committed by GitHub
parent a9f9f18e66
commit d22944419f
3 changed files with 29 additions and 18 deletions
@@ -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<DocumentBlockIntegratio
context: contentKitContext,
};
const initialOutput = await renderIntegrationUi(block.data.integration, initialInput);
const initialOutput = await ignoreAPIError(
renderIntegrationUi(block.data.integration, initialInput),
);
if (!initialOutput) {
return null;
}
return (
<div className={tcls(style)}>
+16
View File
@@ -643,6 +643,22 @@ export function userAgent(): string {
return result;
}
/**
* Ignore error for an API call.
*/
export async function ignoreAPIError<T>(promise: Promise<T>): Promise<T | null> {
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.
*/
+6 -16
View File
@@ -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<T>(promise: Promise<T>): Promise<T | null> {
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;
}