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

Commit 59a8b70

Browse files
authored
[FEATURE] Error code and exception chain for hld init command (#478)
* [FEATURE] Error code and exception chain for hld init command * Update i18n.json
1 parent a960bf8 commit 59a8b70

File tree

4 files changed

+105
-64
lines changed

4 files changed

+105
-64
lines changed

src/commands/hld/init.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { checkoutCommitPushCreatePRLink } from "../../lib/gitutils";
1010
import { hasValue } from "../../lib/validator";
1111
import { logger } from "../../logger";
1212
import decorator from "./init.decorator.json";
13+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
14+
import { errorStatusCode } from "../../lib/errorStatusCode";
1315

1416
// values that we need to pull out from command operator
1517
interface CommandOptions {
@@ -57,7 +59,10 @@ export const execute = async (
5759
): Promise<void> => {
5860
try {
5961
if (!hasValue(hldRepoPath)) {
60-
throw new Error("project path is not provided");
62+
throw buildError(
63+
errorStatusCode.VALIDATION_ERR,
64+
"hld-init-cmd-project-path-missing"
65+
);
6166
}
6267
await initialize(
6368
hldRepoPath,
@@ -68,10 +73,9 @@ export const execute = async (
6873
);
6974
await exitFn(0);
7075
} catch (err) {
71-
logger.error(
72-
`Error occurred while initializing hld repository ${hldRepoPath}`
76+
logError(
77+
buildError(errorStatusCode.CMD_EXE_ERR, "hld-init-cmd-failed", err)
7378
);
74-
logger.error(err);
7579
await exitFn(1);
7680
}
7781
};

src/lib/fileutils.ts

+82-58
Original file line numberDiff line numberDiff line change
@@ -623,37 +623,44 @@ const manifestGenerationPipelineYaml = (): string => {
623623
export const generateHldAzurePipelinesYaml = (
624624
targetDirectory: string
625625
): void => {
626-
const absTargetPath = path.resolve(targetDirectory);
627-
logger.info(`Generating hld manifest-generation in ${absTargetPath}`);
628-
629-
const azurePipelinesYamlPath = path.join(
630-
absTargetPath,
631-
RENDER_HLD_PIPELINE_FILENAME
632-
);
626+
try {
627+
const absTargetPath = path.resolve(targetDirectory);
628+
logger.info(`Generating hld manifest-generation in ${absTargetPath}`);
633629

634-
if (fs.existsSync(azurePipelinesYamlPath)) {
635-
logger.warn(
636-
`Existing ${RENDER_HLD_PIPELINE_FILENAME} found at ${azurePipelinesYamlPath}, skipping generation.`
630+
const azurePipelinesYamlPath = path.join(
631+
absTargetPath,
632+
RENDER_HLD_PIPELINE_FILENAME
637633
);
638634

639-
return;
640-
}
641-
const hldYaml = manifestGenerationPipelineYaml();
642-
logger.info(
643-
`Writing ${RENDER_HLD_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
644-
);
635+
if (fs.existsSync(azurePipelinesYamlPath)) {
636+
logger.warn(
637+
`Existing ${RENDER_HLD_PIPELINE_FILENAME} found at ${azurePipelinesYamlPath}, skipping generation.`
638+
);
639+
return;
640+
}
641+
const hldYaml = manifestGenerationPipelineYaml();
642+
logger.info(
643+
`Writing ${RENDER_HLD_PIPELINE_FILENAME} file to ${azurePipelinesYamlPath}`
644+
);
645645

646-
const requiredPipelineVariables = [
647-
`'MANIFEST_REPO' (Repository for your kubernetes manifests in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/materialized')`,
648-
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
649-
].join(", ");
646+
const requiredPipelineVariables = [
647+
`'MANIFEST_REPO' (Repository for your kubernetes manifests in AzDo. eg. 'dev.azure.com/bhnook/fabrikam/_git/materialized')`,
648+
`'PAT' (AzDo Personal Access Token with permissions to the HLD repository.)`,
649+
].join(", ");
650650

651-
logger.info(
652-
`Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
653-
);
651+
logger.info(
652+
`Generated ${RENDER_HLD_PIPELINE_FILENAME}. Commit and push this file to master before attempting to deploy via the command 'spk hld install-manifest-pipeline'; before running the pipeline ensure the following environment variables are available to your pipeline: ${requiredPipelineVariables}`
653+
);
654654

655-
writeVersion(azurePipelinesYamlPath);
656-
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
655+
writeVersion(azurePipelinesYamlPath);
656+
fs.appendFileSync(azurePipelinesYamlPath, hldYaml, "utf8");
657+
} catch (err) {
658+
throw buildError(
659+
errorStatusCode.FILE_IO_ERR,
660+
"fileutils-generate-hld-pipeline-yaml",
661+
err
662+
);
663+
}
657664
};
658665

659666
/**
@@ -688,34 +695,41 @@ export const generateDefaultHldComponentYaml = (
688695
componentName: string,
689696
componentPath: string
690697
): void => {
691-
const absTargetPath = path.resolve(targetDirectory);
692-
logger.info(`Generating component.yaml in ${absTargetPath}`);
693-
694-
const fabrikateComponentPath = path.join(absTargetPath, "component.yaml");
698+
try {
699+
const absTargetPath = path.resolve(targetDirectory);
700+
logger.info(`Generating component.yaml in ${absTargetPath}`);
701+
702+
const fabrikateComponentPath = path.join(absTargetPath, "component.yaml");
703+
704+
if (fs.existsSync(fabrikateComponentPath)) {
705+
logger.warn(
706+
`Existing component.yaml found at ${fabrikateComponentPath}, skipping generation.`
707+
);
708+
return;
709+
}
710+
711+
const componentYaml = defaultComponentYaml(
712+
componentGit,
713+
componentName,
714+
componentPath
715+
);
695716

696-
if (fs.existsSync(fabrikateComponentPath)) {
697-
logger.warn(
698-
`Existing component.yaml found at ${fabrikateComponentPath}, skipping generation.`
717+
logger.info(
718+
`Writing ${HLD_COMPONENT_FILENAME} file to ${fabrikateComponentPath}`
699719
);
700720

701-
return;
721+
fs.writeFileSync(
722+
fabrikateComponentPath,
723+
yaml.safeDump(componentYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
724+
"utf8"
725+
);
726+
} catch (err) {
727+
throw buildError(
728+
errorStatusCode.FILE_IO_ERR,
729+
"fileutils-generate-default-hld-component-yaml",
730+
err
731+
);
702732
}
703-
704-
const componentYaml = defaultComponentYaml(
705-
componentGit,
706-
componentName,
707-
componentPath
708-
);
709-
710-
logger.info(
711-
`Writing ${HLD_COMPONENT_FILENAME} file to ${fabrikateComponentPath}`
712-
);
713-
714-
fs.writeFileSync(
715-
fabrikateComponentPath,
716-
yaml.safeDump(componentYaml, { lineWidth: Number.MAX_SAFE_INTEGER }),
717-
"utf8"
718-
);
719733
};
720734

721735
const hldLifecyclePipelineYaml = (): string => {
@@ -896,18 +910,28 @@ export const generateGitIgnoreFile = (
896910
const absTargetPath = path.resolve(targetDirectory);
897911
logger.info(`Generating starter .gitignore in ${absTargetPath}`);
898912

899-
const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");
913+
try {
914+
const gitIgnoreFilePath = path.join(absTargetPath, ".gitignore");
900915

901-
if (fs.existsSync(gitIgnoreFilePath)) {
902-
logger.warn(
903-
`Existing .gitignore found at ${gitIgnoreFilePath}, skipping generation.`
904-
);
916+
if (fs.existsSync(gitIgnoreFilePath)) {
917+
logger.warn(
918+
`Existing .gitignore found at ${gitIgnoreFilePath}, skipping generation.`
919+
);
920+
return;
921+
}
905922

906-
return;
923+
logger.info(`Writing .gitignore file to ${gitIgnoreFilePath}`);
924+
fs.writeFileSync(gitIgnoreFilePath, content, "utf8");
925+
} catch (err) {
926+
throw buildError(
927+
errorStatusCode.FILE_IO_ERR,
928+
{
929+
errorKey: "fileutils-generate-git-ignore-file",
930+
values: [absTargetPath],
931+
},
932+
err
933+
);
907934
}
908-
909-
logger.info(`Writing .gitignore file to ${gitIgnoreFilePath}`);
910-
fs.writeFileSync(gitIgnoreFilePath, content, "utf8");
911935
};
912936

913937
/**

src/lib/gitutils.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import path from "path";
44
import url from "url";
55
import { logger } from "../logger";
66
import { exec } from "./shell";
7+
import { build as buildError } from "./errorBuilder";
8+
import { errorStatusCode } from "./errorStatusCode";
79

810
/**
911
* For git urls that you may want to log only!
@@ -329,8 +331,11 @@ export const checkoutCommitPushCreatePRLink = async (
329331
);
330332
});
331333
} catch (err) {
332-
logger.error(err);
333-
throw err;
334+
throw buildError(
335+
errorStatusCode.GIT_OPS_ERR,
336+
"git-checkout-commit-push-create-PR-link",
337+
err
338+
);
334339
}
335340
};
336341

src/lib/i18n.json

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
"storageKeVaultName": "Enter key vault name (have the value as empty and hit enter key to skip)"
1717
},
1818
"errors": {
19+
"hld-init-cmd-failed": "Hld init command was not successfully executed.",
20+
"hld-init-cmd-project-path-missing": "Value for project path was not provided. Provide it.",
21+
1922
"infra-scaffold-cmd-failed": "Infra scaffold command was not successfully executed.",
2023
"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.",
2124
"infra-scaffold-cmd-values-missing": "Values for name, version and/or 'template were missing. Provide value for values for them.",
@@ -38,6 +41,11 @@
3841
"hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.",
3942

4043
"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.",
44+
"fileutils-generate-hld-pipeline-yaml": "Could not generate HLD Azure pipeline YAML.",
45+
"fileutils-generate-default-hld-component-yaml": "Could not generate default HLD component YAML.",
46+
"fileutils-generate-git-ignore-file": "Could not generate .gitignore file in {0}.",
47+
48+
"git-checkout-commit-push-create-PR-link": "Could not checkout, commit, push or create pull request",
4149

4250
"introspect-create-cmd-failed": "Deployment create command was not successfully executed.",
4351
"introspect-create-cmd-no-ops": "No action could be performed for specified arguments.",

0 commit comments

Comments
 (0)