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

Commit ead0d68

Browse files
edaenaEdaena Salinasyradsmikhamdennisseah
authored
Add error codes for project commands (#514)
* Add error codes for project commands * Change error message Co-authored-by: Edaena Salinas <[email protected]> Co-authored-by: Yvonne Radsmikham <[email protected]> Co-authored-by: Dennis Seah <[email protected]>
1 parent 31b1ee9 commit ead0d68

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

src/commands/project/create-variable-group.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
} from "./create-variable-group";
3838
import * as createVariableGrp from "./create-variable-group";
3939
import * as fileutils from "../../lib/fileutils";
40+
import { getErrorMessage } from "../../lib/errorBuilder";
4041

4142
beforeAll(() => {
4243
enableVerboseLogging();
@@ -152,7 +153,9 @@ describe("create", () => {
152153
await create("", "", "", "", "", "", accessOpts);
153154
expect(true).toBeFalsy();
154155
} catch (e) {
155-
expect(e.message).toBe("Required values were missing");
156+
expect(e.message).toBe(
157+
getErrorMessage("project-create-variable-group-cmd-err-values-missing")
158+
);
156159
}
157160
});
158161

src/commands/project/create-variable-group.ts

+38-13
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import {
99
exit as exitCmd,
1010
validateForRequiredValues,
1111
} from "../../lib/commandBuilder";
12-
import {
13-
PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE,
14-
PROJECT_PIPELINE_FILENAME,
15-
} from "../../lib/constants";
12+
import { PROJECT_PIPELINE_FILENAME } from "../../lib/constants";
1613
import { AzureDevOpsOpts } from "../../lib/git";
1714
import { addVariableGroup } from "../../lib/pipelines/variableGroup";
1815
import {
@@ -28,6 +25,8 @@ import {
2825
VariableGroupDataVariable,
2926
} from "../../types";
3027
import decorator from "./create-variable-group.decorator.json";
28+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
29+
import { errorStatusCode } from "../../lib/errorStatusCode";
3130

3231
// values that we need to pull out from command operator
3332
interface CommandOptions {
@@ -44,7 +43,10 @@ interface CommandOptions {
4443
export const checkDependencies = (projectPath: string): void => {
4544
const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath);
4645
if (fileInfo.exist === false) {
47-
throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE);
46+
throw buildError(
47+
errorStatusCode.VALIDATION_ERR,
48+
"project-create-variable-group-cmd-err-dependency"
49+
);
4850
}
4951
};
5052

@@ -84,7 +86,10 @@ export const create = (
8486
!servicePrincipalPassword ||
8587
!tenantId
8688
) {
87-
throw Error("Required values were missing");
89+
throw buildError(
90+
errorStatusCode.VALIDATION_ERR,
91+
"project-create-variable-group-cmd-err-values-missing"
92+
);
8893
}
8994

9095
const vars: VariableGroupDataVariable = {
@@ -131,10 +136,16 @@ export const setVariableGroupInBedrockFile = (
131136
variableGroupName: string
132137
): void => {
133138
if (!hasValue(rootProjectPath)) {
134-
throw new Error("Project root path is not valid");
139+
throw buildError(
140+
errorStatusCode.VALIDATION_ERR,
141+
"project-create-variable-group-cmd-err-root-invalid"
142+
);
135143
}
136144
if (!hasValue(variableGroupName)) {
137-
throw new Error("Variable Group Name is not valid");
145+
throw buildError(
146+
errorStatusCode.VALIDATION_ERR,
147+
"project-create-variable-group-cmd-err-variable-group-invalid"
148+
);
138149
}
139150

140151
const absProjectRoot = path.resolve(rootProjectPath);
@@ -144,7 +155,10 @@ export const setVariableGroupInBedrockFile = (
144155
const bedrockFile = Bedrock(rootProjectPath);
145156

146157
if (typeof bedrockFile === "undefined") {
147-
throw Error(`Bedrock file does not exist.`);
158+
throw buildError(
159+
errorStatusCode.VALIDATION_ERR,
160+
"project-create-variable-group-cmd-err-bedrock-file-missing"
161+
);
148162
}
149163

150164
logger.verbose(
@@ -170,7 +184,10 @@ export const setVariableGroupInBedrockFile = (
170184
*/
171185
export const updateLifeCyclePipeline = (rootProjectPath: string): void => {
172186
if (!hasValue(rootProjectPath)) {
173-
throw Error("Project root path is not valid");
187+
throw buildError(
188+
errorStatusCode.VALIDATION_ERR,
189+
"project-create-variable-group-cmd-err-root-invalid"
190+
);
174191
}
175192

176193
const fileName = PROJECT_PIPELINE_FILENAME;
@@ -183,7 +200,10 @@ export const updateLifeCyclePipeline = (rootProjectPath: string): void => {
183200
) as AzurePipelinesYaml;
184201

185202
if (typeof pipelineFile === "undefined") {
186-
throw new Error("${fileName} file does not exist in ${absProjectRoot}.");
203+
throw buildError(errorStatusCode.VALIDATION_ERR, {
204+
errorKey: "project-create-variable-group-cmd-err-file-missing",
205+
values: [fileName, absProjectRoot],
206+
});
187207
}
188208

189209
logger.verbose(`${fileName} content: \n ${JSON.stringify(pipelineFile)}`);
@@ -297,8 +317,13 @@ export const execute = async (
297317
);
298318
await exitFn(0);
299319
} catch (err) {
300-
logger.error(`Error occurred while creating variable group`);
301-
logger.error(err);
320+
logError(
321+
buildError(
322+
errorStatusCode.CMD_EXE_ERR,
323+
"project-create-variable-group-cmd-failed",
324+
err
325+
)
326+
);
302327
await exitFn(1);
303328
}
304329
};

src/commands/project/init.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { exec } from "../../lib/shell";
1212
import { logger } from "../../logger";
1313
import { BedrockFile, MaintainersFile } from "../../types";
1414
import decorator from "./init.decorator.json";
15+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
16+
import { errorStatusCode } from "../../lib/errorStatusCode";
1517

1618
// values that we need to pull out from command operator
1719
interface CommandOptions {
@@ -172,8 +174,9 @@ export const execute = async (
172174
await initialize(projectPath, { defaultRing });
173175
await exitFn(0);
174176
} catch (err) {
175-
logger.error(`Error occurred while initializing project ${projectPath}`);
176-
logger.error(err);
177+
logError(
178+
buildError(errorStatusCode.EXE_FLOW_ERR, "project-init-cmd-failed", err)
179+
);
177180
await exitFn(1);
178181
}
179182
};

src/lib/i18n.json

+9
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@
223223
"variable-group-create-cmd-err-org-missing": "Provide a value for {0}.",
224224
"variable-group-create-cmd-err-file-missing": "Provide a file with the variable group manifest.",
225225

226+
"project-create-variable-group-cmd-failed": "Create variable group was not successfully executed.",
227+
"project-create-variable-group-cmd-err-root-invalid": "Project root path is not valid. Provide a valid root path.",
228+
"project-create-variable-group-cmd-err-variable-group-invalid": "Variable group name is not valid. Provide a valid variable group name.",
229+
"project-create-variable-group-cmd-err-bedrock-file-missing": "Bedrock file does not exist. Check that the project directory has been initialized.",
230+
"project-create-variable-group-cmd-err-file-missing": "The file '{0}' does not exist in project '{1}'.",
231+
"project-create-variable-group-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
232+
"project-create-variable-group-cmd-err-values-missing": "Required values were missing. Provide values for registry-name, hld-repo-url, service-principal-id, service-principal-password, tenant.",
233+
"project-init-cmd-failed": "Project init was not successfully executed.",
234+
226235
"validation-err-missing-vals": "These mandatory options were missing:\n {0}. Provide them.",
227236
"validation-err-org-name-missing": "Organization name was missing. Provide it.",
228237
"validation-err-org-name": "Organization name must start with a letter or number, followed by letters, numbers or hyphens, and must end with a letter or number.",

0 commit comments

Comments
 (0)