mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-11 16:17:25 +02:00
fix(plugin-content-docs): fix error message context (error cause) when doc processing fails (#8234)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
327158b085
commit
91b92fcd08
2 changed files with 40 additions and 15 deletions
|
@ -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(
|
async function testMeta(
|
||||||
docFileSource: string,
|
docFileSource: string,
|
||||||
expectedMetadata: Optional<
|
expectedMetadata: Optional<
|
||||||
|
@ -172,7 +184,13 @@ function createTestUtils({
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {processDocFile, testMeta, testSlug, generateNavigation};
|
return {
|
||||||
|
processDocFile,
|
||||||
|
getProcessDocFileError,
|
||||||
|
testMeta,
|
||||||
|
testSlug,
|
||||||
|
generateNavigation,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
describe('simple site', () => {
|
describe('simple site', () => {
|
||||||
|
@ -683,16 +701,21 @@ describe('simple site', () => {
|
||||||
|
|
||||||
it('docs with invalid id', async () => {
|
it('docs with invalid id', async () => {
|
||||||
const {defaultTestUtils} = await loadSite();
|
const {defaultTestUtils} = await loadSite();
|
||||||
await expect(async () =>
|
|
||||||
defaultTestUtils.processDocFile(
|
const error = await defaultTestUtils.getProcessDocFileError(
|
||||||
createFakeDocFile({
|
createFakeDocFile({
|
||||||
source: 'some/fake/path',
|
source: 'some/fake/path',
|
||||||
frontMatter: {
|
frontMatter: {
|
||||||
id: 'Hello/world',
|
id: 'Hello/world',
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
),
|
);
|
||||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
|
||||||
|
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."`,
|
`"Document id "Hello/world" cannot include slash."`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
|
@ -316,7 +316,7 @@ async function doProcessDocMetadata({
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function processDocMetadata(args: {
|
export async function processDocMetadata(args: {
|
||||||
docFile: DocFile;
|
docFile: DocFile;
|
||||||
versionMetadata: VersionMetadata;
|
versionMetadata: VersionMetadata;
|
||||||
context: LoadContext;
|
context: LoadContext;
|
||||||
|
@ -324,10 +324,12 @@ export function processDocMetadata(args: {
|
||||||
env: DocEnv;
|
env: DocEnv;
|
||||||
}): Promise<DocMetadataBase> {
|
}): Promise<DocMetadataBase> {
|
||||||
try {
|
try {
|
||||||
return doProcessDocMetadata(args);
|
return await doProcessDocMetadata(args);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error`Can't process doc metadata for doc at path path=${args.docFile.filePath} in version name=${args.versionMetadata.versionName}`;
|
throw new Error(
|
||||||
throw err;
|
`Can't process doc metadata for doc at path path=${args.docFile.filePath} in version name=${args.versionMetadata.versionName}`,
|
||||||
|
{cause: err as Error},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue