Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-content-docs): fix error message context (error cause) when doc processing fails #8234

Merged
merged 7 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -93,6 +93,18 @@ function createTestUtils({
});
}

// Makes it easier to assert failure cases
async function getProcessDocFileError(
docFileArg: DocFile | string,
): Promise<Error> {
try {
await processDocFile(docFileArg);
return new Error("unexpected: getProcessDocFileError didn't crash");
} catch (e) {
return e as Error;
}
}

async function testMeta(
docFileSource: string,
expectedMetadata: Optional<
Expand Down Expand Up @@ -172,7 +184,13 @@ function createTestUtils({
};
}

return {processDocFile, testMeta, testSlug, generateNavigation};
return {
processDocFile,
getProcessDocFileError,
testMeta,
testSlug,
generateNavigation,
};
}

describe('simple site', () => {
Expand Down Expand Up @@ -683,16 +701,21 @@ describe('simple site', () => {

it('docs with invalid id', async () => {
const {defaultTestUtils} = await loadSite();
await expect(async () =>
defaultTestUtils.processDocFile(
createFakeDocFile({
source: 'some/fake/path',
frontMatter: {
id: 'Hello/world',
},
}),
),
).rejects.toThrowErrorMatchingInlineSnapshot(

const error = await defaultTestUtils.getProcessDocFileError(
createFakeDocFile({
source: 'some/fake/path',
frontMatter: {
id: 'Hello/world',
},
}),
);

expect(error.message).toMatchInlineSnapshot(
`"Can't process doc metadata for doc at path path=some/fake/path in version name=current"`,
);
expect(error.cause).toBeDefined();
expect(error.cause!.message).toMatchInlineSnapshot(
`"Document id "Hello/world" cannot include slash."`,
);
});
Expand Down
7 changes: 3 additions & 4 deletions packages/docusaurus-plugin-content-docs/src/docs.ts
Expand Up @@ -316,18 +316,17 @@ async function doProcessDocMetadata({
};
}

export function processDocMetadata(args: {
export async function processDocMetadata(args: {
docFile: DocFile;
versionMetadata: VersionMetadata;
context: LoadContext;
options: MetadataOptions;
env: DocEnv;
}): Promise<DocMetadataBase> {
try {
return doProcessDocMetadata(args);
return await doProcessDocMetadata(args);
} catch (err) {
logger.error`Can't process doc metadata for doc at path path=${args.docFile.filePath} in version name=${args.versionMetadata.versionName}`;
throw err;
throw new Error(`Can't process doc metadata for doc at path path=${args.docFile.filePath} in version name=${args.versionMetadata.versionName}`, {cause: err as Error});
}
}

Expand Down