Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit b46cb5c

Browse files
authored
[BUG] Ring commands do not have error chain when ring name is missing (#531)
* [BUG] Ring commands do not have error chain when ring name is missing * Update set-default.ts * skip ring name in error message * add tests
1 parent 87431bf commit b46cb5c

File tree

6 files changed

+51
-48
lines changed

6 files changed

+51
-48
lines changed

src/commands/ring/create.ts

+7-17
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,14 @@ export const execute = async (
5454
projectPath: string,
5555
exitFn: (status: number) => Promise<void>
5656
): Promise<void> => {
57-
if (!hasValue(ringName)) {
58-
logError(
59-
buildError(
57+
try {
58+
if (!hasValue(ringName)) {
59+
throw buildError(
6060
errorStatusCode.VALIDATION_ERR,
6161
"ring-create-cmd-err-name-missing"
62-
)
63-
);
64-
await exitFn(1);
65-
return;
66-
}
62+
);
63+
}
6764

68-
try {
6965
logger.info(`Project path: ${projectPath}`);
7066

7167
dns.assertIsValid("<ring-name>", ringName);
@@ -91,14 +87,8 @@ export const execute = async (
9187
await exitFn(0);
9288
} catch (err) {
9389
logError(
94-
buildError(
95-
errorStatusCode.CMD_EXE_ERR,
96-
{
97-
errorKey: "ring-create-cmd-failed",
98-
values: [ringName],
99-
},
100-
err
101-
)
90+
// cannot include ring name in error message because it may not be defined.
91+
buildError(errorStatusCode.CMD_EXE_ERR, "ring-create-cmd-failed", err)
10292
);
10393
await exitFn(1);
10494
}

src/commands/ring/delete.test.ts

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ describe("checkDependencies", () => {
3434
});
3535

3636
describe("test execute function and logic", () => {
37+
it("test execute function: missing ring input", async () => {
38+
const exitFn = jest.fn();
39+
await execute("", "someprojectpath", exitFn);
40+
expect(exitFn).toBeCalledTimes(1);
41+
expect(exitFn.mock.calls).toEqual([[1]]);
42+
});
43+
3744
it("test execute function: missing project path", async () => {
3845
const exitFn = jest.fn();
3946
await execute("ring", "", exitFn);

src/commands/ring/delete.ts

+9-13
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ export const execute = async (
3434
projectPath: string,
3535
exitFn: (status: number) => Promise<void>
3636
): Promise<void> => {
37-
if (!hasValue(ringName)) {
38-
await exitFn(1);
39-
return;
40-
}
41-
4237
try {
38+
if (!hasValue(ringName)) {
39+
throw buildError(
40+
errorStatusCode.VALIDATION_ERR,
41+
"ring-delete-cmd-err-name-missing"
42+
);
43+
}
44+
4345
logger.info(`Project path: ${projectPath}`);
4446

4547
// Check if bedrock config exists, if not, warn and exit
@@ -65,14 +67,8 @@ export const execute = async (
6567
await exitFn(0);
6668
} catch (err) {
6769
logError(
68-
buildError(
69-
errorStatusCode.EXE_FLOW_ERR,
70-
{
71-
errorKey: "ring-delete-cmd-failed",
72-
values: [ringName],
73-
},
74-
err
75-
)
70+
// cannot include ring name in error message because it may not be defined.
71+
buildError(errorStatusCode.EXE_FLOW_ERR, "ring-delete-cmd-failed", err)
7672
);
7773
await exitFn(1);
7874
}

src/commands/ring/set-default.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ describe("test valid function", () => {
2424
});
2525

2626
describe("test execute function and logic", () => {
27+
it("test execute function: missing ring input", async () => {
28+
const exitFn = jest.fn();
29+
await execute("", "someprojectpath", exitFn);
30+
expect(exitFn).toBeCalledTimes(1);
31+
expect(exitFn.mock.calls).toEqual([[1]]);
32+
});
2733
it("test execute function: missing project path", async () => {
2834
const exitFn = jest.fn();
2935
await execute("ring", "", exitFn);

src/commands/ring/set-default.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ export const execute = async (
3737
projectPath: string,
3838
exitFn: (status: number) => Promise<void>
3939
): Promise<void> => {
40-
if (!hasValue(ringName)) {
41-
await exitFn(1);
42-
return;
43-
}
44-
4540
try {
41+
if (!hasValue(ringName)) {
42+
throw buildError(
43+
errorStatusCode.VALIDATION_ERR,
44+
"ring-set-default-cmd-err-name-missing"
45+
);
46+
}
47+
4648
logger.info(`Project path: ${projectPath}`);
4749

4850
checkDependencies(projectPath);
@@ -55,12 +57,10 @@ export const execute = async (
5557
await exitFn(0);
5658
} catch (err) {
5759
logError(
60+
// cannot include ring name in error message because it may not be defined.
5861
buildError(
5962
errorStatusCode.EXE_FLOW_ERR,
60-
{
61-
errorKey: "ring-set-default-cmd-failed",
62-
values: [ringName],
63-
},
63+
"ring-set-default-cmd-failed",
6464
err
6565
)
6666
);

src/lib/i18n.json

+13-9
Original file line numberDiff line numberDiff line change
@@ -222,16 +222,20 @@
222222
"var-group-add-with-key-vault-err-missing-provider": "Could not add variable group with key vault because Azure KeyVault provider data was not configured",
223223
"var-group-create-data-err": "Could not create variable group data.",
224224

225-
"ring-create-cmd-failed": "Error occurred while creating ring: {0}",
225+
"ring-create-cmd-failed": "Could not create ring: {0}.",
226+
"ring-create-cmd-err-name-missing": "Could not execute this command because ring name was not provided. Provide a ring name.",
226227
"ring-create-cmd-err-ring-exists": "Could not create ring {0} in project {1} because it already exists. Provide a different name.",
227-
"ring-create-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
228-
"ring-delete-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
229-
"ring-set-default-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
230-
"ring-create-cmd-err-name-missing": "No ring name was provided. Provide a ring name",
231-
"ring-delete-cmd-failed": "Error occurred while deleting ring: {0}.",
232-
"ring-set-default-cmd-failed": "Error occurred while setting default ring: {0}",
233-
234-
"variable-group-create-cmd-failed": "Error occurred while creating variable group.",
228+
"ring-create-cmd-err-dependency": "Configuration information was missing. Run `spk project init` command to initialize them.",
229+
230+
"ring-set-default-cmd-failed": "Could not set default ring: {0}.",
231+
"ring-set-default-cmd-err-name-missing": "Could not execute this command because ring name was not provided. Provide a ring name.",
232+
"ring-set-default-cmd-err-dependency": "Configuration information was missing. Run `spk project init` command to initialize them.",
233+
234+
"ring-delete-cmd-failed": "Could not delete ring: {0}.",
235+
"ring-delete-cmd-err-name-missing": "Could not execute this command because ring name was not provided. Provide a ring name.",
236+
"ring-delete-cmd-err-dependency": "Configuration information was missing. Run `spk project init` command to initialize them.",
237+
238+
"variable-group-create-cmd-failed": "Could not create variable group.",
235239
"variable-group-create-cmd-err-create": "Could not load variable group. The variable group type '{0}' is not supported. Only 'Vsts' and 'AzureKeyVault' are valid types and case sensitive.",
236240
"variable-group-create-cmd-err-access-token": "Provide a value for {0}.",
237241
"variable-group-create-cmd-err-project-missing": "Provide a value for {0}.",

0 commit comments

Comments
 (0)