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

Commit 2758c12

Browse files
[FEATURE] Add error status code to deployment dashboard command (#485)
* [FEATURE] Add error status code to deployment dashboard command * Update dashboard.test.ts Co-authored-by: Samiya Akhtar <[email protected]>
1 parent 8fca041 commit 2758c12

File tree

4 files changed

+117
-68
lines changed

4 files changed

+117
-68
lines changed

src/commands/deployment/dashboard.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ describe("Test validateValues function", () => {
7979
expect(true).toBe(false);
8080
} catch (e) {
8181
expect(e.message).toBe(
82-
"value for port option has to be a valid port number"
82+
"introspect-dashboard-cmd-invalid-port: value for port option has to be a valid port number. Enter a valid port number."
8383
);
8484
}
8585
});
@@ -95,7 +95,7 @@ describe("Test validateValues function", () => {
9595
expect(true).toBe(false);
9696
} catch (e) {
9797
expect(e.message).toBe(
98-
"You need to specify configuration for your introspection storage account and DevOps pipeline to run this dashboard. Please initialize the spk tool with the right configuration"
98+
"introspect-dashboard-cmd-missing-vals: Configuration for storage account and DevOps pipeline were missing. Initialize the spk tool with the right configuration."
9999
);
100100
}
101101
});

src/commands/deployment/dashboard.ts

+106-66
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ import { isPortNumberString, validatePrereqs } from "../../lib/validator";
99
import { logger } from "../../logger";
1010
import { ConfigYaml } from "../../types";
1111
import decorator from "./dashboard.decorator.json";
12+
import {
13+
build as buildError,
14+
log as logError,
15+
build,
16+
} from "../../lib/errorBuilder";
17+
import { errorStatusCode } from "../../lib/errorStatusCode";
1218

1319
export interface IntrospectionManifest {
1420
githubUsername?: string;
@@ -49,7 +55,10 @@ export const validateValues = (
4955
): DashboardConfig => {
5056
if (opts.port) {
5157
if (!isPortNumberString(opts.port)) {
52-
throw new Error("value for port option has to be a valid port number");
58+
throw buildError(
59+
errorStatusCode.VALIDATION_ERR,
60+
"introspect-dashboard-cmd-invalid-port"
61+
);
5362
}
5463
}
5564

@@ -66,8 +75,9 @@ export const validateValues = (
6675
!config.introspection.dashboard ||
6776
!config.introspection.dashboard.image
6877
) {
69-
throw new Error(
70-
"You need to specify configuration for your introspection storage account and DevOps pipeline to run this dashboard. Please initialize the spk tool with the right configuration"
78+
throw buildError(
79+
errorStatusCode.VALIDATION_ERR,
80+
"introspect-dashboard-cmd-missing-vals"
7181
);
7282
}
7383

@@ -92,21 +102,29 @@ export const validateValues = (
92102
export const cleanDashboardContainers = async (
93103
config: DashboardConfig
94104
): Promise<void> => {
95-
let dockerOutput = await exec("docker", [
96-
"ps",
97-
"-a",
98-
"-q",
99-
"--filter",
100-
"ancestor=" + config.image,
101-
'--format="{{.ID}}"',
102-
]);
103-
if (dockerOutput.length > 0) {
104-
dockerOutput = dockerOutput.replace(/\n/g, " ");
105-
dockerOutput = dockerOutput.replace(/"/g, "");
106-
const containerIds = dockerOutput.split(" ");
107-
const args = ["kill", ...containerIds];
105+
try {
106+
let dockerOutput = await exec("docker", [
107+
"ps",
108+
"-a",
109+
"-q",
110+
"--filter",
111+
"ancestor=" + config.image,
112+
'--format="{{.ID}}"',
113+
]);
114+
if (dockerOutput.length > 0) {
115+
dockerOutput = dockerOutput.replace(/\n/g, " ");
116+
dockerOutput = dockerOutput.replace(/"/g, "");
117+
const containerIds = dockerOutput.split(" ");
118+
const args = ["kill", ...containerIds];
108119

109-
await exec("docker", args);
120+
await exec("docker", args);
121+
}
122+
} catch (err) {
123+
throw buildError(
124+
errorStatusCode.DOCKER_ERR,
125+
"introspect-dashboard-cmd-kill-docker-container",
126+
err
127+
);
110128
}
111129
};
112130

@@ -143,60 +161,70 @@ export const extractManifestRepositoryInformation = (
143161
export const getEnvVars = async (
144162
config: DashboardConfig
145163
): Promise<string[]> => {
146-
const envVars = [
147-
"-e",
148-
`REACT_APP_PIPELINE_ORG=${config.org}`,
149-
"-e",
150-
`REACT_APP_PIPELINE_PROJECT=${config.project}`,
151-
"-e",
152-
`REACT_APP_STORAGE_ACCOUNT_NAME=${config.accountName}`,
153-
"-e",
154-
`REACT_APP_STORAGE_PARTITION_KEY=${config.partitionKey}`,
155-
"-e",
156-
`REACT_APP_STORAGE_TABLE_NAME=${config.tableName}`,
157-
"-e",
158-
`REACT_APP_STORAGE_ACCESS_KEY=${config.key}`,
159-
];
164+
try {
165+
const envVars = [
166+
"-e",
167+
`REACT_APP_PIPELINE_ORG=${config.org}`,
168+
"-e",
169+
`REACT_APP_PIPELINE_PROJECT=${config.project}`,
170+
"-e",
171+
`REACT_APP_STORAGE_ACCOUNT_NAME=${config.accountName}`,
172+
"-e",
173+
`REACT_APP_STORAGE_PARTITION_KEY=${config.partitionKey}`,
174+
"-e",
175+
`REACT_APP_STORAGE_TABLE_NAME=${config.tableName}`,
176+
"-e",
177+
`REACT_APP_STORAGE_ACCESS_KEY=${config.key}`,
178+
];
160179

161-
if (config.accessToken) {
162-
envVars.push("-e");
163-
envVars.push(`REACT_APP_PIPELINE_ACCESS_TOKEN=${config.accessToken}`);
180+
if (config.accessToken) {
181+
envVars.push("-e");
182+
envVars.push(`REACT_APP_PIPELINE_ACCESS_TOKEN=${config.accessToken}`);
164183

165-
if (!config.sourceRepoAccessToken) {
184+
if (!config.sourceRepoAccessToken) {
185+
envVars.push("-e");
186+
envVars.push(
187+
`REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${config.accessToken}`
188+
);
189+
envVars.push("-e");
190+
envVars.push(`REACT_APP_MANIFEST_ACCESS_TOKEN=${config.accessToken}`);
191+
}
192+
} else {
193+
logger.warn(
194+
"Pipeline access token was not specified during init, dashboard may show empty results if pipelines are private"
195+
);
196+
}
197+
if (config.sourceRepoAccessToken) {
166198
envVars.push("-e");
167-
envVars.push(`REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${config.accessToken}`);
199+
envVars.push(
200+
`REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${config.sourceRepoAccessToken}`
201+
);
168202
envVars.push("-e");
169-
envVars.push(`REACT_APP_MANIFEST_ACCESS_TOKEN=${config.accessToken}`);
203+
envVars.push(
204+
`REACT_APP_MANIFEST_ACCESS_TOKEN=${config.sourceRepoAccessToken}`
205+
);
170206
}
171-
} else {
172-
logger.warn(
173-
"Pipeline access token was not specified during init, dashboard may show empty results if pipelines are private"
174-
);
175-
}
176-
if (config.sourceRepoAccessToken) {
177-
envVars.push("-e");
178-
envVars.push(
179-
`REACT_APP_SOURCE_REPO_ACCESS_TOKEN=${config.sourceRepoAccessToken}`
180-
);
181-
envVars.push("-e");
182-
envVars.push(
183-
`REACT_APP_MANIFEST_ACCESS_TOKEN=${config.sourceRepoAccessToken}`
184-
);
185-
}
186207

187-
const manifestRepo = extractManifestRepositoryInformation(config);
188-
if (manifestRepo) {
189-
envVars.push("-e");
190-
envVars.push(`REACT_APP_MANIFEST=${manifestRepo.manifestRepoName}`);
191-
if (manifestRepo.githubUsername) {
208+
const manifestRepo = extractManifestRepositoryInformation(config);
209+
if (manifestRepo) {
192210
envVars.push("-e");
193-
envVars.push(
194-
`REACT_APP_GITHUB_MANIFEST_USERNAME=${manifestRepo.githubUsername}`
195-
);
211+
envVars.push(`REACT_APP_MANIFEST=${manifestRepo.manifestRepoName}`);
212+
if (manifestRepo.githubUsername) {
213+
envVars.push("-e");
214+
envVars.push(
215+
`REACT_APP_GITHUB_MANIFEST_USERNAME=${manifestRepo.githubUsername}`
216+
);
217+
}
196218
}
197-
}
198219

199-
return envVars;
220+
return envVars;
221+
} catch (err) {
222+
throw buildError(
223+
errorStatusCode.ENV_SETTING_ERR,
224+
"introspect-dashboard-cmd-get-env",
225+
err
226+
);
227+
}
200228
};
201229

202230
/**
@@ -211,7 +239,10 @@ export const launchDashboard = async (
211239
): Promise<string> => {
212240
try {
213241
if (!validatePrereqs(["docker"], false)) {
214-
throw new Error("Requirements to launch dashboard are not met");
242+
throw buildError(
243+
errorStatusCode.DOCKER_ERR,
244+
"introspect-dashboard-cmd-launch-pre-req-err"
245+
);
215246
}
216247

217248
if (removeAll) {
@@ -233,8 +264,11 @@ export const launchDashboard = async (
233264
]);
234265
return containerId;
235266
} catch (err) {
236-
logger.error(`Error occurred while launching dashboard ${err}`);
237-
throw err;
267+
throw buildError(
268+
errorStatusCode.DOCKER_ERR,
269+
"introspect-dashboard-cmd-launch-err",
270+
err
271+
);
238272
}
239273
};
240274

@@ -257,7 +291,13 @@ export const execute = async (
257291
}
258292
await exitFn(0);
259293
} catch (err) {
260-
logger.error(err);
294+
logError(
295+
buildError(
296+
errorStatusCode.CMD_EXE_ERR,
297+
"introspect-dashboard-cmd-failed",
298+
err
299+
)
300+
);
261301
await exitFn(1);
262302
}
263303
};

src/lib/errorStatusCode.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export enum errorStatusCode {
1010
INCORRECT_DEFINITION = 1012,
1111
GIT_OPS_ERR = 1100,
1212
AZURE_STORAGE_OP_ERR = 2000,
13+
DOCKER_ERR = 3000,
1314
}

src/lib/i18n.json

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@
7878
"introspect-create-cmd-cmd-p1-missing-values": "Values for image-tag, commit-id and service options were missing. They are required for updating the details of source pipeline. Provide them.",
7979
"introspect-create-cmd-cmd-p2-missing-values": "Values for p2, hld-commit-id, image-tag and env options were missing. They are required For updating the details of image tag release pipeline. Provide them.",
8080

81+
"introspect-dashboard-cmd-failed": "Deployment dashboard command was not successfully executed.",
82+
"introspect-dashboard-cmd-invalid-port": "value for port option has to be a valid port number. Enter a valid port number.",
83+
"introspect-dashboard-cmd-missing-vals": "Configuration for storage account and DevOps pipeline were missing. Initialize the spk tool with the right configuration.",
84+
"introspect-dashboard-cmd-kill-docker-container": "Could not kill dashboard docker containers.",
85+
"introspect-dashboard-cmd-get-env": "Could not get environment parameters.",
86+
"introspect-dashboard-cmd-launch-pre-req-err": "Requirements to launch dashboard were not met",
87+
"introspect-dashboard-cmd-launch-err": "Could not launch dashboard docker container.",
88+
8189
"deployment-table-update-hld-manifest-pipeline-failed": "Could not update HLD to manifest pipeline.",
8290
"deployment-table-update-manifest-commit-id-failed": "Could not update manifest commit Id.",
8391
"deployment-table-update-manifest-commit-id-failed-no-generation": "No manifest generation found to update manifest commit {0}."

0 commit comments

Comments
 (0)