Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit 98f2f92

Browse files
authored
[FEATURE] Added code to have error chaining in deployment create cmd. (#477)
* [FEATURE] Added code to have error chaining in deployment create cmd. * Update create.ts
1 parent 1428442 commit 98f2f92

File tree

4 files changed

+95
-50
lines changed

4 files changed

+95
-50
lines changed

src/commands/deployment/create.ts

+22-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
1010
import { hasValue } from "../../lib/validator";
1111
import { logger } from "../../logger";
1212
import decorator from "./create.decorator.json";
13+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
14+
import { errorStatusCode } from "../../lib/errorStatusCode";
1315

1416
/**
1517
* Command Line values from the commander
@@ -51,8 +53,9 @@ export const validateValues = (opts: CommandOptions): CreateConfig => {
5153
!hasValue(opts.partitionKey) ||
5254
!hasValue(opts.tableName)
5355
) {
54-
throw new Error(
55-
"Access key, storage account name, partition key and/or table name are not provided"
56+
throw buildError(
57+
errorStatusCode.VALIDATION_ERR,
58+
"introspect-create-cmd-missing-values"
5659
);
5760
}
5861

@@ -74,8 +77,9 @@ export const handlePipeline1 = async (
7477
!hasValue(opts.commitId) ||
7578
!hasValue(opts.service)
7679
) {
77-
throw new Error(
78-
"For updating the details of source pipeline, you must specify --image-tag, --commit-id and --service"
80+
throw buildError(
81+
errorStatusCode.VALIDATION_ERR,
82+
"introspect-create-cmd-cmd-p1-missing-values"
7983
);
8084
}
8185
await addSrcToACRPipeline(
@@ -98,8 +102,9 @@ export const handlePipeline2 = async (
98102
!hasValue(opts.env) ||
99103
!hasValue(opts.imageTag)
100104
) {
101-
throw new Error(
102-
"For updating the details of image tag release pipeline, you must specify --p2, --hld-commit-id, --image-tag and --env"
105+
throw buildError(
106+
errorStatusCode.VALIDATION_ERR,
107+
"introspect-create-cmd-cmd-p2-missing-values"
103108
);
104109
}
105110
await updateACRToHLDPipeline(
@@ -155,11 +160,20 @@ export const execute = async (
155160
opts.repository
156161
);
157162
} else {
158-
throw new Error("No action could be performed for specified arguments.");
163+
throw buildError(
164+
errorStatusCode.VALIDATION_ERR,
165+
"introspect-create-cmd-no-ops"
166+
);
159167
}
160168
await exitFn(0);
161169
} catch (err) {
162-
logger.error(err);
170+
logError(
171+
buildError(
172+
errorStatusCode.CMD_EXE_ERR,
173+
"introspect-create-cmd-failed",
174+
err
175+
)
176+
);
163177
await exitFn(1);
164178
}
165179
};

src/lib/azure/deploymenttable.ts

+58-38
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
import * as azure from "azure-storage";
33
import uuid from "uuid/v4";
44
import { logger } from "../../logger";
5+
import { build as buildError } from "../errorBuilder";
6+
import { errorStatusCode } from "../errorStatusCode";
7+
58
/**
69
* Deployment Table interface to hold necessary information about a table for deployments
710
*/
@@ -400,30 +403,38 @@ export const updateHLDToManifestPipeline = async (
400403
pr?: string,
401404
repository?: string
402405
): Promise<RowHLDToManifestPipeline> => {
403-
let entries = await findMatchingDeployments<EntryHLDToManifestPipeline>(
404-
tableInfo,
405-
"hldCommitId",
406-
hldCommitId
407-
);
406+
try {
407+
let entries = await findMatchingDeployments<EntryHLDToManifestPipeline>(
408+
tableInfo,
409+
"hldCommitId",
410+
hldCommitId
411+
);
408412

409-
// cannot find entries by hldCommitId.
410-
// attempt to find entries by pr
411-
if ((!entries || entries.length === 0) && pr) {
412-
entries = await findMatchingDeployments<EntryHLDToManifestPipeline>(
413+
// cannot find entries by hldCommitId.
414+
// attempt to find entries by pr
415+
if ((!entries || entries.length === 0) && pr) {
416+
entries = await findMatchingDeployments<EntryHLDToManifestPipeline>(
417+
tableInfo,
418+
"pr",
419+
pr
420+
);
421+
}
422+
return updateHLDtoManifestHelper(
423+
entries,
413424
tableInfo,
414-
"pr",
415-
pr
425+
hldCommitId,
426+
pipelineId,
427+
manifestCommitId,
428+
pr,
429+
repository
430+
);
431+
} catch (err) {
432+
throw buildError(
433+
errorStatusCode.AZURE_STORAGE_OP_ERR,
434+
"deployment-table-update-hld-manifest-pipeline-failed",
435+
err
416436
);
417437
}
418-
return updateHLDtoManifestHelper(
419-
entries,
420-
tableInfo,
421-
hldCommitId,
422-
pipelineId,
423-
manifestCommitId,
424-
pr,
425-
repository
426-
);
427438
};
428439

429440
/**
@@ -664,27 +675,36 @@ export const updateManifestCommitId = async (
664675
manifestCommitId: string,
665676
repository?: string
666677
): Promise<RowManifest> => {
667-
const entries = await findMatchingDeployments<RowManifest>(
668-
tableInfo,
669-
"p3",
670-
pipelineId
671-
);
672-
// Ideally there should only be one entry for every pipeline id
673-
if (entries.length > 0) {
674-
const entry = entries[0];
675-
entry.manifestCommitId = manifestCommitId;
676-
if (repository) {
677-
entry.manifestRepo = repository.toLowerCase();
678+
try {
679+
const entries = await findMatchingDeployments<RowManifest>(
680+
tableInfo,
681+
"p3",
682+
pipelineId
683+
);
684+
// Ideally there should only be one entry for every pipeline id
685+
if (entries.length > 0) {
686+
const entry = entries[0];
687+
entry.manifestCommitId = manifestCommitId;
688+
if (repository) {
689+
entry.manifestRepo = repository.toLowerCase();
690+
}
691+
await updateEntryInTable(tableInfo, entry);
692+
logger.info(
693+
`Update manifest commit Id ${manifestCommitId} for pipeline Id ${pipelineId}`
694+
);
695+
return entry;
678696
}
679-
await updateEntryInTable(tableInfo, entry);
680-
logger.info(
681-
`Update manifest commit Id ${manifestCommitId} for pipeline Id ${pipelineId}`
697+
} catch (err) {
698+
throw buildError(
699+
errorStatusCode.AZURE_STORAGE_OP_ERR,
700+
"deployment-table-update-manifest-commit-id-failed",
701+
err
682702
);
683-
return entry;
684703
}
685-
throw new Error(
686-
`No manifest generation found to update manifest commit ${manifestCommitId}`
687-
);
704+
throw buildError(errorStatusCode.AZURE_STORAGE_OP_ERR, {
705+
errorKey: "deployment-table-update-manifest-commit-id-failed-no-generation",
706+
values: [manifestCommitId],
707+
});
688708
};
689709

690710
/**

src/lib/errorStatusCode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export enum errorStatusCode {
99
FILE_IO_ERR = 1011,
1010
INCORRECT_DEFINITION = 1012,
1111
GIT_OPS_ERR = 1100,
12+
AZURE_STORAGE_OP_ERR = 2000,
1213
}

src/lib/i18n.json

+14-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
"storageKeVaultName": "Enter key vault name (have the value as empty and hit enter key to skip)"
1717
},
1818
"errors": {
19-
"infra-scaffold-cmd-failed": "Infra scaffold Command was not successfully executed.",
19+
"infra-scaffold-cmd-failed": "Infra scaffold command was not successfully executed.",
2020
"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.",
2121
"infra-scaffold-cmd-values-missing": "Values for name, version and/or 'template were missing. Provide value for values for them.",
2222

23-
"infra-generate-cmd-failed": "Infra generate Command was not successfully executed.",
23+
"infra-generate-cmd-failed": "Infra generate command was not successfully executed.",
2424

2525
"infra-defn-yaml-not-found": "{0} was not found in {1}",
2626
"infra-defn-yaml-invalid": "The {0} file is invalid. There are missing fields. template: {1} source: {2} version: {3}.",
@@ -34,9 +34,19 @@
3434
"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",
3535
"infra-git-source-no-exist": "Source path, {0} did not exist.",
3636

37-
"hld-append-var-group-cmd-failed": "HLD Append Variable Group Command was not successfully executed.",
37+
"hld-append-var-group-cmd-failed": "HLD Append Variable Group command was not successfully executed.",
3838
"hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.",
3939

40-
"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."
40+
"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.",
41+
42+
"introspect-create-cmd-failed": "Deployment create command was not successfully executed.",
43+
"introspect-create-cmd-no-ops": "No action could be performed for specified arguments.",
44+
"introspect-create-cmd-missing-values": "Access key, storage account name, partition key and/or table name were not provided. Provide them.",
45+
"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.",
46+
"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.",
47+
48+
"deployment-table-update-hld-manifest-pipeline-failed": "Could not update HLD to manifest pipeline.",
49+
"deployment-table-update-manifest-commit-id-failed": "Could not update manifest commit Id.",
50+
"deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}."
4151
}
4252
}

0 commit comments

Comments
 (0)