Skip to content

Commit

Permalink
fix: error keys support createError
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Mar 2, 2022
1 parent 5d917bf commit f094c49
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 19 deletions.
8 changes: 4 additions & 4 deletions messages/messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ apiVersion configuration overridden at %s

API versions up to %s are deprecated. See %s for more information.

# flags.apiVersion.errors.InvalidApiVersion
# errors.InvalidApiVersion

%s is not a valid API version.

# flags.apiVersion.errors.RetiredApiVersion
# errors.RetiredApiVersion

The API version must be greater than %s.

# flags.duration.errors.InvalidInput
# errors.InvalidDuration

The value must be an integer.

# flags.duration.errors.DurationBounds
# errors.DurationBounds

The value must be between %s and %s (inclusive).
8 changes: 4 additions & 4 deletions src/flags/duration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ const validate = (input: string, config: DurationFlagConfig): Duration => {
try {
parsedInput = parseInt(input, 10);
if (typeof parsedInput !== 'number' || isNaN(parsedInput)) {
throw messages.createError('flags.duration.errors.InvalidInput');
throw messages.createError('errors.InvalidDuration');
}
} catch (e) {
throw messages.createError('flags.duration.errors.InvalidInput');
throw messages.createError('errors.InvalidDuration');
}

if (min && parsedInput < min) {
throw messages.createError('flags.duration.errors.DurationBounds', [min, max]);
throw messages.createError('errors.DurationBounds', [min, max]);
}
if (max && parsedInput > max) {
throw messages.createError('flags.duration.errors.DurationBounds', [min, max]);
throw messages.createError('errors.DurationBounds', [min, max]);
}
return toDuration(parsedInput, unit);
};
Expand Down
4 changes: 2 additions & 2 deletions src/flags/orgApiVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ const getDefaultFromConfig = async (): Promise<string | undefined> => {
const validate = async (input: string): Promise<string> => {
// basic format check
if (!sfdc.validateApiVersion(input)) {
throw messages.createError('flags.apiVersion.errors.InvalidApiVersion', [input]);
throw messages.createError('errors.InvalidApiVersion', [input]);
}
const requestedVersion = parseInt(input, 10);
if (requestedVersion < minValidApiVersion) {
throw messages.createError('flags.apiVersion.errors.RetiredApiVersion', [minValidApiVersion]);
throw messages.createError('errors.RetiredApiVersion', [minValidApiVersion]);
}
if (requestedVersion <= maxDeprecated) {
await Lifecycle.getInstance().emitWarning(
Expand Down
3 changes: 2 additions & 1 deletion src/flags/orgFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const getHubOrThrow = async (aliasOrUsername?: string): Promise<Org> => {
if (!aliasOrUsername) {
// check config for a default
const config = await ConfigAggregator.create();
aliasOrUsername = config.getInfo('defaultdevhubusername')?.value as string;
aliasOrUsername =
(config.getInfo('target-dev-hub')?.value as string) ?? (config.getInfo('defaultdevhubusername')?.value as string);
if (!aliasOrUsername) {
throw messages.createError('errors.NoDefaultDevHub');
}
Expand Down
2 changes: 1 addition & 1 deletion src/flags/salesforceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Messages, sfdc } from '@salesforce/core';
Messages.importMessagesDirectory(__dirname);
const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages');

interface IdFlagConfig extends Partial<Interfaces.OptionFlagProps> {
export interface IdFlagConfig extends Partial<Interfaces.OptionFlagProps> {
/**
* Can specify if the version must be 15 or 18 characters long. Leave blank to allow either 15 or 18.
*/
Expand Down
6 changes: 2 additions & 4 deletions test/unit/flags/apiVersion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('fs flags', () => {
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.equal(messages.getMessage('flags.apiVersion.errors.InvalidApiVersion', ['foo']));
expect(error.message).to.equal(messages.getMessage('errors.InvalidApiVersion', ['foo']));
}
});

Expand All @@ -73,9 +73,7 @@ describe('fs flags', () => {
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.equal(
messages.getMessage('flags.apiVersion.errors.RetiredApiVersion', [minValidApiVersion])
);
expect(error.message).to.equal(messages.getMessage('errors.RetiredApiVersion', [minValidApiVersion]));
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/unit/flags/duration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('duration flag', () => {
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.equal(messages.getMessage('flags.duration.errors.DurationBounds', [1, 60]));
expect(error.message).to.equal(messages.getMessage('errors.DurationBounds', [1, 60]));
}
});
it('above max fails', async () => {
Expand All @@ -108,7 +108,7 @@ describe('duration flag', () => {
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.equal(messages.getMessage('flags.duration.errors.DurationBounds', [1, 60]));
expect(error.message).to.equal(messages.getMessage('errors.DurationBounds', [1, 60]));
}
});
it('invalid input', async () => {
Expand All @@ -117,7 +117,7 @@ describe('duration flag', () => {
throw new Error(`Should have thrown an error ${JSON.stringify(out)}`);
} catch (err) {
const error = err as Error;
expect(error.message).to.equal(messages.getMessage('flags.duration.errors.InvalidInput'));
expect(error.message).to.equal(messages.getMessage('errors.InvalidInput'));
}
});
});
Expand Down

0 comments on commit f094c49

Please sign in to comment.