refactor: normalize error logging (#7370)

This commit is contained in:
Joshua Chen 2022-05-08 13:40:34 +08:00 committed by GitHub
parent 87c7639a52
commit 7c9892888d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 27 additions and 30 deletions

View file

@ -142,7 +142,7 @@ function getRealPath(pathname: string) {
// eslint-disable-next-line no-restricted-properties // eslint-disable-next-line no-restricted-properties
const realPath = fs.realpathSync(pathname); const realPath = fs.realpathSync(pathname);
return realPath; return realPath;
} catch (error) { } catch (err) {
return pathname; return pathname;
} }
} }

View file

@ -199,8 +199,10 @@ describe('writeRedirectFiles', () => {
'file already exists!', 'file already exists!',
); );
await expect(writeRedirectFiles(filesMetadata)).rejects.toThrowError( await expect(
`Redirect file creation error for "${filesMetadata[0].fileAbsolutePath}" path: Error: The redirect plugin is not supposed to override existing files.`, writeRedirectFiles(filesMetadata),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The redirect plugin is not supposed to override existing files."`,
); );
}); });
}); });

View file

@ -12,6 +12,7 @@ import _ from 'lodash';
import type {PluginContext, RedirectMetadata} from './types'; import type {PluginContext, RedirectMetadata} from './types';
import createRedirectPageContent from './createRedirectPageContent'; import createRedirectPageContent from './createRedirectPageContent';
import {normalizeUrl} from '@docusaurus/utils'; import {normalizeUrl} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>; export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
@ -100,9 +101,8 @@ export async function writeRedirectFile(
{flag: 'wx'}, {flag: 'wx'},
); );
} catch (err) { } catch (err) {
throw new Error( logger.error`Redirect file creation error for path=${file.fileAbsolutePath}.`;
`Redirect file creation error for "${file.fileAbsolutePath}" path: ${err}.`, throw err;
);
} }
} }

View file

@ -18,6 +18,7 @@ import type {
BlogPost, BlogPost,
} from '@docusaurus/plugin-content-blog'; } from '@docusaurus/plugin-content-blog';
import {blogPostContainerID} from '@docusaurus/utils-common'; import {blogPostContainerID} from '@docusaurus/utils-common';
import logger from '@docusaurus/logger';
async function generateBlogFeed({ async function generateBlogFeed({
blogPosts, blogPosts,
@ -127,7 +128,8 @@ async function createBlogFeedFile({
try { try {
await fs.outputFile(path.join(generatePath, feedPath), feedContent); await fs.outputFile(path.join(generatePath, feedPath), feedContent);
} catch (err) { } catch (err) {
throw new Error(`Generating ${feedType} feed failed: ${err}.`); logger.error(`Generating ${feedType} feed failed.`);
throw err;
} }
} }

View file

@ -64,9 +64,9 @@ export async function cliDocsVersionCommand(
try { try {
validateVersionName(version); validateVersionName(version);
} catch (e) { } catch (err) {
logger.info`${pluginIdLogPrefix}: Invalid version name provided. Try something like: 1.0.0`; logger.info`${pluginIdLogPrefix}: Invalid version name provided. Try something like: 1.0.0`;
throw e; throw err;
} }
// Load existing versions. // Load existing versions.

View file

@ -19,6 +19,7 @@
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@docusaurus/core": "2.0.0-beta.20", "@docusaurus/core": "2.0.0-beta.20",
"@docusaurus/logger": "2.0.0-beta.20",
"@docusaurus/utils": "2.0.0-beta.20", "@docusaurus/utils": "2.0.0-beta.20",
"@docusaurus/utils-common": "2.0.0-beta.20", "@docusaurus/utils-common": "2.0.0-beta.20",
"@docusaurus/utils-validation": "2.0.0-beta.20", "@docusaurus/utils-validation": "2.0.0-beta.20",

View file

@ -7,6 +7,7 @@
import fs from 'fs-extra'; import fs from 'fs-extra';
import path from 'path'; import path from 'path';
import logger from '@docusaurus/logger';
import createSitemap from './createSitemap'; import createSitemap from './createSitemap';
import type {PluginOptions, Options} from './options'; import type {PluginOptions, Options} from './options';
import type {LoadContext, Plugin} from '@docusaurus/types'; import type {LoadContext, Plugin} from '@docusaurus/types';
@ -35,7 +36,8 @@ export default function pluginSitemap(
try { try {
await fs.outputFile(sitemapPath, generatedSitemap); await fs.outputFile(sitemapPath, generatedSitemap);
} catch (err) { } catch (err) {
throw new Error(`Writing sitemap failed: ${err}`); logger.error('Writing sitemap failed.');
throw err;
} }
}, },
}; };

View file

@ -10,6 +10,7 @@ import type {SwizzleComponentConfig, SwizzleConfig} from '@docusaurus/types';
import type {SwizzlePlugin} from './common'; import type {SwizzlePlugin} from './common';
import {SwizzleActions, SwizzleActionsStatuses} from './common'; import {SwizzleActions, SwizzleActionsStatuses} from './common';
import {getPluginByThemeName} from './themes'; import {getPluginByThemeName} from './themes';
import logger from '@docusaurus/logger';
function getModuleSwizzleConfig( function getModuleSwizzleConfig(
swizzlePlugin: SwizzlePlugin, swizzlePlugin: SwizzlePlugin,
@ -103,12 +104,9 @@ export function getThemeSwizzleConfig(
if (config) { if (config) {
try { try {
return normalizeSwizzleConfig(config); return normalizeSwizzleConfig(config);
} catch (e) { } catch (err) {
throw new Error( logger.error`Invalid Swizzle config for theme name=${themeName}.`;
`Invalid Swizzle config for theme ${themeName}.\n${ throw err;
(e as Error).message
}`,
);
} }
} }
return FallbackSwizzleConfig; return FallbackSwizzleConfig;

View file

@ -343,17 +343,13 @@ describe('getHttpsConfig', () => {
process.env.HTTPS = 'true'; process.env.HTTPS = 'true';
process.env.SSL_CRT_FILE = path.join(__dirname, '__fixtures__/host.crt'); process.env.SSL_CRT_FILE = path.join(__dirname, '__fixtures__/host.crt');
process.env.SSL_KEY_FILE = path.join(__dirname, '__fixtures__/invalid.key'); process.env.SSL_KEY_FILE = path.join(__dirname, '__fixtures__/invalid.key');
await expect(getHttpsConfig()).rejects.toThrowError( await expect(getHttpsConfig()).rejects.toThrowError();
/The certificate key .*[/\\]__fixtures__[/\\]invalid\.key is invalid/,
);
}); });
it('throws for invalid cert', async () => { it('throws for invalid cert', async () => {
process.env.HTTPS = 'true'; process.env.HTTPS = 'true';
process.env.SSL_CRT_FILE = path.join(__dirname, '__fixtures__/invalid.crt'); process.env.SSL_CRT_FILE = path.join(__dirname, '__fixtures__/invalid.crt');
process.env.SSL_KEY_FILE = path.join(__dirname, '__fixtures__/host.key'); process.env.SSL_KEY_FILE = path.join(__dirname, '__fixtures__/host.key');
await expect(getHttpsConfig()).rejects.toThrowError( await expect(getHttpsConfig()).rejects.toThrowError();
/The certificate .*[/\\]__fixtures__[/\\]invalid\.crt is invalid/,
);
}); });
}); });

View file

@ -291,20 +291,16 @@ function validateKeyAndCerts({
// publicEncrypt will throw an error with an invalid cert // publicEncrypt will throw an error with an invalid cert
encrypted = crypto.publicEncrypt(cert, Buffer.from('test')); encrypted = crypto.publicEncrypt(cert, Buffer.from('test'));
} catch (err) { } catch (err) {
throw new Error( logger.error`The certificate path=${crtFile} is invalid.`;
`The certificate ${crtFile} is invalid. throw err;
${err}`,
);
} }
try { try {
// privateDecrypt will throw an error with an invalid key // privateDecrypt will throw an error with an invalid key
crypto.privateDecrypt(key, encrypted); crypto.privateDecrypt(key, encrypted);
} catch (err) { } catch (err) {
throw new Error( logger.error`The certificate key path=${keyFile} is invalid.`;
`The certificate key ${keyFile} is invalid. throw err;
${err}`,
);
} }
} }