From 3b6a70e26d61e274b221c624141f564aa4086e12 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Wed, 1 Apr 2020 13:15:33 -0700 Subject: [PATCH 1/4] Add error codes in reconcile --- docs/commands/data.json | 2 +- src/commands/hld/reconcile.ts | 94 ++++++++++++++++++++++++++--------- src/lib/i18n.json | 10 ++++ 3 files changed, 81 insertions(+), 25 deletions(-) diff --git a/docs/commands/data.json b/docs/commands/data.json index 79b206aa9..966eda283 100644 --- a/docs/commands/data.json +++ b/docs/commands/data.json @@ -674,4 +674,4 @@ } ] } -} +} \ No newline at end of file diff --git a/src/commands/hld/reconcile.ts b/src/commands/hld/reconcile.ts index da5d2fbbd..000da0344 100644 --- a/src/commands/hld/reconcile.ts +++ b/src/commands/hld/reconcile.ts @@ -18,6 +18,8 @@ import * as middleware from "../../lib/traefik/middleware"; import { logger } from "../../logger"; import { BedrockFile, BedrockServiceConfig } from "../../types"; import decorator from "./reconcile.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; /** * IExecResult represents the possible return value of a Promise based wrapper @@ -70,8 +72,16 @@ export const execAndLog = async (commandToRun: string): Promise => { const pipeOutputToCurrentShell = true; const result = await exec(commandToRun, pipeOutputToCurrentShell); if (result.error) { - logger.error(`an error occurred executing command: \`${commandToRun}\``); - throw result.error; + const errorInfo = buildError( + errorStatusCode.CMD_EXE_ERR, + { + errorKey: "hld-reconcile-err-cmd-failed", + values: [commandToRun], + }, + result.error + ); + logError(errorInfo); + throw errorInfo; } return result; }; @@ -107,10 +117,16 @@ export const createRepositoryComponent = async ( return execCmd( `cd ${absHldPath} && mkdir -p ${repositoryName} && fab add ${repositoryName} --path ./${repositoryName} --method local` ).catch((err) => { - logger.error( - `error creating repository component '${repositoryName}' in path '${absHldPath}'` + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "hld-reconcile-err-repo-create", + values: [repositoryName, absHldPath], + }, + err ); - throw err; + logError(errorInfo); + throw errorInfo; }); }; @@ -142,8 +158,13 @@ export const configureChartForRing = async ( const fabConfigureCommand = `cd ${normalizedRingPathInHld} && fab set --subcomponent "chart" serviceName="${k8sSvcBackendAndName}"`; return execCmd(fabConfigureCommand).catch((err) => { - logger.error(`error configuring helm chart for service `); - throw err; + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + "hld-reconcile-err-helm-config", + err + ); + logError(errorInfo); + throw errorInfo; }); }; @@ -160,10 +181,16 @@ export const createServiceComponent = async ( return execCmd( `cd ${absRepositoryInHldPath} && mkdir -p ${serviceName} config && fab add ${serviceName} --path ./${serviceName} --method local --type component && touch ./config/common.yaml` ).catch((err) => { - logger.error( - `error creating service component '${serviceName}' in path '${absRepositoryInHldPath}'` + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "hld-reconcile-err-service-create", + values: [serviceName, absRepositoryInHldPath], + }, + err ); - throw err; + logError(errorInfo); + throw errorInfo; }); }; @@ -177,10 +204,16 @@ export const createRingComponent = async ( const createRingInSvcCommand = `cd ${svcPathInHld} && mkdir -p ${normalizedRingName} config && fab add ${normalizedRingName} --path ./${normalizedRingName} --method local --type component && touch ./config/common.yaml`; return execCmd(createRingInSvcCommand).catch((err) => { - logger.error( - `error creating ring component '${normalizedRingName}' in path '${svcPathInHld}'` + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "hld-reconcile-err-ring-create", + values: [normalizedRingName, svcPathInHld], + }, + err ); - throw err; + logError(errorInfo); + throw errorInfo; }); }; @@ -211,12 +244,16 @@ export const addChartToRing = async ( return execCmd(`cd ${ringPathInHld} && ${addHelmChartCommand}`).catch( (err) => { - logger.error( - `error adding helm chart for service-config ${JSON.stringify( - serviceConfig - )} to ring path '${ringPathInHld}'` + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "hld-reconcile-err-helm-add", + values: [JSON.stringify(serviceConfig), ringPathInHld], + }, + err ); - throw err; + logError(errorInfo); + throw errorInfo; } ); }; @@ -229,8 +266,16 @@ export const createStaticComponent = async ( const createConfigAndStaticComponentCommand = `cd ${ringPathInHld} && mkdir -p config static && fab add static --path ./static --method local --type static && touch ./config/common.yaml`; return execCmd(createConfigAndStaticComponentCommand).catch((err) => { - logger.error(`error creating static component in path '${ringPathInHld}'`); - throw err; + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "hld-reconcile-err-static-create", + values: [ringPathInHld], + }, + err + ); + logError(errorInfo); + throw errorInfo; }); }; @@ -379,9 +424,7 @@ export const validateInputs = ( export const checkForFabrikate = (which: (path: string) => string): void => { const fabrikateInstalled = which("fab"); if (fabrikateInstalled === "") { - throw ReferenceError( - `Fabrikate not installed. Please fetch and install the latest version: https://github.com/microsoft/fabrikate/releases` - ); + throw buildError(errorStatusCode.VALIDATION_ERR, "hld-reconcile-err-fab"); } }; @@ -393,7 +436,10 @@ export const testAndGetAbsPath = ( ): string => { const absPath = path.resolve(possiblyRelativePath); if (!test("-e", absPath) && !test("-d", absPath)) { - throw Error(`Could not validate ${pathType} path.`); + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "hld-reconcile-err-path", + values: [pathType], + }); } log(`Found ${pathType} at ${absPath}`); return absPath; diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 9f39d2ece..07cd3b46b 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -19,6 +19,16 @@ "hld-init-cmd-failed": "Hld init command was not successfully executed.", "hld-init-cmd-project-path-missing": "Value for project path was not provided. Provide it.", + "hld-reconcile-err-repo-create": "Error creating repository component '{0}' in path '{1}'.", + "hld-reconcile-err-helm-config": "Error configuring helm chart for service.", + "hld-reconcile-err-service-create": "Error creating service component '{0}' in path '{1}'.", + "hld-reconcile-err-ring-create": "`error creating ring component '{0}' in path '{1}'.", + "hld-reconcile-err-helm-add": "Error adding helm chart for service-config {0} to ring path '{1}'.", + "hld-reconcile-err-static-create": "Error creating static component in path '{0}'.", + "hld-reconcile-err-fab": "Fabrikate not installed. Please fetch and install the latest version: https://github.com/microsoft/fabrikate/releases", + "hld-reconcile-err-path": "Could not validate {0} path.", + "hld-reconcile-err-cmd-failed": "An error occurred executing command: {0}", + "infra-scaffold-cmd-failed": "Infra scaffold command was not successfully executed.", "infra-scaffold-cmd-src-missing": "Value for source is required because it cannot be constructed with properties in spk-config.yaml. Provide value for source.", "infra-scaffold-cmd-values-missing": "Values for name, version and/or 'template were missing. Provide value for values for them.", From 637870b04efdc9e6d087fbf52dcefb4c891d9f87 Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Wed, 1 Apr 2020 13:47:35 -0700 Subject: [PATCH 2/4] Updates based on feedback --- src/commands/hld/reconcile.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/commands/hld/reconcile.ts b/src/commands/hld/reconcile.ts index 000da0344..8e40447f0 100644 --- a/src/commands/hld/reconcile.ts +++ b/src/commands/hld/reconcile.ts @@ -72,7 +72,7 @@ export const execAndLog = async (commandToRun: string): Promise => { const pipeOutputToCurrentShell = true; const result = await exec(commandToRun, pipeOutputToCurrentShell); if (result.error) { - const errorInfo = buildError( + throw buildError( errorStatusCode.CMD_EXE_ERR, { errorKey: "hld-reconcile-err-cmd-failed", @@ -80,8 +80,6 @@ export const execAndLog = async (commandToRun: string): Promise => { }, result.error ); - logError(errorInfo); - throw errorInfo; } return result; }; @@ -117,7 +115,7 @@ export const createRepositoryComponent = async ( return execCmd( `cd ${absHldPath} && mkdir -p ${repositoryName} && fab add ${repositoryName} --path ./${repositoryName} --method local` ).catch((err) => { - const errorInfo = buildError( + throw buildError( errorStatusCode.EXE_FLOW_ERR, { errorKey: "hld-reconcile-err-repo-create", @@ -125,8 +123,6 @@ export const createRepositoryComponent = async ( }, err ); - logError(errorInfo); - throw errorInfo; }); }; From 62d1344de73a985e2f8ac71234cb436cbd39131f Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Wed, 1 Apr 2020 13:54:25 -0700 Subject: [PATCH 3/4] Remove log error --- src/commands/hld/reconcile.ts | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/commands/hld/reconcile.ts b/src/commands/hld/reconcile.ts index 8e40447f0..931d66467 100644 --- a/src/commands/hld/reconcile.ts +++ b/src/commands/hld/reconcile.ts @@ -18,7 +18,7 @@ import * as middleware from "../../lib/traefik/middleware"; import { logger } from "../../logger"; import { BedrockFile, BedrockServiceConfig } from "../../types"; import decorator from "./reconcile.decorator.json"; -import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { build as buildError } from "../../lib/errorBuilder"; import { errorStatusCode } from "../../lib/errorStatusCode"; /** @@ -154,13 +154,11 @@ export const configureChartForRing = async ( const fabConfigureCommand = `cd ${normalizedRingPathInHld} && fab set --subcomponent "chart" serviceName="${k8sSvcBackendAndName}"`; return execCmd(fabConfigureCommand).catch((err) => { - const errorInfo = buildError( + throw buildError( errorStatusCode.EXE_FLOW_ERR, "hld-reconcile-err-helm-config", err ); - logError(errorInfo); - throw errorInfo; }); }; @@ -177,7 +175,7 @@ export const createServiceComponent = async ( return execCmd( `cd ${absRepositoryInHldPath} && mkdir -p ${serviceName} config && fab add ${serviceName} --path ./${serviceName} --method local --type component && touch ./config/common.yaml` ).catch((err) => { - const errorInfo = buildError( + throw buildError( errorStatusCode.EXE_FLOW_ERR, { errorKey: "hld-reconcile-err-service-create", @@ -185,8 +183,6 @@ export const createServiceComponent = async ( }, err ); - logError(errorInfo); - throw errorInfo; }); }; @@ -200,7 +196,7 @@ export const createRingComponent = async ( const createRingInSvcCommand = `cd ${svcPathInHld} && mkdir -p ${normalizedRingName} config && fab add ${normalizedRingName} --path ./${normalizedRingName} --method local --type component && touch ./config/common.yaml`; return execCmd(createRingInSvcCommand).catch((err) => { - const errorInfo = buildError( + throw buildError( errorStatusCode.EXE_FLOW_ERR, { errorKey: "hld-reconcile-err-ring-create", @@ -208,8 +204,6 @@ export const createRingComponent = async ( }, err ); - logError(errorInfo); - throw errorInfo; }); }; @@ -240,7 +234,7 @@ export const addChartToRing = async ( return execCmd(`cd ${ringPathInHld} && ${addHelmChartCommand}`).catch( (err) => { - const errorInfo = buildError( + throw buildError( errorStatusCode.EXE_FLOW_ERR, { errorKey: "hld-reconcile-err-helm-add", @@ -248,8 +242,6 @@ export const addChartToRing = async ( }, err ); - logError(errorInfo); - throw errorInfo; } ); }; @@ -262,7 +254,7 @@ export const createStaticComponent = async ( const createConfigAndStaticComponentCommand = `cd ${ringPathInHld} && mkdir -p config static && fab add static --path ./static --method local --type static && touch ./config/common.yaml`; return execCmd(createConfigAndStaticComponentCommand).catch((err) => { - const errorInfo = buildError( + throw buildError( errorStatusCode.EXE_FLOW_ERR, { errorKey: "hld-reconcile-err-static-create", @@ -270,8 +262,6 @@ export const createStaticComponent = async ( }, err ); - logError(errorInfo); - throw errorInfo; }); }; From 9f48a7cbfab57a612e0d7ca2e275485975a22bee Mon Sep 17 00:00:00 2001 From: Edaena Salinas Date: Wed, 1 Apr 2020 14:02:06 -0700 Subject: [PATCH 4/4] Change error logging --- src/commands/hld/reconcile.ts | 7 ++++--- src/lib/i18n.json | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/commands/hld/reconcile.ts b/src/commands/hld/reconcile.ts index 931d66467..13a781dea 100644 --- a/src/commands/hld/reconcile.ts +++ b/src/commands/hld/reconcile.ts @@ -18,7 +18,7 @@ import * as middleware from "../../lib/traefik/middleware"; import { logger } from "../../logger"; import { BedrockFile, BedrockServiceConfig } from "../../types"; import decorator from "./reconcile.decorator.json"; -import { build as buildError } from "../../lib/errorBuilder"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; import { errorStatusCode } from "../../lib/errorStatusCode"; /** @@ -664,8 +664,9 @@ export const execute = async ( ); await exitFn(0); } catch (err) { - logger.error(`An error occurred while reconciling HLD`); - logger.error(err); + logError( + buildError(errorStatusCode.CMD_EXE_ERR, "hld-reconcile-err-cmd-exe", err) + ); await exitFn(1); } }; diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 07cd3b46b..7cc761f40 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -28,6 +28,7 @@ "hld-reconcile-err-fab": "Fabrikate not installed. Please fetch and install the latest version: https://github.com/microsoft/fabrikate/releases", "hld-reconcile-err-path": "Could not validate {0} path.", "hld-reconcile-err-cmd-failed": "An error occurred executing command: {0}", + "hld-reconcile-err-cmd-exe": "An error occurred while reconciling HLD.", "infra-scaffold-cmd-failed": "Infra scaffold command was not successfully executed.", "infra-scaffold-cmd-src-missing": "Value for source is required because it cannot be constructed with properties in spk-config.yaml. Provide value for source.",