diff --git a/src/commands/project/pipeline.test.ts b/src/commands/project/pipeline.test.ts index bc8590d99..7c1ce039e 100644 --- a/src/commands/project/pipeline.test.ts +++ b/src/commands/project/pipeline.test.ts @@ -227,7 +227,7 @@ describe("installLifecyclePipeline and execute tests", () => { expect(e).toBeDefined(); const builtDefnString = JSON.stringify({ fakeProperty: "temp" }); expect(e.message).toBe( - `Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}` + `project-pipeline-err-invalid-build-definition: Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}` ); } }); diff --git a/src/commands/project/pipeline.ts b/src/commands/project/pipeline.ts index 9f4cf19e4..086c398d3 100644 --- a/src/commands/project/pipeline.ts +++ b/src/commands/project/pipeline.ts @@ -15,8 +15,6 @@ import { } from "../../lib/commandBuilder"; import { BUILD_SCRIPT_URL, - PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE, - PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE, PROJECT_PIPELINE_FILENAME, } from "../../lib/constants"; import { AzureDevOpsOpts } from "../../lib/git"; @@ -39,6 +37,8 @@ import { import { logger } from "../../logger"; import { BedrockFileInfo, ConfigYaml } from "../../types"; import decorator from "./pipeline.decorator.json"; +import { build as buildError, log as logError } from "../../lib/errorBuilder"; +import { errorStatusCode } from "../../lib/errorStatusCode"; export interface CommandOptions { orgName: string | undefined; @@ -54,9 +54,15 @@ export interface CommandOptions { export const checkDependencies = (projectPath: string): void => { const file: BedrockFileInfo = bedrockFileInfo(projectPath); if (file.exist === false) { - throw new Error(PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "project-pipeline-err-init-dependency" + ); } else if (file.hasVariableGroups === false) { - throw new Error(PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "project-pipeline-err-cvg" + ); } }; @@ -75,11 +81,17 @@ export const fetchValidateValues = ( spkConfig: ConfigYaml | undefined ): CommandOptions => { if (!spkConfig) { - throw new Error("SPK Config is missing"); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "project-pipeline-err-spk-config-missing" + ); } const azureDevops = spkConfig?.azure_devops; if (!opts.repoUrl) { - throw Error(`Repo url not defined`); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "project-pipeline-err-repo-url-undefined" + ); } const values: CommandOptions = { @@ -109,7 +121,10 @@ export const fetchValidateValues = ( const error = validateForRequiredValues(decorator, map); if (error.length > 0) { - throw Error("invalid option values"); + throw buildError( + errorStatusCode.VALIDATION_ERR, + "project-pipeline-err-invalid-options" + ); } validateProjectNameThrowable(values.devopsProject!); @@ -162,10 +177,16 @@ const createPipeline = async ( definition ); } catch (err) { - logger.error( - `Error occurred during pipeline creation for ${values.pipelineName}` + const errorInfo = buildError( + errorStatusCode.EXE_FLOW_ERR, + { + errorKey: "project-pipeline-err-pipeline-create", + values: [values.pipelineName], + }, + err ); - throw err; // catch by other catch block + logError(errorInfo); + throw errorInfo; } }; @@ -191,9 +212,10 @@ export const installLifecyclePipeline = async ( ); if (typeof pipeline.id === "undefined") { const builtDefnString = JSON.stringify(pipeline); - throw Error( - `Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}` - ); + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "project-pipeline-err-invalid-build-definition", + values: [builtDefnString], + }); } logger.info(`Created pipeline for ${values.pipelineName}`); logger.info(`Pipeline ID: ${pipeline.id}`); @@ -213,28 +235,29 @@ export const execute = async ( projectPath: string, exitFn: (status: number) => Promise ): Promise => { - if (!opts.repoUrl || !opts.pipelineName) { - logger.error(`Values for repo url and/or pipeline name are missing`); - await exitFn(1); - return; - } - const gitUrlType = await isGitHubUrl(opts.repoUrl); - if (gitUrlType) { - logger.error( - `GitHub repos are not supported. Repo url: ${opts.repoUrl} is invalid` - ); - await exitFn(1); - return; - } - if (!projectPath) { - logger.error("Project Path is missing"); - await exitFn(1); - return; - } + try { + if (!opts.repoUrl || !opts.pipelineName) { + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "project-pipeline-err-missing-values", + values: ["repo url and/or pipeline name"], + }); + } + const gitUrlType = await isGitHubUrl(opts.repoUrl); + if (gitUrlType) { + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "project-pipeline-err-github-repo", + values: [opts.repoUrl], + }); + } + if (!projectPath) { + throw buildError(errorStatusCode.VALIDATION_ERR, { + errorKey: "project-pipeline-err-missing-values", + values: ["project path"], + }); + } - logger.verbose(`project path: ${projectPath}`); + logger.verbose(`project path: ${projectPath}`); - try { checkDependencies(projectPath); const gitOriginUrl = await getOriginUrl(); const values = fetchValidateValues(opts, gitOriginUrl, Config()); @@ -254,10 +277,13 @@ export const execute = async ( await installLifecyclePipeline(values); await exitFn(0); } catch (err) { - logger.error( - `Error occurred installing pipeline for project hld lifecycle.` + logError( + buildError( + errorStatusCode.CMD_EXE_ERR, + "project-pipeline-cmd-failed", + err + ) ); - logger.error(err); await exitFn(1); } }; diff --git a/src/lib/gitutils.ts b/src/lib/gitutils.ts index fc81642e7..9ed90622b 100644 --- a/src/lib/gitutils.ts +++ b/src/lib/gitutils.ts @@ -182,8 +182,15 @@ export const getRepositoryName = (originUrl: string): string => { logger.debug("github repo found."); return name; } else { - throw Error( - "Could not determine origin repository, or it is not a supported type." + if (!resource.startsWith("http")) { + throw buildError( + errorStatusCode.VALIDATION_ERR, + "git-err-invalid-repository-url" + ); + } + throw buildError( + errorStatusCode.VALIDATION_ERR, + "git-err-validating-remote-git" ); } }; diff --git a/src/lib/i18n.json b/src/lib/i18n.json index 9f5104be4..9f39d2ece 100644 --- a/src/lib/i18n.json +++ b/src/lib/i18n.json @@ -40,6 +40,20 @@ "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.", + "project-pipeline-cmd-failed": "Project install lifecycle pipeline was not successfully executed.", + "project-pipeline-err-github-repo": "GitHub repos are not supported. Repo url: {0} is invalid", + "project-pipeline-err-missing-values": "Value for {0} is missing.", + "project-pipeline-err-init-dependency": "Please run `spk project init` and `spk project cvg` commands before running this command to initialize the project.", + "project-pipeline-err-cvg": "Please run `spk project cvg` command before running this command to create a variable group.", + "project-pipeline-err-repo-url-undefined": "Repo url not defined", + "project-pipeline-err-spk-config-missing": "SPK Config is missing", + "project-pipeline-err-invalid-options": "Invalid option values", + "project-pipeline-err-pipeline-create": "Error occurred during pipeline creation for {0}", + "project-pipeline-err-invalid-build-definition": "Invalid BuildDefinition created, parameter 'id' is missing from {0}", + + "git-err-validating-remote-git": "Could not determine origin repository, or it is not a supported type.", + "git-err-invalid-repository-url": "Repositiry URL does not begin with http(s).", + "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-generate-hld-pipeline-yaml": "Could not generate HLD Azure pipeline YAML.", "fileutils-generate-default-hld-component-yaml": "Could not generate default HLD component YAML.",