diff --git a/src/commands/deployment/create.ts b/src/commands/deployment/create.ts index f0b3ef241..a3bc03327 100644 --- a/src/commands/deployment/create.ts +++ b/src/commands/deployment/create.ts @@ -10,6 +10,8 @@ import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder"; import { hasValue } from "../../lib/validator"; import { logger } from "../../logger"; import decorator from "./create.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; /** * Command Line values from the commander @@ -51,8 +53,9 @@ export const validateValues = (opts: CommandOptions): CreateConfig => { !hasValue(opts.partitionKey) || !hasValue(opts.tableName) ) { - throw new Error( - "Access key, storage account name, partition key and/or table name are not provided" + throw buildError( + errorStatusCode.VALIDATION_ERR, + "introspect-create-cmd-missing-values" ); } @@ -74,8 +77,9 @@ export const handlePipeline1 = async ( !hasValue(opts.commitId) || !hasValue(opts.service) ) { - throw new Error( - "For updating the details of source pipeline, you must specify --image-tag, --commit-id and --service" + throw buildError( + errorStatusCode.VALIDATION_ERR, + "introspect-create-cmd-cmd-p1-missing-values" ); } await addSrcToACRPipeline( @@ -98,8 +102,9 @@ export const handlePipeline2 = async ( !hasValue(opts.env) || !hasValue(opts.imageTag) ) { - throw new Error( - "For updating the details of image tag release pipeline, you must specify --p2, --hld-commit-id, --image-tag and --env" + throw buildError( + errorStatusCode.VALIDATION_ERR, + "introspect-create-cmd-cmd-p2-missing-values" ); } await updateACRToHLDPipeline( @@ -155,11 +160,20 @@ export const execute = async ( opts.repository ); } else { - throw new Error("No action could be performed for specified arguments."); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "introspect-create-cmd-no-ops" + ); } await exitFn(0); } catch (err) { - logger.error(err); + logError( + buildError( + errorStatusCode.CMD_EXE_ERR, + "introspect-create-cmd-failed", + err + ) + ); await exitFn(1); } }; diff --git a/src/lib/azure/deploymenttable.ts b/src/lib/azure/deploymenttable.ts index 8e00cb444..9a181332b 100644 --- a/src/lib/azure/deploymenttable.ts +++ b/src/lib/azure/deploymenttable.ts @@ -2,6 +2,9 @@ import * as azure from "azure-storage"; import uuid from "uuid/v4"; import { logger } from "../../logger"; +import { build as buildError } from "../errorBuilder"; +import { errorStatusCode } from "../errorStatusCode"; + /** * Deployment Table interface to hold necessary information about a table for deployments */ @@ -400,30 +403,38 @@ export const updateHLDToManifestPipeline = async ( pr?: string, repository?: string ): Promise => { - let entries = await findMatchingDeployments( - tableInfo, - "hldCommitId", - hldCommitId - ); + try { + let entries = await findMatchingDeployments( + tableInfo, + "hldCommitId", + hldCommitId + ); - // cannot find entries by hldCommitId. - // attempt to find entries by pr - if ((!entries || entries.length === 0) && pr) { - entries = await findMatchingDeployments( + // cannot find entries by hldCommitId. + // attempt to find entries by pr + if ((!entries || entries.length === 0) && pr) { + entries = await findMatchingDeployments( + tableInfo, + "pr", + pr + ); + } + return updateHLDtoManifestHelper( + entries, tableInfo, - "pr", - pr + hldCommitId, + pipelineId, + manifestCommitId, + pr, + repository + ); + } catch (err) { + throw buildError( + errorStatusCode.AZURE_STORAGE_OP_ERR, + "deployment-table-update-hld-manifest-pipeline-failed", + err ); } - return updateHLDtoManifestHelper( - entries, - tableInfo, - hldCommitId, - pipelineId, - manifestCommitId, - pr, - repository - ); }; /** @@ -664,27 +675,36 @@ export const updateManifestCommitId = async ( manifestCommitId: string, repository?: string ): Promise => { - const entries = await findMatchingDeployments( - tableInfo, - "p3", - pipelineId - ); - // Ideally there should only be one entry for every pipeline id - if (entries.length > 0) { - const entry = entries[0]; - entry.manifestCommitId = manifestCommitId; - if (repository) { - entry.manifestRepo = repository.toLowerCase(); + try { + const entries = await findMatchingDeployments( + tableInfo, + "p3", + pipelineId + ); + // Ideally there should only be one entry for every pipeline id + if (entries.length > 0) { + const entry = entries[0]; + entry.manifestCommitId = manifestCommitId; + if (repository) { + entry.manifestRepo = repository.toLowerCase(); + } + await updateEntryInTable(tableInfo, entry); + logger.info( + `Update manifest commit Id ${manifestCommitId} for pipeline Id ${pipelineId}` + ); + return entry; } - await updateEntryInTable(tableInfo, entry); - logger.info( - `Update manifest commit Id ${manifestCommitId} for pipeline Id ${pipelineId}` + } catch (err) { + throw buildError( + errorStatusCode.AZURE_STORAGE_OP_ERR, + "deployment-table-update-manifest-commit-id-failed", + err ); - return entry; } - throw new Error( - `No manifest generation found to update manifest commit ${manifestCommitId}` - ); + throw buildError(errorStatusCode.AZURE_STORAGE_OP_ERR, { + errorKey: "deployment-table-update-manifest-commit-id-failed-no-generation", + values: [manifestCommitId], + }); }; /** diff --git a/src/lib/errorStatusCode.ts b/src/lib/errorStatusCode.ts index 5b34ee267..769513aa3 100644 --- a/src/lib/errorStatusCode.ts +++ b/src/lib/errorStatusCode.ts @@ -9,4 +9,5 @@ export enum errorStatusCode { FILE_IO_ERR = 1011, INCORRECT_DEFINITION = 1012, GIT_OPS_ERR = 1100, + AZURE_STORAGE_OP_ERR = 2000, } diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 71bfc0ad7..2701ce250 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -16,11 +16,11 @@ "storageKeVaultName": "Enter key vault name (have the value as empty and hit enter key to skip)" }, "errors": { - "infra-scaffold-cmd-failed": "Infra scaffold Command was not successfully executed.", + "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.", - "infra-generate-cmd-failed": "Infra generate Command was not successfully executed.", + "infra-generate-cmd-failed": "Infra generate command was not successfully executed.", "infra-defn-yaml-not-found": "{0} was not found in {1}", "infra-defn-yaml-invalid": "The {0} file is invalid. There are missing fields. template: {1} source: {2} version: {3}.", @@ -34,9 +34,19 @@ "infra-err-git-clone-failed": "Could not clone the source remote repository. The remote repo might not exist or you did not have the rights to access it", "infra-git-source-no-exist": "Source path, {0} did not exist.", - "hld-append-var-group-cmd-failed": "HLD Append Variable Group Command was not successfully executed.", + "hld-append-var-group-cmd-failed": "HLD Append Variable Group command was not successfully executed.", "hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.", - "fileutils-append-variable-group-to-pipeline-yaml": "Could not append variable group name to manifest-generation.yaml file in HLD repo. Check this is file exist and if it is YAML format." + "fileutils-append-variable-group-to-pipeline-yaml": "Could not append variable group name to manifest-generation.yaml file in HLD repo. Check this is file exist and if it is YAML format.", + + "introspect-create-cmd-failed": "Deployment create command was not successfully executed.", + "introspect-create-cmd-no-ops": "No action could be performed for specified arguments.", + "introspect-create-cmd-missing-values": "Access key, storage account name, partition key and/or table name were not provided. Provide them.", + "introspect-create-cmd-cmd-p1-missing-values": "Values for image-tag, commit-id and service options were missing. They are required for updating the details of source pipeline. Provide them.", + "introspect-create-cmd-cmd-p2-missing-values": "Values for p2, hld-commit-id, image-tag and env options were missing. They are required For updating the details of image tag release pipeline. Provide them.", + + "deployment-table-update-hld-manifest-pipeline-failed": "Could not update HLD to manifest pipeline.", + "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}." } }