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

@ -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;
}
}