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

Commit 0288bd6

Browse files
authored
Add error codes to create variable group (#506)
1 parent 343547d commit 0288bd6

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

src/commands/variable-group/create.ts

+31-18
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
import { logger } from "../../logger";
2222
import { VariableGroupData } from "../../types";
2323
import decorator from "./create.decorator.json";
24+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
25+
import { errorStatusCode } from "../../lib/errorStatusCode";
2426

2527
interface CommandOptions {
2628
file: string | undefined;
@@ -31,16 +33,20 @@ interface CommandOptions {
3133

3234
export const validateValues = (opts: CommandOptions): void => {
3335
if (!opts.file) {
34-
throw Error("You need to specify a file with variable group manifest");
36+
throw buildError(
37+
errorStatusCode.VALIDATION_ERR,
38+
"variable-group-create-cmd-err-file-missing"
39+
);
3540
}
3641
const config = Config();
3742
const azure = config.azure_devops;
3843

3944
if (!hasValue(opts.orgName) && !azure?.org) {
40-
throw Error(
45+
throw buildError(errorStatusCode.VALIDATION_ERR, {
46+
errorKey: "variable-group-create-cmd-err-org-missing",
4147
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
42-
`value for ${getCmdOption(decorator, "org-name")!.arg} is missing`
43-
);
48+
values: [getCmdOption(decorator, "org-name")!.arg],
49+
});
4450
}
4551

4652
if (hasValue(opts.orgName)) {
@@ -49,10 +55,11 @@ export const validateValues = (opts: CommandOptions): void => {
4955
}
5056

5157
if (!hasValue(opts.devopsProject) && !azure?.project) {
52-
throw Error(
58+
throw buildError(errorStatusCode.VALIDATION_ERR, {
59+
errorKey: "variable-group-create-cmd-err-project-missing",
5360
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
54-
`value for ${getCmdOption(decorator, "devops-project")!.arg} is missing`
55-
);
61+
values: [getCmdOption(decorator, "devops-project")!.arg],
62+
});
5663
}
5764

5865
if (hasValue(opts.devopsProject)) {
@@ -61,12 +68,11 @@ export const validateValues = (opts: CommandOptions): void => {
6168
}
6269

6370
if (!hasValue(opts.personalAccessToken) && !azure?.access_token) {
64-
throw Error(
65-
`value for ${
66-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
67-
getCmdOption(decorator, "personal-access-token")!.arg
68-
} is missing`
69-
);
71+
throw buildError(errorStatusCode.VALIDATION_ERR, {
72+
errorKey: "variable-group-create-cmd-err-access-token",
73+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
74+
values: [getCmdOption(decorator, "personal-access-token")!.arg],
75+
});
7076
}
7177
};
7278

@@ -95,8 +101,13 @@ export const commandDecorator = (command: commander.Command): void => {
95101
);
96102
await exitCmd(logger, process.exit, 0);
97103
} catch (err) {
98-
logger.error(`Error occurred while creating variable group`);
99-
logger.error(err);
104+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
105+
logError(
106+
buildError(
107+
errorStatusCode.CMD_EXE_ERR,
108+
"variable-group-create-cmd-failed"
109+
)
110+
);
100111
await exitCmd(logger, process.exit, 1);
101112
}
102113
});
@@ -126,8 +137,10 @@ export const create = async (
126137
} else if (data.type === "Vsts") {
127138
await addVariableGroup(data, accessOpts);
128139
} else {
129-
throw new Error(
130-
`Variable Group type "${data.type}" is not supported. Only "Vsts" and "AzureKeyVault" are valid types and case sensitive.`
131-
);
140+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
141+
throw buildError(errorStatusCode.EXE_FLOW_ERR, {
142+
errorKey: "variable-group-create-cmd-err-create",
143+
values: [data.type],
144+
});
132145
}
133146
};

src/lib/i18n.json

+7
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@
109109
"ring-delete-cmd-failed": "Error occurred while deleting ring: {0}.",
110110
"ring-set-default-cmd-failed": "Error occurred while setting default ring: {0}",
111111

112+
"variable-group-create-cmd-failed": "Error occurred while creating variable group.",
113+
"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.",
114+
"variable-group-create-cmd-err-access-token": "Provide a value for {0}.",
115+
"variable-group-create-cmd-err-project-missing": "Provide a value for {0}.",
116+
"variable-group-create-cmd-err-org-missing": "Provide a value for {0}.",
117+
"variable-group-create-cmd-err-file-missing": "Provide a file with the variable group manifest.",
118+
112119
"validation-err-org-name-missing": "Organization name was missing. Provide it.",
113120
"validation-err-org-name": "Organization name must start with a letter or number, followed by letters, numbers or hyphens, and must end with a letter or number.",
114121
"validation-err-password-missing": "Password was missing. Provide it.",

0 commit comments

Comments
 (0)