Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike to clean up the lang file #59

Merged
merged 10 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 57 additions & 32 deletions config/CLIConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,8 @@ class CLIConfiguration {
validate(
logCallbacks?: LogCallbacksArg<typeof validateLogCallbackKeys>
): boolean {
const validateLogger = makeTypedLogger<typeof validateLogCallbackKeys>(
logCallbacks,
'config.cliConfiguration.validate'
);
const validateLogger =
makeTypedLogger<typeof validateLogCallbackKeys>(logCallbacks);

if (!this.config) {
validateLogger('noConfig');
Expand All @@ -154,22 +152,34 @@ class CLIConfiguration {
return false;
}
if (accountIdsMap[accountConfig.accountId]) {
validateLogger('duplicateAccountIds', {
accountId: accountConfig.accountId,
});
validateLogger(
'duplicateAccountIds',
`${i18nKey}.validate.duplicateAccountIds`,
{
accountId: accountConfig.accountId,
}
);
return false;
}
if (accountConfig.name) {
if (accountNamesMap[accountConfig.name]) {
validateLogger('duplicateAccountNames', {
accountName: accountConfig.name,
});
validateLogger(
'duplicateAccountNames',
`${i18nKey}.validate.duplicateAccountNames`,
{
accountName: accountConfig.name,
}
);
return false;
}
if (/\s+/.test(accountConfig.name)) {
validateLogger('nameContainsSpaces', {
accountName: accountConfig.name,
});
validateLogger(
'nameContainsSpaces',
`${i18nKey}.validate.nameContainsSpaces`,
{
accountName: accountConfig.name,
}
);
return false;
}
accountNamesMap[accountConfig.name] = true;
Expand Down Expand Up @@ -315,7 +325,9 @@ class CLIConfiguration {
} = updatedAccountFields;

if (!accountId) {
throwErrorWithMessage(`${i18nKey}.updateAccount`);
throwErrorWithMessage(
`${i18nKey}.updateAccount.errors.accountIdRequired`
);
}
if (!this.config) {
debug(`${i18nKey}.updateAccount.noConfigToUpdate`);
Expand Down Expand Up @@ -402,13 +414,15 @@ class CLIConfiguration {
*/
updateDefaultAccount(defaultAccount: string | number): CLIConfig_NEW | null {
if (!this.config) {
throwErrorWithMessage(`${i18nKey}.noConfigLoaded`);
throwErrorWithMessage(`${i18nKey}.errors.noConfigLoaded`);
}
if (
!defaultAccount ||
(typeof defaultAccount !== 'number' && typeof defaultAccount !== 'string')
) {
throwErrorWithMessage(`${i18nKey}.updateDefaultAccount`);
throwErrorWithMessage(
`${i18nKey}.updateDefaultAccount.errors.invalidInput`
);
}

this.config.defaultAccount = defaultAccount;
Expand All @@ -420,7 +434,7 @@ class CLIConfiguration {
*/
renameAccount(currentName: string, newName: string): void {
if (!this.config) {
throwErrorWithMessage(`${i18nKey}.noConfigLoaded`);
throwErrorWithMessage(`${i18nKey}.errors.noConfigLoaded`);
}
const accountId = this.getAccountId(currentName);
let accountConfigToRename: CLIAccount_NEW | null = null;
Expand All @@ -430,7 +444,9 @@ class CLIConfiguration {
}

if (!accountConfigToRename) {
throwErrorWithMessage(`${i18nKey}.renameAccount`, { currentName });
throwErrorWithMessage(`${i18nKey}.renameAccount.errors.invalidName`, {
currentName,
});
}

if (accountId) {
Expand All @@ -447,19 +463,22 @@ class CLIConfiguration {
*/
removeAccountFromConfig(nameOrId: string | number): boolean {
if (!this.config) {
throwErrorWithMessage(`${i18nKey}.noConfigLoaded`);
throwErrorWithMessage(`${i18nKey}.errors.noConfigLoaded`);
}
const accountId = this.getAccountId(nameOrId);

if (!accountId) {
throwErrorWithMessage(`${i18nKey}.removeAccountFromConfig`, { nameOrId });
throwErrorWithMessage(
`${i18nKey}.removeAccountFromConfig.errors.invalidId`,
{ nameOrId }
);
}

let removedAccountIsDefault = false;
const accountConfig = this.getAccount(accountId);

if (accountConfig) {
debug(`${i18nKey}.removeAccountFromConfig`, { accountId });
debug(`${i18nKey}.removeAccountFromConfig.deleting`, { accountId });
const index = this.getConfigAccountIndex(accountId);
this.config.accounts.splice(index, 1);

Expand All @@ -478,11 +497,11 @@ class CLIConfiguration {
*/
updateDefaultMode(defaultMode: string): CLIConfig_NEW | null {
if (!this.config) {
throwErrorWithMessage(`${i18nKey}.noConfigLoaded`);
throwErrorWithMessage(`${i18nKey}.errors.noConfigLoaded`);
}
const ALL_MODES = Object.values(MODE);
if (!defaultMode || !ALL_MODES.find(m => m === defaultMode)) {
throwErrorWithMessage(`${i18nKey}.updateDefaultMode`, {
throwErrorWithMessage(`${i18nKey}.updateDefaultMode.errors.invalidMode`, {
defaultMode,
validModes: commaSeparatedValues(ALL_MODES),
});
Expand All @@ -497,14 +516,17 @@ class CLIConfiguration {
*/
updateHttpTimeout(timeout: string): CLIConfig_NEW | null {
if (!this.config) {
throwErrorWithMessage(`${i18nKey}.noConfigLoaded`);
throwErrorWithMessage(`${i18nKey}.errors.noConfigLoaded`);
}
const parsedTimeout = parseInt(timeout);
if (isNaN(parsedTimeout) || parsedTimeout < MIN_HTTP_TIMEOUT) {
throwErrorWithMessage(`${i18nKey}.updateHttpTimeout`, {
timeout,
minTimeout: MIN_HTTP_TIMEOUT,
});
throwErrorWithMessage(
`${i18nKey}.updateHttpTimeout.errors.invalidTimeout`,
{
timeout,
minTimeout: MIN_HTTP_TIMEOUT,
}
);
}

this.config.httpTimeout = parsedTimeout;
Expand All @@ -516,12 +538,15 @@ class CLIConfiguration {
*/
updateAllowUsageTracking(isEnabled: boolean): CLIConfig_NEW | null {
if (!this.config) {
throwErrorWithMessage(`${i18nKey}.noConfigLoaded`);
throwErrorWithMessage(`${i18nKey}.errors.noConfigLoaded`);
}
if (typeof isEnabled !== 'boolean') {
throwErrorWithMessage(`${i18nKey}.updateAllowUsageTracking`, {
isEnabled: `${isEnabled}`,
});
throwErrorWithMessage(
`${i18nKey}.updateAllowUsageTracking.errors.invalidInput`,
{
isEnabled: `${isEnabled}`,
}
);
}

this.config.allowUsageTracking = isEnabled;
Expand Down
3 changes: 1 addition & 2 deletions config/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function parseConfig(configSource: string): CLIConfig_NEW {
try {
parsed = yaml.load(configSource) as CLIConfig_NEW;
} catch (err) {
throwErrorWithMessage(`${i18nKey}.parsing`, {}, err as BaseError);
throwErrorWithMessage(`${i18nKey}.errors.parsing`, {}, err as BaseError);
}

return parsed;
Expand All @@ -88,7 +88,6 @@ export function loadConfigFromFile(): CLIConfig_NEW | null {
return parseConfig(source);
}

// TODO: Maybe use log callbacks here
debug(`${i18nKey}.errorLoading`, { configPath });

return null;
Expand Down
4 changes: 3 additions & 1 deletion config/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
PersonalAccessKeyAccount_NEW,
} from '../types/Accounts';

const i18nKey = 'config.configUtils';

export function getOrderedAccount(
unorderedAccount: CLIAccount_NEW
): CLIAccount_NEW {
Expand Down Expand Up @@ -142,7 +144,7 @@ export function generateConfig(
configAccount = generateOauthAccountConfig(options as OAuthOptions);
break;
default:
debug('config.configUtils.unknownType', { type });
debug(`${i18nKey}.unknownType`, { type });
return null;
}

Expand Down
8 changes: 5 additions & 3 deletions config/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import { generateConfig } from './configUtils';
import { getValidEnv } from '../lib/environment';

const i18nKey = 'config.environment';

type EnvironmentConfigVariables = {
apiKey?: string;
clientId?: string;
Expand Down Expand Up @@ -47,12 +49,12 @@ export function loadConfigFromEnvironment(): CLIConfig_NEW | null {
env,
} = getConfigVariablesFromEnv();
if (!accountId) {
debug('environment.loadConfig.missingAccountId');
debug(`${i18nKey}.loadConfig.missingAccountId`);
return null;
}

if (!env) {
debug('environment.loadConfig.missingEnv');
debug(`${i18nKey}.loadConfig.missingEnv`);
return null;
}

Expand All @@ -79,6 +81,6 @@ export function loadConfigFromEnvironment(): CLIConfig_NEW | null {
});
}

debug('environment.loadConfig.unknownAuthType');
debug(`${i18nKey}.loadConfig.unknownAuthType`);
return null;
}
12 changes: 9 additions & 3 deletions errors/__tests__/standardErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,27 @@ describe('standardErrors', () => {
describe('throwErrorWithMessage', () => {
it('throws error with message', () => {
const error = newError();
expect(() => throwErrorWithMessage('', {}, error)).toThrow();
expect(() =>
throwErrorWithMessage('errors.generic', {}, error)
).toThrow();
});
});

describe('throwTypeErrorWithMessage', () => {
it('throws type error with message', () => {
const error = newError();
expect(() => throwTypeErrorWithMessage('', {}, error)).toThrow();
expect(() =>
throwTypeErrorWithMessage('errors.generic', {}, error)
).toThrow();
});
});

describe('throwAuthErrorWithMessage', () => {
it('throws auth error with message', () => {
const error = newError() as StatusCodeError;
expect(() => throwAuthErrorWithMessage('', {}, error)).toThrow();
expect(() =>
throwAuthErrorWithMessage('errors.generic', {}, error)
).toThrow();
});
});

Expand Down
3 changes: 2 additions & 1 deletion errors/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { i18n } from '../utils/lang';
import { throwError } from './standardErrors';
import { HubSpotAuthError } from '../models/HubSpotAuthError';

const i18nKey = 'errors.apiErrors';

export function isApiStatusCodeError(err: GenericError): boolean {
return (
err.name === 'StatusCodeError' ||
Expand Down Expand Up @@ -121,7 +123,6 @@ export function throwApiStatusCodeError(
error: StatusCodeError,
context: StatusCodeErrorContext = {}
): never {
const i18nKey = 'errors.errorTypes.api';
const { status } = error;
const { method } = error.options || {};
const { projectName } = context;
Expand Down
2 changes: 1 addition & 1 deletion errors/fileSystemErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { i18n } from '../utils/lang';
import { isSystemError } from './standardErrors';
import { BaseError, FileSystemErrorContext } from '../types/Error';

const i18nKey = 'errors.errorTypes.fileSystem';
const i18nKey = 'errors.fileSystemErrors';

export function throwFileSystemError(
error: BaseError,
Expand Down
13 changes: 7 additions & 6 deletions errors/standardErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { i18n } from '../utils/lang';
import { throwStatusCodeError } from './apiErrors';

import { BaseError, StatusCodeError } from '../types/Error';
import { LangKey } from '../types/Lang';

export function isSystemError(err: BaseError): boolean {
return err.errno != null && err.code != null && err.syscall != null;
Expand All @@ -14,11 +15,11 @@ export function isFatalError(err: BaseError): boolean {

function genericThrowErrorWithMessage(
ErrorType: ErrorConstructor,
identifier: string,
identifier: LangKey,
interpolation?: { [key: string]: string | number },
cause?: BaseError
): never {
const message = i18n(`errors.${identifier}`, interpolation);
const message = i18n(identifier, interpolation);
if (cause) {
throw new ErrorType(message, { cause });
}
Expand All @@ -29,7 +30,7 @@ function genericThrowErrorWithMessage(
* @throws
*/
export function throwErrorWithMessage(
identifier: string,
identifier: LangKey,
interpolation?: { [key: string]: string | number },
cause?: BaseError
): never {
Expand All @@ -40,7 +41,7 @@ export function throwErrorWithMessage(
* @throws
*/
export function throwTypeErrorWithMessage(
identifier: string,
identifier: LangKey,
interpolation?: { [key: string]: string | number },
cause?: BaseError
): never {
Expand All @@ -51,7 +52,7 @@ export function throwTypeErrorWithMessage(
* @throws
*/
export function throwAuthErrorWithMessage(
identifier: string,
identifier: LangKey,
interpolation?: { [key: string]: string | number },
cause?: StatusCodeError
): never {
Expand All @@ -73,7 +74,7 @@ export function throwError(error: BaseError): never {
} else {
// Error or Error subclass
const name = error.name || 'Error';
const message = [i18n('errors.errorTypes.generic', { name })];
const message = [i18n('errors.generic', { name })];
[error.message, error.reason].forEach(msg => {
if (msg) {
message.push(msg);
Expand Down
Loading