refactor: unify error handling behavior (#6755)

* refactor: unify error handling behavior

* revert

* normalize paths

* test...

* change

* does this work...

* fix...

* maybe fix

* maybe fix

* fix

* fix...
This commit is contained in:
Joshua Chen 2022-02-25 15:07:13 +08:00 committed by GitHub
parent dcbf9f644e
commit f903422617
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
58 changed files with 1123 additions and 299 deletions

View file

@ -26,8 +26,7 @@ Array [
<link href=\\"https://docusaurus.io/myBaseUrl/blog/mdx-blog-post\\"/>
<updated>2021-03-05T00:00:00.000Z</updated>
<summary type=\\"html\\"><![CDATA[HTML Heading 1]]></summary>
<content type=\\"html\\"><![CDATA[<h1>HTML Heading 1</h1><h2>HTML Heading 2</h2><p>HTML Paragraph</p><p>Import DOM</p><h1>Heading 1</h1><h2 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-2\\">Heading 2<a class=\\"hash-link\\" href=\\"#heading-2\\" title=\\"Direct link to heading\\"></a></h2><h3 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-3\\">Heading 3<a class=\\"hash-link\\" href=\\"#heading-3\\" title=\\"Direct link to heading\\"></a></h3><h4 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-4\\">Heading 4<a class=\\"hash-link\\" href=\\"#heading-4\\" title=\\"Direct link to heading\\"></a></h4><h5 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-5\\">Heading 5<a class=\\"hash-link\\" href=\\"#heading-5\\" title=\\"Direct link to heading\\"></a></h5><ul><li>list1</li><li>list2</li><li>list3</li></ul><ul><li>list1</li><li>list2</li><li>list3</li></ul><p>Normal Text <em>Italics Text</em> <strong>Bold Text</strong></p><p><a href=\\"https://v2.docusaurus.io/\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">link</a>
<img src=\\"https://v2.docusaurus.io/\\" alt=\\"image\\"></p>]]></content>
<content type=\\"html\\"><![CDATA[<h1>HTML Heading 1</h1><h2>HTML Heading 2</h2><p>HTML Paragraph</p><p>Import DOM</p><h1>Heading 1</h1><h2 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-2\\">Heading 2<a class=\\"hash-link\\" href=\\"#heading-2\\" title=\\"Direct link to heading\\"></a></h2><h3 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-3\\">Heading 3<a class=\\"hash-link\\" href=\\"#heading-3\\" title=\\"Direct link to heading\\"></a></h3><h4 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-4\\">Heading 4<a class=\\"hash-link\\" href=\\"#heading-4\\" title=\\"Direct link to heading\\"></a></h4><h5 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-5\\">Heading 5<a class=\\"hash-link\\" href=\\"#heading-5\\" title=\\"Direct link to heading\\"></a></h5><ul><li>list1</li><li>list2</li><li>list3</li></ul><ul><li>list1</li><li>list2</li><li>list3</li></ul><p>Normal Text <em>Italics Text</em> <strong>Bold Text</strong></p><p><a href=\\"https://v2.docusaurus.io/\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">link</a><img src=\\"https://v2.docusaurus.io/\\" alt=\\"image\\"></p>]]></content>
</entry>
<entry>
<title type=\\"html\\"><![CDATA[Complex Slug]]></title>
@ -103,7 +102,7 @@ Array [
},
{
\\"id\\": \\"/mdx-blog-post\\",
\\"content_html\\": \\"<h1>HTML Heading 1</h1><h2>HTML Heading 2</h2><p>HTML Paragraph</p><p>Import DOM</p><h1>Heading 1</h1><h2 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-2\\\\\\">Heading 2<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-2\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h2><h3 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-3\\\\\\">Heading 3<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-3\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h3><h4 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-4\\\\\\">Heading 4<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-4\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h4><h5 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-5\\\\\\">Heading 5<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-5\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h5><ul><li>list1</li><li>list2</li><li>list3</li></ul><ul><li>list1</li><li>list2</li><li>list3</li></ul><p>Normal Text <em>Italics Text</em> <strong>Bold Text</strong></p><p><a href=\\\\\\"https://v2.docusaurus.io/\\\\\\" target=\\\\\\"_blank\\\\\\" rel=\\\\\\"noopener noreferrer\\\\\\">link</a>\\\\n<img src=\\\\\\"https://v2.docusaurus.io/\\\\\\" alt=\\\\\\"image\\\\\\"></p>\\",
\\"content_html\\": \\"<h1>HTML Heading 1</h1><h2>HTML Heading 2</h2><p>HTML Paragraph</p><p>Import DOM</p><h1>Heading 1</h1><h2 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-2\\\\\\">Heading 2<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-2\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h2><h3 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-3\\\\\\">Heading 3<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-3\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h3><h4 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-4\\\\\\">Heading 4<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-4\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h4><h5 class=\\\\\\"anchor anchorWithStickyNavbar_mojV\\\\\\" id=\\\\\\"heading-5\\\\\\">Heading 5<a class=\\\\\\"hash-link\\\\\\" href=\\\\\\"#heading-5\\\\\\" title=\\\\\\"Direct link to heading\\\\\\"></a></h5><ul><li>list1</li><li>list2</li><li>list3</li></ul><ul><li>list1</li><li>list2</li><li>list3</li></ul><p>Normal Text <em>Italics Text</em> <strong>Bold Text</strong></p><p><a href=\\\\\\"https://v2.docusaurus.io/\\\\\\" target=\\\\\\"_blank\\\\\\" rel=\\\\\\"noopener noreferrer\\\\\\">link</a><img src=\\\\\\"https://v2.docusaurus.io/\\\\\\" alt=\\\\\\"image\\\\\\"></p>\\",
\\"url\\": \\"https://docusaurus.io/myBaseUrl/blog/mdx-blog-post\\",
\\"title\\": \\"Full Blog Sample\\",
\\"summary\\": \\"HTML Heading 1\\",
@ -197,8 +196,7 @@ Array [
<guid>/mdx-blog-post</guid>
<pubDate>Fri, 05 Mar 2021 00:00:00 GMT</pubDate>
<description><![CDATA[HTML Heading 1]]></description>
<content:encoded><![CDATA[<h1>HTML Heading 1</h1><h2>HTML Heading 2</h2><p>HTML Paragraph</p><p>Import DOM</p><h1>Heading 1</h1><h2 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-2\\">Heading 2<a class=\\"hash-link\\" href=\\"#heading-2\\" title=\\"Direct link to heading\\"></a></h2><h3 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-3\\">Heading 3<a class=\\"hash-link\\" href=\\"#heading-3\\" title=\\"Direct link to heading\\"></a></h3><h4 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-4\\">Heading 4<a class=\\"hash-link\\" href=\\"#heading-4\\" title=\\"Direct link to heading\\"></a></h4><h5 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-5\\">Heading 5<a class=\\"hash-link\\" href=\\"#heading-5\\" title=\\"Direct link to heading\\"></a></h5><ul><li>list1</li><li>list2</li><li>list3</li></ul><ul><li>list1</li><li>list2</li><li>list3</li></ul><p>Normal Text <em>Italics Text</em> <strong>Bold Text</strong></p><p><a href=\\"https://v2.docusaurus.io/\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">link</a>
<img src=\\"https://v2.docusaurus.io/\\" alt=\\"image\\"></p>]]></content:encoded>
<content:encoded><![CDATA[<h1>HTML Heading 1</h1><h2>HTML Heading 2</h2><p>HTML Paragraph</p><p>Import DOM</p><h1>Heading 1</h1><h2 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-2\\">Heading 2<a class=\\"hash-link\\" href=\\"#heading-2\\" title=\\"Direct link to heading\\"></a></h2><h3 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-3\\">Heading 3<a class=\\"hash-link\\" href=\\"#heading-3\\" title=\\"Direct link to heading\\"></a></h3><h4 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-4\\">Heading 4<a class=\\"hash-link\\" href=\\"#heading-4\\" title=\\"Direct link to heading\\"></a></h4><h5 class=\\"anchor anchorWithStickyNavbar_mojV\\" id=\\"heading-5\\">Heading 5<a class=\\"hash-link\\" href=\\"#heading-5\\" title=\\"Direct link to heading\\"></a></h5><ul><li>list1</li><li>list2</li><li>list3</li></ul><ul><li>list1</li><li>list2</li><li>list3</li></ul><p>Normal Text <em>Italics Text</em> <strong>Bold Text</strong></p><p><a href=\\"https://v2.docusaurus.io/\\" target=\\"_blank\\" rel=\\"noopener noreferrer\\">link</a><img src=\\"https://v2.docusaurus.io/\\" alt=\\"image\\"></p>]]></content:encoded>
</item>
<item>
<title><![CDATA[Complex Slug]]></title>

View file

@ -75,3 +75,64 @@ Object {
},
}
`;
exports[`loadBlog test blog tags: no pagination 1`] = `
Object {
"/blog/tags/tag-1": Object {
"items": Array [
"/simple/slug/another",
"/another/tags",
"/another/tags2",
],
"name": "tag1",
"pages": Array [
Object {
"items": Array [
"/simple/slug/another",
"/another/tags",
"/another/tags2",
],
"metadata": Object {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": null,
"page": 1,
"permalink": "/blog/tags/tag-1",
"postsPerPage": 3,
"previousPage": null,
"totalCount": 3,
"totalPages": 1,
},
},
],
"permalink": "/blog/tags/tag-1",
},
"/blog/tags/tag-2": Object {
"items": Array [
"/another/tags",
"/another/tags2",
],
"name": "tag2",
"pages": Array [
Object {
"items": Array [
"/another/tags",
"/another/tags2",
],
"metadata": Object {
"blogDescription": "Blog",
"blogTitle": "Blog",
"nextPage": null,
"page": 1,
"permalink": "/blog/tags/tag-2",
"postsPerPage": 2,
"previousPage": null,
"totalCount": 2,
"totalPages": 1,
},
},
],
"permalink": "/blog/tags/tag-2",
},
}
`;

View file

@ -54,9 +54,9 @@ function testField(params: {
)}`,
),
);
} catch (e) {
} catch (err) {
// eslint-disable-next-line jest/no-conditional-expect
expect(e.message).toMatch(new RegExp(escapeStringRegexp(message)));
expect(err.message).toMatch(new RegExp(escapeStringRegexp(message)));
}
});
});

View file

@ -479,4 +479,17 @@ describe('loadBlog', () => {
expect(Object.keys(blogTags).length).toEqual(2);
expect(blogTags).toMatchSnapshot();
});
test('test blog tags: no pagination', async () => {
const siteDir = path.join(
__dirname,
'__fixtures__',
'website-blog-with-tags',
);
const blogTags = await getBlogTags(siteDir, {
postsPerPage: 'ALL',
});
expect(blogTags).toMatchSnapshot();
});
});

View file

@ -157,8 +157,9 @@ function formatBlogPostDate(locale: string, date: Date): string {
year: 'numeric',
timeZone: 'UTC',
}).format(date);
} catch (e) {
throw new Error(`Can't format blog post date "${date}"`);
} catch (err) {
logger.error`Can't format blog post date "${String(date)}"`;
throw err;
}
}
@ -172,12 +173,9 @@ async function parseBlogPostMarkdownFile(blogSourceAbsolute: string) {
...result,
frontMatter: validateBlogPostFrontMatter(result.frontMatter),
};
} catch (e) {
throw new Error(
`Error while parsing blog post file ${blogSourceAbsolute}: "${
(e as Error).message
}".`,
);
} catch (err) {
logger.error`Error while parsing blog post file path=${blogSourceAbsolute}.`;
throw err;
}
}
@ -246,8 +244,8 @@ async function processBlogSourceFile(
includeAuthor: false,
});
return result.date;
} catch (e) {
logger.error(e);
} catch (err) {
logger.error(err);
return (await fs.stat(blogSourceAbsolute)).birthtime;
}
}
@ -357,9 +355,9 @@ export async function generateBlogPosts(
options,
authorsMap,
);
} catch (e) {
logger.error`Processing of blog source file failed for path path=${blogSourceFile}.`;
throw e;
} catch (err) {
logger.error`Processing of blog source file path=${blogSourceFile} failed.`;
throw err;
}
}),
)