From 19fb3e3c3a6dbbada0db0c4b1e3630f95bf63b33 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Fri, 3 Apr 2020 11:02:14 -0700 Subject: [PATCH 1/3] Add error codes to create variable group --- src/commands/variable-group/create.ts | 49 +++++++++++++++++---------- src/lib/i18n.json | 7 ++++ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/commands/variable-group/create.ts b/src/commands/variable-group/create.ts index 6f5e6e5ef..4f43dfc2d 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" + ); } 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", // 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", // 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..058c1a95e 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": "Please provide a value for {0}.", + "variable-group-create-cmd-err-project": "Please provide a value for {0}.", + "variable-group-create-cmd-err-org": "Please provide a value for {0}.", + "variable-group-create-cmd-err-file": "Please 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.", From f2f0bf72a78b71dcdf9768954d9787389099a516 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Fri, 3 Apr 2020 11:28:12 -0700 Subject: [PATCH 2/3] Change error code name --- src/commands/variable-group/create.ts | 6 +++--- src/lib/i18n.json | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/commands/variable-group/create.ts b/src/commands/variable-group/create.ts index 4f43dfc2d..5a3d27d4b 100644 --- a/src/commands/variable-group/create.ts +++ b/src/commands/variable-group/create.ts @@ -35,7 +35,7 @@ export const validateValues = (opts: CommandOptions): void => { if (!opts.file) { throw buildError( errorStatusCode.VALIDATION_ERR, - "variable-group-create-cmd-err-file" + "variable-group-create-cmd-err-file-missing" ); } const config = Config(); @@ -43,7 +43,7 @@ export const validateValues = (opts: CommandOptions): void => { if (!hasValue(opts.orgName) && !azure?.org) { throw buildError(errorStatusCode.VALIDATION_ERR, { - errorKey: "variable-group-create-cmd-err-org", + errorKey: "variable-group-create-cmd-err-org-missing", // eslint-disable-next-line @typescript-eslint/no-non-null-assertion values: [getCmdOption(decorator, "org-name")!.arg], }); @@ -56,7 +56,7 @@ export const validateValues = (opts: CommandOptions): void => { if (!hasValue(opts.devopsProject) && !azure?.project) { throw buildError(errorStatusCode.VALIDATION_ERR, { - errorKey: "variable-group-create-cmd-err-project", + errorKey: "variable-group-create-cmd-err-project-missing", // eslint-disable-next-line @typescript-eslint/no-non-null-assertion values: [getCmdOption(decorator, "devops-project")!.arg], }); diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 058c1a95e..8bf637914 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -112,9 +112,9 @@ "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": "Please provide a value for {0}.", - "variable-group-create-cmd-err-project": "Please provide a value for {0}.", - "variable-group-create-cmd-err-org": "Please provide a value for {0}.", - "variable-group-create-cmd-err-file": "Please provide a file with the variable group manifest.", + "variable-group-create-cmd-err-project-missing": "Please provide a value for {0}.", + "variable-group-create-cmd-err-org-missing": "Please provide a value for {0}.", + "variable-group-create-cmd-err-file-missing": "Please 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.", From 34cd858c6d6125a77388568778e069dfd47724bf Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Fri, 3 Apr 2020 11:29:58 -0700 Subject: [PATCH 3/3] Update error message --- src/lib/i18n.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 8bf637914..bbb0948de 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -111,10 +111,10 @@ "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": "Please provide a value for {0}.", - "variable-group-create-cmd-err-project-missing": "Please provide a value for {0}.", - "variable-group-create-cmd-err-org-missing": "Please provide a value for {0}.", - "variable-group-create-cmd-err-file-missing": "Please provide a file with the variable group manifest.", + "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.",