From 5406e7ee3d5375de94b2ae1947a5e8078367ba27 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Thu, 2 Apr 2020 12:30:12 -0700 Subject: [PATCH 1/3] Add error codes to ring commands --- src/commands/ring/create.ts | 25 ++++++++++++++++++------- src/commands/ring/delete.ts | 17 +++++++++++++---- src/commands/ring/set-default.ts | 17 +++++++++++++---- src/lib/i18n.json | 9 ++++++++- 4 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/commands/ring/create.ts b/src/commands/ring/create.ts index 1fbecb16c..24121a1eb 100644 --- a/src/commands/ring/create.ts +++ b/src/commands/ring/create.ts @@ -15,6 +15,8 @@ import { hasValue } from "../../lib/validator"; import { logger } from "../../logger"; import { BedrockFile, BedrockFileInfo } from "../../types"; import decorator from "./create.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; /** * Check for bedrock.yaml @@ -28,15 +30,16 @@ export const checkDependencies = ( ): void => { const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath); if (fileInfo.exist === false) { - throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE); + throw buildError(errorStatusCode.VALIDATION_ERR, "ring-err-dependency"); } // Check if ring already exists, if it does, warn and exit const bedrockFile: BedrockFile = loadBedrockFile(projectPath); if (ringName in bedrockFile.rings) { - throw new Error( - `ring: ${ringName} already exists in project ${BEDROCK_FILENAME}.` - ); + throw buildError(errorStatusCode.EXE_FLOW_ERR, { + errorKey: "ring-err-exists", + values: [ringName, BEDROCK_FILENAME], + }); } }; @@ -52,7 +55,7 @@ export const execute = async ( exitFn: (status: number) => Promise ): Promise => { if (!hasValue(ringName)) { - logger.error(`No ring name given.`); + logError(buildError(errorStatusCode.VALIDATION_ERR, "ring-err-name")); await exitFn(1); return; } @@ -82,8 +85,16 @@ export const execute = async ( logger.info(`Successfully created ring: ${ringName} for this project!`); await exitFn(0); } catch (err) { - logger.error(`Error occurred while creating ring: ${ringName}`); - logger.error(err); + logError( + buildError( + errorStatusCode.CMD_EXE_ERR, + { + errorKey: "ring-err-create", + values: [ringName], + }, + err + ) + ); await exitFn(1); } }; diff --git a/src/commands/ring/delete.ts b/src/commands/ring/delete.ts index ee75d0287..ede6493cd 100644 --- a/src/commands/ring/delete.ts +++ b/src/commands/ring/delete.ts @@ -1,12 +1,13 @@ import commander from "commander"; import * as bedrock from "../../lib/bedrockYaml"; import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; -import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants"; import { updateTriggerBranchesForServiceBuildAndUpdatePipeline } from "../../lib/fileutils"; import { hasValue } from "../../lib/validator"; import { logger } from "../../logger"; import { BedrockFileInfo } from "../../types"; import decorator from "./delete.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; /** * Check the bedrock.yaml and the target ring exists @@ -15,7 +16,7 @@ import decorator from "./delete.decorator.json"; export const checkDependencies = (projectPath: string): void => { const fileInfo: BedrockFileInfo = bedrock.fileInfo(projectPath); if (fileInfo.exist === false) { - throw Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE); + throw buildError(errorStatusCode.VALIDATION_ERR, "ring-err-dependency"); } }; @@ -60,8 +61,16 @@ export const execute = async ( logger.info(`Successfully deleted ring: ${ringName} from this project!`); await exitFn(0); } catch (err) { - logger.error(`Error occurred while deleting ring: ${ringName}`); - logger.error(err); + logError( + buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "ring-err-delete", + values: [ringName], + }, + err + ) + ); await exitFn(1); } }; diff --git a/src/commands/ring/set-default.ts b/src/commands/ring/set-default.ts index b485d92c1..10fb1cd59 100644 --- a/src/commands/ring/set-default.ts +++ b/src/commands/ring/set-default.ts @@ -5,11 +5,12 @@ import { setDefaultRing, } from "../../lib/bedrockYaml"; import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; -import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants"; import { hasValue } from "../../lib/validator"; import { logger } from "../../logger"; import { BedrockFileInfo } from "../../types"; import decorator from "./set-default.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; /** * Check for bedrock.yaml @@ -18,7 +19,7 @@ import decorator from "./set-default.decorator.json"; export const checkDependencies = (projectPath: string): void => { const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath); if (fileInfo.exist === false) { - throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE); + throw buildError(errorStatusCode.VALIDATION_ERR, "ring-err-dependency"); } }; @@ -50,8 +51,16 @@ export const execute = async ( logger.info(`Successfully set default ring: ${ringName} for this project!`); await exitFn(0); } catch (err) { - logger.error(`Error occurred while setting default ring: ${ringName}`); - logger.error(err); + logError( + buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "ring-err-set-default", + values: [ringName], + }, + err + ) + ); await exitFn(1); } }; diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 9e3aa9d5a..a30b85559 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -97,6 +97,13 @@ "deployment-table-update-manifest-commit-id-failed": "Could not update manifest commit Id.", "deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}.", "deployment-table-add-src-to-acr-pipeline": "Could not add source to ACR pipeline information to storage table.", - "deployment-table-add-acr-to-hld-pipeline": "Could not add ACR to HLD pipeline information to storage table." + "deployment-table-add-acr-to-hld-pipeline": "Could not add ACR to HLD pipeline information to storage table.", + + "ring-err-create": "Error occurred while creating ring: {0}", + "ring-err-exists": "Ring: {0} already exists in project {1}.", + "ring-err-dependency": "Please run `spk project init` command before running this command to initialize the project.", + "ring-err-name": "No ring name given.", + "ring-err-delete": "Error occurred while deleting ring: {0}.", + "ring-err-set-default": "Error occurred while setting default ring: {0}" } } From b13d7302885787a3331f53d7867c3152ac6524fe Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Thu, 2 Apr 2020 12:31:47 -0700 Subject: [PATCH 2/3] Remove unused import --- src/commands/ring/create.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/commands/ring/create.ts b/src/commands/ring/create.ts index 24121a1eb..a0770e97f 100644 --- a/src/commands/ring/create.ts +++ b/src/commands/ring/create.ts @@ -5,10 +5,7 @@ import { read as loadBedrockFile, } from "../../lib/bedrockYaml"; import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; -import { - BEDROCK_FILENAME, - PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE, -} from "../../lib/constants"; +import { BEDROCK_FILENAME } from "../../lib/constants"; import { updateTriggerBranchesForServiceBuildAndUpdatePipeline } from "../../lib/fileutils"; import * as dns from "../../lib/net/dns"; import { hasValue } from "../../lib/validator"; From 44f4e298e8b372892ab136c6084a7b7f6082a143 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Fri, 3 Apr 2020 09:24:41 -0700 Subject: [PATCH 3/3] Update error codes --- src/commands/ring/create.ts | 16 ++++++++++++---- src/commands/ring/delete.ts | 7 +++++-- src/commands/ring/set-default.ts | 7 +++++-- src/lib/i18n.json | 14 ++++++++------ 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/commands/ring/create.ts b/src/commands/ring/create.ts index a0770e97f..8508ad6c8 100644 --- a/src/commands/ring/create.ts +++ b/src/commands/ring/create.ts @@ -27,14 +27,17 @@ export const checkDependencies = ( ): void => { const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath); if (fileInfo.exist === false) { - throw buildError(errorStatusCode.VALIDATION_ERR, "ring-err-dependency"); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "ring-create-cmd-err-dependency" + ); } // Check if ring already exists, if it does, warn and exit const bedrockFile: BedrockFile = loadBedrockFile(projectPath); if (ringName in bedrockFile.rings) { throw buildError(errorStatusCode.EXE_FLOW_ERR, { - errorKey: "ring-err-exists", + errorKey: "ring-create-cmd-err-ring-exists", values: [ringName, BEDROCK_FILENAME], }); } @@ -52,7 +55,12 @@ export const execute = async ( exitFn: (status: number) => Promise ): Promise => { if (!hasValue(ringName)) { - logError(buildError(errorStatusCode.VALIDATION_ERR, "ring-err-name")); + logError( + buildError( + errorStatusCode.VALIDATION_ERR, + "ring-create-cmd-err-name-missing" + ) + ); await exitFn(1); return; } @@ -86,7 +94,7 @@ export const execute = async ( buildError( errorStatusCode.CMD_EXE_ERR, { - errorKey: "ring-err-create", + errorKey: "ring-create-cmd-failed", values: [ringName], }, err diff --git a/src/commands/ring/delete.ts b/src/commands/ring/delete.ts index ede6493cd..2c8c1ec6b 100644 --- a/src/commands/ring/delete.ts +++ b/src/commands/ring/delete.ts @@ -16,7 +16,10 @@ import { errorStatusCode } from "../../lib/errorStatusCode"; export const checkDependencies = (projectPath: string): void => { const fileInfo: BedrockFileInfo = bedrock.fileInfo(projectPath); if (fileInfo.exist === false) { - throw buildError(errorStatusCode.VALIDATION_ERR, "ring-err-dependency"); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "ring-delete-cmd-err-dependency" + ); } }; @@ -65,7 +68,7 @@ export const execute = async ( buildError( errorStatusCode.EXE_FLOW_ERR, { - errorKey: "ring-err-delete", + errorKey: "ring-delete-cmd-failed", values: [ringName], }, err diff --git a/src/commands/ring/set-default.ts b/src/commands/ring/set-default.ts index 10fb1cd59..72e21dc55 100644 --- a/src/commands/ring/set-default.ts +++ b/src/commands/ring/set-default.ts @@ -19,7 +19,10 @@ import { errorStatusCode } from "../../lib/errorStatusCode"; export const checkDependencies = (projectPath: string): void => { const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath); if (fileInfo.exist === false) { - throw buildError(errorStatusCode.VALIDATION_ERR, "ring-err-dependency"); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "ring-set-default-cmd-err-dependency" + ); } }; @@ -55,7 +58,7 @@ export const execute = async ( buildError( errorStatusCode.EXE_FLOW_ERR, { - errorKey: "ring-err-set-default", + errorKey: "ring-set-default-cmd-failed", values: [ringName], }, err diff --git a/src/lib/i18n.json b/src/lib/i18n.json index a30b85559..fed59e14d 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -99,11 +99,13 @@ "deployment-table-add-src-to-acr-pipeline": "Could not add source to ACR pipeline information to storage table.", "deployment-table-add-acr-to-hld-pipeline": "Could not add ACR to HLD pipeline information to storage table.", - "ring-err-create": "Error occurred while creating ring: {0}", - "ring-err-exists": "Ring: {0} already exists in project {1}.", - "ring-err-dependency": "Please run `spk project init` command before running this command to initialize the project.", - "ring-err-name": "No ring name given.", - "ring-err-delete": "Error occurred while deleting ring: {0}.", - "ring-err-set-default": "Error occurred while setting default ring: {0}" + "ring-create-cmd-failed": "Error occurred while creating ring: {0}", + "ring-create-cmd-err-ring-exists": "Could not create ring {0} in project {1} because it already exists. Provide a different name.", + "ring-create-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.", + "ring-delete-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.", + "ring-set-default-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.", + "ring-create-cmd-err-name-missing": "No ring name was provided. Provide a ring name", + "ring-delete-cmd-failed": "Error occurred while deleting ring: {0}.", + "ring-set-default-cmd-failed": "Error occurred while setting default ring: {0}" } }