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
const realPath = fs.realpathSync(pathname);
return realPath;
} catch (error) {
} catch (err) {
return pathname;
}
}

View file

@ -199,8 +199,10 @@ describe('writeRedirectFiles', () => {
'file already exists!',
);
await expect(writeRedirectFiles(filesMetadata)).rejects.toThrowError(
`Redirect file creation error for "${filesMetadata[0].fileAbsolutePath}" path: Error: The redirect plugin is not supposed to override existing files.`,
await expect(
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 createRedirectPageContent from './createRedirectPageContent';
import {normalizeUrl} from '@docusaurus/utils';
import logger from '@docusaurus/logger';
export type WriteFilesPluginContext = Pick<PluginContext, 'baseUrl' | 'outDir'>;
@ -100,9 +101,8 @@ export async function writeRedirectFile(
{flag: 'wx'},
);
} catch (err) {
throw new Error(
`Redirect file creation error for "${file.fileAbsolutePath}" path: ${err}.`,
);
logger.error`Redirect file creation error for path=${file.fileAbsolutePath}.`;
throw err;
}
}

View file

@ -18,6 +18,7 @@ import type {
BlogPost,
} from '@docusaurus/plugin-content-blog';
import {blogPostContainerID} from '@docusaurus/utils-common';
import logger from '@docusaurus/logger';
async function generateBlogFeed({
blogPosts,
@ -127,7 +128,8 @@ async function createBlogFeedFile({
try {
await fs.outputFile(path.join(generatePath, feedPath), feedContent);
} 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 {
validateVersionName(version);
} catch (e) {
} catch (err) {
logger.info`${pluginIdLogPrefix}: Invalid version name provided. Try something like: 1.0.0`;
throw e;
throw err;
}
// Load existing versions.

View file

@ -19,6 +19,7 @@
"license": "MIT",
"dependencies": {
"@docusaurus/core": "2.0.0-beta.20",
"@docusaurus/logger": "2.0.0-beta.20",
"@docusaurus/utils": "2.0.0-beta.20",
"@docusaurus/utils-common": "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 path from 'path';
import logger from '@docusaurus/logger';
import createSitemap from './createSitemap';
import type {PluginOptions, Options} from './options';
import type {LoadContext, Plugin} from '@docusaurus/types';
@ -35,7 +36,8 @@ export default function pluginSitemap(
try {
await fs.outputFile(sitemapPath, generatedSitemap);
} 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 {SwizzleActions, SwizzleActionsStatuses} from './common';
import {getPluginByThemeName} from './themes';
import logger from '@docusaurus/logger';
function getModuleSwizzleConfig(
swizzlePlugin: SwizzlePlugin,
@ -103,12 +104,9 @@ export function getThemeSwizzleConfig(
if (config) {
try {
return normalizeSwizzleConfig(config);
} catch (e) {
throw new Error(
`Invalid Swizzle config for theme ${themeName}.\n${
(e as Error).message
}`,
);
} catch (err) {
logger.error`Invalid Swizzle config for theme name=${themeName}.`;
throw err;
}
}
return FallbackSwizzleConfig;

View file

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

View file

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