refactor(v2): blog/docs: add more context in error messages (#4989)

* refactor(v2): blog/docs: add more context in error messages

* more error handling
This commit is contained in:
Sébastien Lorber 2021-06-16 20:16:55 +02:00 committed by GitHub
parent b54ec72389
commit 1b0acc5547
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 178 additions and 116 deletions

View file

@ -55,6 +55,19 @@ function toUrl({date, link}: DateLink) {
.replace(/-/g, '/')}/${link}`; .replace(/-/g, '/')}/${link}`;
} }
function formatBlogPostDate(locale: string, date: Date): string {
try {
return new Intl.DateTimeFormat(locale, {
day: 'numeric',
month: 'long',
year: 'numeric',
timeZone: 'UTC',
}).format(date);
} catch (e) {
throw new Error(`Can't format blog post date "${date}"`);
}
}
export async function generateBlogFeed( export async function generateBlogFeed(
contentPaths: BlogContentPaths, contentPaths: BlogContentPaths,
context: LoadContext, context: LoadContext,
@ -131,8 +144,7 @@ export async function generateBlogPosts(
const blogPosts: BlogPost[] = []; const blogPosts: BlogPost[] = [];
await Promise.all( async function processBlogSourceFile(blogSourceFile: string) {
blogSourceFiles.map(async (blogSourceFile: string) => {
// Lookup in localized folder in priority // Lookup in localized folder in priority
const blogDirPath = await getFolderContainingFile( const blogDirPath = await getFolderContainingFile(
getContentPathList(contentPaths), getContentPathList(contentPaths),
@ -184,12 +196,7 @@ export async function generateBlogPosts(
// Use file create time for blog. // Use file create time for blog.
date = date ?? (await fs.stat(source)).birthtime; date = date ?? (await fs.stat(source)).birthtime;
const formattedDate = new Intl.DateTimeFormat(i18n.currentLocale, { const formattedDate = formatBlogPostDate(i18n.currentLocale, date);
day: 'numeric',
month: 'long',
year: 'numeric',
timeZone: 'UTC',
}).format(date);
const title = frontMatter.title ?? contentTitle ?? linkName; const title = frontMatter.title ?? contentTitle ?? linkName;
const description = frontMatter.description ?? excerpt ?? ''; const description = frontMatter.description ?? excerpt ?? '';
@ -201,10 +208,7 @@ export async function generateBlogPosts(
const permalink = normalizeUrl([baseUrl, routeBasePath, slug]); const permalink = normalizeUrl([baseUrl, routeBasePath, slug]);
function getBlogEditUrl() { function getBlogEditUrl() {
const blogPathRelative = path.relative( const blogPathRelative = path.relative(blogDirPath, path.resolve(source));
blogDirPath,
path.resolve(source),
);
if (typeof editUrl === 'function') { if (typeof editUrl === 'function') {
return editUrl({ return editUrl({
@ -242,12 +246,24 @@ export async function generateBlogPosts(
date, date,
formattedDate, formattedDate,
tags: frontMatter.tags ?? [], tags: frontMatter.tags ?? [],
readingTime: showReadingTime readingTime: showReadingTime ? readingTime(content).minutes : undefined,
? readingTime(content).minutes
: undefined,
truncated: truncateMarker?.test(content) || false, truncated: truncateMarker?.test(content) || false,
}, },
}); });
}
await Promise.all(
blogSourceFiles.map(async (blogSourceFile: string) => {
try {
return await processBlogSourceFile(blogSourceFile);
} catch (e) {
console.error(
chalk.red(
`Processing of blog source file failed for path "${blogSourceFile}"`,
),
);
throw e;
}
}), }),
); );

View file

@ -32,6 +32,7 @@ import globby from 'globby';
import {getDocsDirPaths} from './versions'; import {getDocsDirPaths} from './versions';
import {stripPathNumberPrefixes} from './numberPrefix'; import {stripPathNumberPrefixes} from './numberPrefix';
import {validateDocFrontMatter} from './docFrontMatter'; import {validateDocFrontMatter} from './docFrontMatter';
import chalk from 'chalk';
type LastUpdateOptions = Pick< type LastUpdateOptions = Pick<
PluginOptions, PluginOptions,
@ -102,7 +103,7 @@ export async function readVersionDocs(
); );
} }
export function processDocMetadata({ function doProcessDocMetadata({
docFile, docFile,
versionMetadata, versionMetadata,
context, context,
@ -262,3 +263,21 @@ export function processDocMetadata({
frontMatter, frontMatter,
}; };
} }
export function processDocMetadata(args: {
docFile: DocFile;
versionMetadata: VersionMetadata;
context: LoadContext;
options: MetadataOptions;
}): DocMetadataBase {
try {
return doProcessDocMetadata(args);
} catch (e) {
console.error(
chalk.red(
`Can't process doc metadatas for doc at path "${args.docFile.filePath}" in version "${args.versionMetadata.versionName}"`,
),
);
throw e;
}
}

View file

@ -49,6 +49,7 @@ import {
getLoadedContentTranslationFiles, getLoadedContentTranslationFiles,
} from './translations'; } from './translations';
import {CategoryMetadataFilenamePattern} from './sidebarItemsGenerator'; import {CategoryMetadataFilenamePattern} from './sidebarItemsGenerator';
import chalk from 'chalk';
export default function pluginContentDocs( export default function pluginContentDocs(
context: LoadContext, context: LoadContext,
@ -163,7 +164,7 @@ export default function pluginContentDocs(
return Promise.all(docFiles.map(processVersionDoc)); return Promise.all(docFiles.map(processVersionDoc));
} }
async function loadVersion( async function doLoadVersion(
versionMetadata: VersionMetadata, versionMetadata: VersionMetadata,
): Promise<LoadedVersion> { ): Promise<LoadedVersion> {
const unprocessedSidebars = loadSidebars( const unprocessedSidebars = loadSidebars(
@ -267,6 +268,19 @@ export default function pluginContentDocs(
}; };
} }
async function loadVersion(versionMetadata: VersionMetadata) {
try {
return await doLoadVersion(versionMetadata);
} catch (e) {
console.error(
chalk.red(
`Loading of version failed for version "${versionMetadata.versionName}"`,
),
);
throw e;
}
}
return { return {
loadedVersions: await Promise.all(versionsMetadata.map(loadVersion)), loadedVersions: await Promise.all(versionsMetadata.map(loadVersion)),
}; };
@ -307,7 +321,7 @@ export default function pluginContentDocs(
return routes.sort((a, b) => a.path.localeCompare(b.path)); return routes.sort((a, b) => a.path.localeCompare(b.path));
}; };
async function handleVersion(loadedVersion: LoadedVersion) { async function doCreateVersionRoutes(loadedVersion: LoadedVersion) {
const versionMetadataPropPath = await createData( const versionMetadataPropPath = await createData(
`${docuHash( `${docuHash(
`version-${loadedVersion.versionName}-metadata-prop`, `version-${loadedVersion.versionName}-metadata-prop`,
@ -334,7 +348,20 @@ export default function pluginContentDocs(
}); });
} }
await Promise.all(loadedVersions.map(handleVersion)); async function createVersionRoutes(loadedVersion: LoadedVersion) {
try {
return await doCreateVersionRoutes(loadedVersion);
} catch (e) {
console.error(
chalk.red(
`Can't create version routes for version "${loadedVersion.versionName}"`,
),
);
throw e;
}
}
await Promise.all(loadedVersions.map(createVersionRoutes));
setGlobalData<GlobalPluginData>({ setGlobalData<GlobalPluginData>({
path: normalizeUrl([baseUrl, options.routeBasePath]), path: normalizeUrl([baseUrl, options.routeBasePath]),