Skip to content

Commit

Permalink
refactor: normalize error logging (#7370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena authored May 8, 2022
1 parent 87c7639 commit 7c98928
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion jest/snapshotPathNormalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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."`,
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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'>;

Expand Down Expand Up @@ -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;
}
}

Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion packages/docusaurus-plugin-sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
},
};
Expand Down
10 changes: 4 additions & 6 deletions packages/docusaurus/src/commands/swizzle/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions packages/docusaurus/src/webpack/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
12 changes: 4 additions & 8 deletions packages/docusaurus/src/webpack/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down

0 comments on commit 7c98928

Please sign in to comment.