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

Commit 871828d

Browse files
authored
Better error message in spk project (#484)
1 parent 9397b60 commit 871828d

File tree

4 files changed

+86
-39
lines changed

4 files changed

+86
-39
lines changed

src/commands/project/pipeline.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ describe("installLifecyclePipeline and execute tests", () => {
227227
expect(e).toBeDefined();
228228
const builtDefnString = JSON.stringify({ fakeProperty: "temp" });
229229
expect(e.message).toBe(
230-
`Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}`
230+
`project-pipeline-err-invalid-build-definition: Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}`
231231
);
232232
}
233233
});

src/commands/project/pipeline.ts

+62-36
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import {
1515
} from "../../lib/commandBuilder";
1616
import {
1717
BUILD_SCRIPT_URL,
18-
PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE,
19-
PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE,
2018
PROJECT_PIPELINE_FILENAME,
2119
} from "../../lib/constants";
2220
import { AzureDevOpsOpts } from "../../lib/git";
@@ -39,6 +37,8 @@ import {
3937
import { logger } from "../../logger";
4038
import { BedrockFileInfo, ConfigYaml } from "../../types";
4139
import decorator from "./pipeline.decorator.json";
40+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
41+
import { errorStatusCode } from "../../lib/errorStatusCode";
4242

4343
export interface CommandOptions {
4444
orgName: string | undefined;
@@ -54,9 +54,15 @@ export interface CommandOptions {
5454
export const checkDependencies = (projectPath: string): void => {
5555
const file: BedrockFileInfo = bedrockFileInfo(projectPath);
5656
if (file.exist === false) {
57-
throw new Error(PROJECT_INIT_CVG_DEPENDENCY_ERROR_MESSAGE);
57+
throw buildError(
58+
errorStatusCode.VALIDATION_ERR,
59+
"project-pipeline-err-init-dependency"
60+
);
5861
} else if (file.hasVariableGroups === false) {
59-
throw new Error(PROJECT_CVG_DEPENDENCY_ERROR_MESSAGE);
62+
throw buildError(
63+
errorStatusCode.VALIDATION_ERR,
64+
"project-pipeline-err-cvg"
65+
);
6066
}
6167
};
6268

@@ -75,11 +81,17 @@ export const fetchValidateValues = (
7581
spkConfig: ConfigYaml | undefined
7682
): CommandOptions => {
7783
if (!spkConfig) {
78-
throw new Error("SPK Config is missing");
84+
throw buildError(
85+
errorStatusCode.VALIDATION_ERR,
86+
"project-pipeline-err-spk-config-missing"
87+
);
7988
}
8089
const azureDevops = spkConfig?.azure_devops;
8190
if (!opts.repoUrl) {
82-
throw Error(`Repo url not defined`);
91+
throw buildError(
92+
errorStatusCode.VALIDATION_ERR,
93+
"project-pipeline-err-repo-url-undefined"
94+
);
8395
}
8496

8597
const values: CommandOptions = {
@@ -109,7 +121,10 @@ export const fetchValidateValues = (
109121
const error = validateForRequiredValues(decorator, map);
110122

111123
if (error.length > 0) {
112-
throw Error("invalid option values");
124+
throw buildError(
125+
errorStatusCode.VALIDATION_ERR,
126+
"project-pipeline-err-invalid-options"
127+
);
113128
}
114129

115130
validateProjectNameThrowable(values.devopsProject!);
@@ -162,10 +177,16 @@ const createPipeline = async (
162177
definition
163178
);
164179
} catch (err) {
165-
logger.error(
166-
`Error occurred during pipeline creation for ${values.pipelineName}`
180+
const errorInfo = buildError(
181+
errorStatusCode.EXE_FLOW_ERR,
182+
{
183+
errorKey: "project-pipeline-err-pipeline-create",
184+
values: [values.pipelineName],
185+
},
186+
err
167187
);
168-
throw err; // catch by other catch block
188+
logError(errorInfo);
189+
throw errorInfo;
169190
}
170191
};
171192

@@ -191,9 +212,10 @@ export const installLifecyclePipeline = async (
191212
);
192213
if (typeof pipeline.id === "undefined") {
193214
const builtDefnString = JSON.stringify(pipeline);
194-
throw Error(
195-
`Invalid BuildDefinition created, parameter 'id' is missing from ${builtDefnString}`
196-
);
215+
throw buildError(errorStatusCode.VALIDATION_ERR, {
216+
errorKey: "project-pipeline-err-invalid-build-definition",
217+
values: [builtDefnString],
218+
});
197219
}
198220
logger.info(`Created pipeline for ${values.pipelineName}`);
199221
logger.info(`Pipeline ID: ${pipeline.id}`);
@@ -213,28 +235,29 @@ export const execute = async (
213235
projectPath: string,
214236
exitFn: (status: number) => Promise<void>
215237
): Promise<void> => {
216-
if (!opts.repoUrl || !opts.pipelineName) {
217-
logger.error(`Values for repo url and/or pipeline name are missing`);
218-
await exitFn(1);
219-
return;
220-
}
221-
const gitUrlType = await isGitHubUrl(opts.repoUrl);
222-
if (gitUrlType) {
223-
logger.error(
224-
`GitHub repos are not supported. Repo url: ${opts.repoUrl} is invalid`
225-
);
226-
await exitFn(1);
227-
return;
228-
}
229-
if (!projectPath) {
230-
logger.error("Project Path is missing");
231-
await exitFn(1);
232-
return;
233-
}
238+
try {
239+
if (!opts.repoUrl || !opts.pipelineName) {
240+
throw buildError(errorStatusCode.VALIDATION_ERR, {
241+
errorKey: "project-pipeline-err-missing-values",
242+
values: ["repo url and/or pipeline name"],
243+
});
244+
}
245+
const gitUrlType = await isGitHubUrl(opts.repoUrl);
246+
if (gitUrlType) {
247+
throw buildError(errorStatusCode.VALIDATION_ERR, {
248+
errorKey: "project-pipeline-err-github-repo",
249+
values: [opts.repoUrl],
250+
});
251+
}
252+
if (!projectPath) {
253+
throw buildError(errorStatusCode.VALIDATION_ERR, {
254+
errorKey: "project-pipeline-err-missing-values",
255+
values: ["project path"],
256+
});
257+
}
234258

235-
logger.verbose(`project path: ${projectPath}`);
259+
logger.verbose(`project path: ${projectPath}`);
236260

237-
try {
238261
checkDependencies(projectPath);
239262
const gitOriginUrl = await getOriginUrl();
240263
const values = fetchValidateValues(opts, gitOriginUrl, Config());
@@ -254,10 +277,13 @@ export const execute = async (
254277
await installLifecyclePipeline(values);
255278
await exitFn(0);
256279
} catch (err) {
257-
logger.error(
258-
`Error occurred installing pipeline for project hld lifecycle.`
280+
logError(
281+
buildError(
282+
errorStatusCode.CMD_EXE_ERR,
283+
"project-pipeline-cmd-failed",
284+
err
285+
)
259286
);
260-
logger.error(err);
261287
await exitFn(1);
262288
}
263289
};

src/lib/gitutils.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,15 @@ export const getRepositoryName = (originUrl: string): string => {
182182
logger.debug("github repo found.");
183183
return name;
184184
} else {
185-
throw Error(
186-
"Could not determine origin repository, or it is not a supported type."
185+
if (!resource.startsWith("http")) {
186+
throw buildError(
187+
errorStatusCode.VALIDATION_ERR,
188+
"git-err-invalid-repository-url"
189+
);
190+
}
191+
throw buildError(
192+
errorStatusCode.VALIDATION_ERR,
193+
"git-err-validating-remote-git"
187194
);
188195
}
189196
};

src/lib/i18n.json

+14
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@
4040
"hld-append-var-group-cmd-failed": "HLD Append Variable Group command was not successfully executed.",
4141
"hld-append-var-group-name-missing": "Variable group name was not provided. Provide variable group.",
4242

43+
"project-pipeline-cmd-failed": "Project install lifecycle pipeline was not successfully executed.",
44+
"project-pipeline-err-github-repo": "GitHub repos are not supported. Repo url: {0} is invalid",
45+
"project-pipeline-err-missing-values": "Value for {0} is missing.",
46+
"project-pipeline-err-init-dependency": "Please run `spk project init` and `spk project cvg` commands before running this command to initialize the project.",
47+
"project-pipeline-err-cvg": "Please run `spk project cvg` command before running this command to create a variable group.",
48+
"project-pipeline-err-repo-url-undefined": "Repo url not defined",
49+
"project-pipeline-err-spk-config-missing": "SPK Config is missing",
50+
"project-pipeline-err-invalid-options": "Invalid option values",
51+
"project-pipeline-err-pipeline-create": "Error occurred during pipeline creation for {0}",
52+
"project-pipeline-err-invalid-build-definition": "Invalid BuildDefinition created, parameter 'id' is missing from {0}",
53+
54+
"git-err-validating-remote-git": "Could not determine origin repository, or it is not a supported type.",
55+
"git-err-invalid-repository-url": "Repositiry URL does not begin with http(s).",
56+
4357
"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.",
4458
"fileutils-generate-hld-pipeline-yaml": "Could not generate HLD Azure pipeline YAML.",
4559
"fileutils-generate-default-hld-component-yaml": "Could not generate default HLD component YAML.",

0 commit comments

Comments
 (0)