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

Commit 343547d

Browse files
authored
Add error codes to ring commands (#501)
1 parent a3d83fe commit 343547d

File tree

8 files changed

+82
-35
lines changed

8 files changed

+82
-35
lines changed

docs/commands/data.json

+4-8
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@
399399
{
400400
"arg": "-U, --hld-repo-url <hld-repo-url>",
401401
"description": "The high level definition (HLD) git repo url; falls back to azure_devops.org in spk config.",
402-
"required": true,
403-
"inherit": "azure_devops.hld_repository"
402+
"required": true
404403
},
405404
{
406405
"arg": "-u, --service-principal-id <service-principal-id>",
@@ -420,20 +419,17 @@
420419
{
421420
"arg": "-o, --org-name <organization-name>",
422421
"description": "Azure DevOps organization name; falls back to azure_devops.org in spk config.",
423-
"required": true,
424-
"inherit": "azure_devops.org"
422+
"required": true
425423
},
426424
{
427425
"arg": "-d, --devops-project <project>",
428426
"description": "Azure DevOps project name; falls back to azure_devops.project in spk config.",
429-
"required": true,
430-
"inherit": "azure_devops.project"
427+
"required": true
431428
},
432429
{
433430
"arg": "-a, --personal-access-token <personal-access-token>",
434431
"description": "Azure DevOps Personal access token; falls back to azure_devops.access_token in spk config.",
435-
"required": true,
436-
"inherit": "azure_devops.access_token"
432+
"required": true
437433
}
438434
],
439435
"markdown": "## Description\n\nCreate new variable group in Azure DevOps project\n\n## Command Prerequisites\n\nIn addition to an existing\n[Azure DevOps project](https://azure.microsoft.com/en-us/services/devops/), to\nlink secrets from an Azure key vault as variables in Variable Group, you will\nneed an existing key vault containing your secrets and the Service Principal for\nauthorization with Azure Key Vault.\n\n1. Use existng or\n [create a service principal either in Azure Portal](https://docs.microsoft.com/en-us/azure/active-directory/develop/howto-create-service-principal-portal)\n or\n [with Azure CLI](https://docs.microsoft.com/en-us/cli/azure/create-an-azure-service-principal-azure-cli?view=azure-cli-latest).\n2. Use existing or\n [create a Azure Container Registry in Azure Portal](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-portal)\n or\n [with Azure CLI](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-get-started-azure-cli).\n"

src/commands/project/pipeline.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
getRepositoryName,
2323
getRepositoryUrl,
2424
isGitHubUrl,
25-
validateRepoUrl
25+
validateRepoUrl,
2626
} from "../../lib/gitutils";
2727
import {
2828
createPipelineForDefinition,
@@ -98,16 +98,15 @@ export const fetchValidateValues = (
9898
);
9999
}
100100
const azureDevops = spkConfig?.azure_devops;
101-
const repoUrl = validateRepoUrl(opts,gitOriginUrl);
101+
const repoUrl = validateRepoUrl(opts, gitOriginUrl);
102102
const values: CommandOptions = {
103103
buildScriptUrl: opts.buildScriptUrl || BUILD_SCRIPT_URL,
104104
devopsProject: opts.devopsProject || azureDevops?.project,
105105
orgName: opts.orgName || azureDevops?.org,
106106
personalAccessToken: opts.personalAccessToken || azureDevops?.access_token,
107107
pipelineName:
108108
opts.pipelineName || getRepositoryName(gitOriginUrl) + "-lifecycle",
109-
repoName:
110-
getRepositoryName(repoUrl) || getRepositoryName(gitOriginUrl),
109+
repoName: getRepositoryName(repoUrl) || getRepositoryName(gitOriginUrl),
111110
repoUrl: opts.repoUrl || getRepositoryUrl(gitOriginUrl),
112111
yamlFileBranch: opts.yamlFileBranch,
113112
};

src/commands/ring/create.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@ import {
55
read as loadBedrockFile,
66
} from "../../lib/bedrockYaml";
77
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
8-
import {
9-
BEDROCK_FILENAME,
10-
PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE,
11-
} from "../../lib/constants";
8+
import { BEDROCK_FILENAME } from "../../lib/constants";
129
import { updateTriggerBranchesForServiceBuildAndUpdatePipeline } from "../../lib/fileutils";
1310
import * as dns from "../../lib/net/dns";
1411
import { hasValue } from "../../lib/validator";
1512
import { logger } from "../../logger";
1613
import { BedrockFile, BedrockFileInfo } from "../../types";
1714
import decorator from "./create.decorator.json";
15+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
16+
import { errorStatusCode } from "../../lib/errorStatusCode";
1817

1918
/**
2019
* Check for bedrock.yaml
@@ -28,15 +27,19 @@ export const checkDependencies = (
2827
): void => {
2928
const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath);
3029
if (fileInfo.exist === false) {
31-
throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE);
30+
throw buildError(
31+
errorStatusCode.VALIDATION_ERR,
32+
"ring-create-cmd-err-dependency"
33+
);
3234
}
3335

3436
// Check if ring already exists, if it does, warn and exit
3537
const bedrockFile: BedrockFile = loadBedrockFile(projectPath);
3638
if (ringName in bedrockFile.rings) {
37-
throw new Error(
38-
`ring: ${ringName} already exists in project ${BEDROCK_FILENAME}.`
39-
);
39+
throw buildError(errorStatusCode.EXE_FLOW_ERR, {
40+
errorKey: "ring-create-cmd-err-ring-exists",
41+
values: [ringName, BEDROCK_FILENAME],
42+
});
4043
}
4144
};
4245

@@ -52,7 +55,12 @@ export const execute = async (
5255
exitFn: (status: number) => Promise<void>
5356
): Promise<void> => {
5457
if (!hasValue(ringName)) {
55-
logger.error(`No ring name given.`);
58+
logError(
59+
buildError(
60+
errorStatusCode.VALIDATION_ERR,
61+
"ring-create-cmd-err-name-missing"
62+
)
63+
);
5664
await exitFn(1);
5765
return;
5866
}
@@ -82,8 +90,16 @@ export const execute = async (
8290
logger.info(`Successfully created ring: ${ringName} for this project!`);
8391
await exitFn(0);
8492
} catch (err) {
85-
logger.error(`Error occurred while creating ring: ${ringName}`);
86-
logger.error(err);
93+
logError(
94+
buildError(
95+
errorStatusCode.CMD_EXE_ERR,
96+
{
97+
errorKey: "ring-create-cmd-failed",
98+
values: [ringName],
99+
},
100+
err
101+
)
102+
);
87103
await exitFn(1);
88104
}
89105
};

src/commands/ring/delete.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import commander from "commander";
22
import * as bedrock from "../../lib/bedrockYaml";
33
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
4-
import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants";
54
import { updateTriggerBranchesForServiceBuildAndUpdatePipeline } from "../../lib/fileutils";
65
import { hasValue } from "../../lib/validator";
76
import { logger } from "../../logger";
87
import { BedrockFileInfo } from "../../types";
98
import decorator from "./delete.decorator.json";
9+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
10+
import { errorStatusCode } from "../../lib/errorStatusCode";
1011

1112
/**
1213
* Check the bedrock.yaml and the target ring exists
@@ -15,7 +16,10 @@ import decorator from "./delete.decorator.json";
1516
export const checkDependencies = (projectPath: string): void => {
1617
const fileInfo: BedrockFileInfo = bedrock.fileInfo(projectPath);
1718
if (fileInfo.exist === false) {
18-
throw Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE);
19+
throw buildError(
20+
errorStatusCode.VALIDATION_ERR,
21+
"ring-delete-cmd-err-dependency"
22+
);
1923
}
2024
};
2125

@@ -60,8 +64,16 @@ export const execute = async (
6064
logger.info(`Successfully deleted ring: ${ringName} from this project!`);
6165
await exitFn(0);
6266
} catch (err) {
63-
logger.error(`Error occurred while deleting ring: ${ringName}`);
64-
logger.error(err);
67+
logError(
68+
buildError(
69+
errorStatusCode.EXE_FLOW_ERR,
70+
{
71+
errorKey: "ring-delete-cmd-failed",
72+
values: [ringName],
73+
},
74+
err
75+
)
76+
);
6577
await exitFn(1);
6678
}
6779
};

src/commands/ring/set-default.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import {
55
setDefaultRing,
66
} from "../../lib/bedrockYaml";
77
import { build as buildCmd, exit as exitCmd } from "../../lib/commandBuilder";
8-
import { PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE } from "../../lib/constants";
98
import { hasValue } from "../../lib/validator";
109
import { logger } from "../../logger";
1110
import { BedrockFileInfo } from "../../types";
1211
import decorator from "./set-default.decorator.json";
12+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
13+
import { errorStatusCode } from "../../lib/errorStatusCode";
1314

1415
/**
1516
* Check for bedrock.yaml
@@ -18,7 +19,10 @@ import decorator from "./set-default.decorator.json";
1819
export const checkDependencies = (projectPath: string): void => {
1920
const fileInfo: BedrockFileInfo = bedrockFileInfo(projectPath);
2021
if (fileInfo.exist === false) {
21-
throw new Error(PROJECT_INIT_DEPENDENCY_ERROR_MESSAGE);
22+
throw buildError(
23+
errorStatusCode.VALIDATION_ERR,
24+
"ring-set-default-cmd-err-dependency"
25+
);
2226
}
2327
};
2428

@@ -50,8 +54,16 @@ export const execute = async (
5054
logger.info(`Successfully set default ring: ${ringName} for this project!`);
5155
await exitFn(0);
5256
} catch (err) {
53-
logger.error(`Error occurred while setting default ring: ${ringName}`);
54-
logger.error(err);
57+
logError(
58+
buildError(
59+
errorStatusCode.EXE_FLOW_ERR,
60+
{
61+
errorKey: "ring-set-default-cmd-failed",
62+
values: [ringName],
63+
},
64+
err
65+
)
66+
);
5567
await exitFn(1);
5668
}
5769
};

src/commands/service/pipeline.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
getRepositoryName,
1919
getRepositoryUrl,
2020
isGitHubUrl,
21-
validateRepoUrl
21+
validateRepoUrl,
2222
} from "../../lib/gitutils";
2323
import {
2424
createPipelineForDefinition,
@@ -87,7 +87,6 @@ export const requiredPipelineVariables = (
8787
};
8888
};
8989

90-
9190
/**
9291
* Install a pipeline for the service in an azure devops org.
9392
*

src/lib/gitutils.test.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,9 @@ describe("Returns an azure devops git repo url if it is defined", () => {
586586
yamlFileBranch: "master",
587587
};
588588
const gitUrl = "https://github.com/CatalystCode/spk.git";
589-
expect(validateRepoUrl(mockValues, gitUrl)).toBe("https://dev.azure.com/myOrg/myProject/_git/myRepo");
589+
expect(validateRepoUrl(mockValues, gitUrl)).toBe(
590+
"https://dev.azure.com/myOrg/myProject/_git/myRepo"
591+
);
590592
});
591593
it("another positive test", async () => {
592594
const mockValues: ConfigValues = {
@@ -600,6 +602,8 @@ describe("Returns an azure devops git repo url if it is defined", () => {
600602
yamlFileBranch: "master",
601603
};
602604
const gitUrl = "https://github.com/CatalystCode/spk";
603-
expect(validateRepoUrl(mockValues, gitUrl)).toBe("https://github.com/CatalystCode/spk");
605+
expect(validateRepoUrl(mockValues, gitUrl)).toBe(
606+
"https://github.com/CatalystCode/spk"
607+
);
604608
});
605609
});

src/lib/i18n.json

+9
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@
100100
"deployment-table-add-src-to-acr-pipeline": "Could not add source to ACR pipeline information to storage table.",
101101
"deployment-table-add-acr-to-hld-pipeline": "Could not add ACR to HLD pipeline information to storage table.",
102102

103+
"ring-create-cmd-failed": "Error occurred while creating ring: {0}",
104+
"ring-create-cmd-err-ring-exists": "Could not create ring {0} in project {1} because it already exists. Provide a different name.",
105+
"ring-create-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
106+
"ring-delete-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
107+
"ring-set-default-cmd-err-dependency": "Please run `spk project init` command before running this command to initialize the project.",
108+
"ring-create-cmd-err-name-missing": "No ring name was provided. Provide a ring name",
109+
"ring-delete-cmd-failed": "Error occurred while deleting ring: {0}.",
110+
"ring-set-default-cmd-failed": "Error occurred while setting default ring: {0}",
111+
103112
"validation-err-org-name-missing": "Organization name was missing. Provide it.",
104113
"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.",
105114
"validation-err-password-missing": "Password was missing. Provide it.",

0 commit comments

Comments
 (0)