diff --git a/src/commands/variable-group/create.ts b/src/commands/variable-group/create.ts index 6f5e6e5ef..5a3d27d4b 100644 --- a/src/commands/variable-group/create.ts +++ b/src/commands/variable-group/create.ts @@ -21,6 +21,8 @@ import { import { logger } from "../../logger"; import { VariableGroupData } from "../../types"; import decorator from "./create.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; interface CommandOptions { file: string | undefined; @@ -31,16 +33,20 @@ interface CommandOptions { export const validateValues = (opts: CommandOptions): void => { if (!opts.file) { - throw Error("You need to specify a file with variable group manifest"); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "variable-group-create-cmd-err-file-missing" + ); } const config = Config(); const azure = config.azure_devops; if (!hasValue(opts.orgName) && !azure?.org) { - throw Error( + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "variable-group-create-cmd-err-org-missing", // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - `value for ${getCmdOption(decorator, "org-name")!.arg} is missing` - ); + values: [getCmdOption(decorator, "org-name")!.arg], + }); } if (hasValue(opts.orgName)) { @@ -49,10 +55,11 @@ export const validateValues = (opts: CommandOptions): void => { } if (!hasValue(opts.devopsProject) && !azure?.project) { - throw Error( + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "variable-group-create-cmd-err-project-missing", // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - `value for ${getCmdOption(decorator, "devops-project")!.arg} is missing` - ); + values: [getCmdOption(decorator, "devops-project")!.arg], + }); } if (hasValue(opts.devopsProject)) { @@ -61,12 +68,11 @@ export const validateValues = (opts: CommandOptions): void => { } if (!hasValue(opts.personalAccessToken) && !azure?.access_token) { - throw Error( - `value for ${ - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - getCmdOption(decorator, "personal-access-token")!.arg - } is missing` - ); + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "variable-group-create-cmd-err-access-token", + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + values: [getCmdOption(decorator, "personal-access-token")!.arg], + }); } }; @@ -95,8 +101,13 @@ export const commandDecorator = (command: commander.Command): void => { ); await exitCmd(logger, process.exit, 0); } catch (err) { - logger.error(`Error occurred while creating variable group`); - logger.error(err); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + logError( + buildError( + errorStatusCode.CMD_EXE_ERR, + "variable-group-create-cmd-failed" + ) + ); await exitCmd(logger, process.exit, 1); } }); @@ -126,8 +137,10 @@ export const create = async ( } else if (data.type === "Vsts") { await addVariableGroup(data, accessOpts); } else { - throw new Error( - `Variable Group type "${data.type}" is not supported. Only "Vsts" and "AzureKeyVault" are valid types and case sensitive.` - ); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + throw buildError(errorStatusCode.EXE_FLOW_ERR, { + errorKey: "variable-group-create-cmd-err-create", + values: [data.type], + }); } }; diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 5e6728b5f..bbb0948de 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -109,6 +109,13 @@ "ring-delete-cmd-failed": "Error occurred while deleting ring: {0}.", "ring-set-default-cmd-failed": "Error occurred while setting default ring: {0}", + "variable-group-create-cmd-failed": "Error occurred while creating variable group.", + "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.", + "variable-group-create-cmd-err-access-token": "Provide a value for {0}.", + "variable-group-create-cmd-err-project-missing": "Provide a value for {0}.", + "variable-group-create-cmd-err-org-missing": "Provide a value for {0}.", + "variable-group-create-cmd-err-file-missing": "Provide a file with the variable group manifest.", + "validation-err-org-name-missing": "Organization name was missing. Provide it.", "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.", "validation-err-password-missing": "Password was missing. Provide it.",