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

Commit c21d4aa

Browse files
authored
Add error codes in reconcile (#494)
1 parent 871828d commit c21d4aa

File tree

3 files changed

+71
-27
lines changed

3 files changed

+71
-27
lines changed

docs/commands/data.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,4 +674,4 @@
674674
}
675675
]
676676
}
677-
}
677+
}

src/commands/hld/reconcile.ts

+59-26
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import * as middleware from "../../lib/traefik/middleware";
1818
import { logger } from "../../logger";
1919
import { BedrockFile, BedrockServiceConfig } from "../../types";
2020
import decorator from "./reconcile.decorator.json";
21+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
22+
import { errorStatusCode } from "../../lib/errorStatusCode";
2123

2224
/**
2325
* IExecResult represents the possible return value of a Promise based wrapper
@@ -70,8 +72,14 @@ export const execAndLog = async (commandToRun: string): Promise<ExecResult> => {
7072
const pipeOutputToCurrentShell = true;
7173
const result = await exec(commandToRun, pipeOutputToCurrentShell);
7274
if (result.error) {
73-
logger.error(`an error occurred executing command: \`${commandToRun}\``);
74-
throw result.error;
75+
throw buildError(
76+
errorStatusCode.CMD_EXE_ERR,
77+
{
78+
errorKey: "hld-reconcile-err-cmd-failed",
79+
values: [commandToRun],
80+
},
81+
result.error
82+
);
7583
}
7684
return result;
7785
};
@@ -107,10 +115,14 @@ export const createRepositoryComponent = async (
107115
return execCmd(
108116
`cd ${absHldPath} && mkdir -p ${repositoryName} && fab add ${repositoryName} --path ./${repositoryName} --method local`
109117
).catch((err) => {
110-
logger.error(
111-
`error creating repository component '${repositoryName}' in path '${absHldPath}'`
118+
throw buildError(
119+
errorStatusCode.EXE_FLOW_ERR,
120+
{
121+
errorKey: "hld-reconcile-err-repo-create",
122+
values: [repositoryName, absHldPath],
123+
},
124+
err
112125
);
113-
throw err;
114126
});
115127
};
116128

@@ -142,8 +154,11 @@ export const configureChartForRing = async (
142154
const fabConfigureCommand = `cd ${normalizedRingPathInHld} && fab set --subcomponent "chart" serviceName="${k8sSvcBackendAndName}"`;
143155

144156
return execCmd(fabConfigureCommand).catch((err) => {
145-
logger.error(`error configuring helm chart for service `);
146-
throw err;
157+
throw buildError(
158+
errorStatusCode.EXE_FLOW_ERR,
159+
"hld-reconcile-err-helm-config",
160+
err
161+
);
147162
});
148163
};
149164

@@ -160,10 +175,14 @@ export const createServiceComponent = async (
160175
return execCmd(
161176
`cd ${absRepositoryInHldPath} && mkdir -p ${serviceName} config && fab add ${serviceName} --path ./${serviceName} --method local --type component && touch ./config/common.yaml`
162177
).catch((err) => {
163-
logger.error(
164-
`error creating service component '${serviceName}' in path '${absRepositoryInHldPath}'`
178+
throw buildError(
179+
errorStatusCode.EXE_FLOW_ERR,
180+
{
181+
errorKey: "hld-reconcile-err-service-create",
182+
values: [serviceName, absRepositoryInHldPath],
183+
},
184+
err
165185
);
166-
throw err;
167186
});
168187
};
169188

@@ -177,10 +196,14 @@ export const createRingComponent = async (
177196
const createRingInSvcCommand = `cd ${svcPathInHld} && mkdir -p ${normalizedRingName} config && fab add ${normalizedRingName} --path ./${normalizedRingName} --method local --type component && touch ./config/common.yaml`;
178197

179198
return execCmd(createRingInSvcCommand).catch((err) => {
180-
logger.error(
181-
`error creating ring component '${normalizedRingName}' in path '${svcPathInHld}'`
199+
throw buildError(
200+
errorStatusCode.EXE_FLOW_ERR,
201+
{
202+
errorKey: "hld-reconcile-err-ring-create",
203+
values: [normalizedRingName, svcPathInHld],
204+
},
205+
err
182206
);
183-
throw err;
184207
});
185208
};
186209

@@ -211,12 +234,14 @@ export const addChartToRing = async (
211234

212235
return execCmd(`cd ${ringPathInHld} && ${addHelmChartCommand}`).catch(
213236
(err) => {
214-
logger.error(
215-
`error adding helm chart for service-config ${JSON.stringify(
216-
serviceConfig
217-
)} to ring path '${ringPathInHld}'`
237+
throw buildError(
238+
errorStatusCode.EXE_FLOW_ERR,
239+
{
240+
errorKey: "hld-reconcile-err-helm-add",
241+
values: [JSON.stringify(serviceConfig), ringPathInHld],
242+
},
243+
err
218244
);
219-
throw err;
220245
}
221246
);
222247
};
@@ -229,8 +254,14 @@ export const createStaticComponent = async (
229254
const createConfigAndStaticComponentCommand = `cd ${ringPathInHld} && mkdir -p config static && fab add static --path ./static --method local --type static && touch ./config/common.yaml`;
230255

231256
return execCmd(createConfigAndStaticComponentCommand).catch((err) => {
232-
logger.error(`error creating static component in path '${ringPathInHld}'`);
233-
throw err;
257+
throw buildError(
258+
errorStatusCode.EXE_FLOW_ERR,
259+
{
260+
errorKey: "hld-reconcile-err-static-create",
261+
values: [ringPathInHld],
262+
},
263+
err
264+
);
234265
});
235266
};
236267

@@ -379,9 +410,7 @@ export const validateInputs = (
379410
export const checkForFabrikate = (which: (path: string) => string): void => {
380411
const fabrikateInstalled = which("fab");
381412
if (fabrikateInstalled === "") {
382-
throw ReferenceError(
383-
`Fabrikate not installed. Please fetch and install the latest version: https://github.com/microsoft/fabrikate/releases`
384-
);
413+
throw buildError(errorStatusCode.VALIDATION_ERR, "hld-reconcile-err-fab");
385414
}
386415
};
387416

@@ -393,7 +422,10 @@ export const testAndGetAbsPath = (
393422
): string => {
394423
const absPath = path.resolve(possiblyRelativePath);
395424
if (!test("-e", absPath) && !test("-d", absPath)) {
396-
throw Error(`Could not validate ${pathType} path.`);
425+
throw buildError(errorStatusCode.VALIDATION_ERR, {
426+
errorKey: "hld-reconcile-err-path",
427+
values: [pathType],
428+
});
397429
}
398430
log(`Found ${pathType} at ${absPath}`);
399431
return absPath;
@@ -632,8 +664,9 @@ export const execute = async (
632664
);
633665
await exitFn(0);
634666
} catch (err) {
635-
logger.error(`An error occurred while reconciling HLD`);
636-
logger.error(err);
667+
logError(
668+
buildError(errorStatusCode.CMD_EXE_ERR, "hld-reconcile-err-cmd-exe", err)
669+
);
637670
await exitFn(1);
638671
}
639672
};

src/lib/i18n.json

+11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
"hld-init-cmd-failed": "Hld init command was not successfully executed.",
2020
"hld-init-cmd-project-path-missing": "Value for project path was not provided. Provide it.",
2121

22+
"hld-reconcile-err-repo-create": "Error creating repository component '{0}' in path '{1}'.",
23+
"hld-reconcile-err-helm-config": "Error configuring helm chart for service.",
24+
"hld-reconcile-err-service-create": "Error creating service component '{0}' in path '{1}'.",
25+
"hld-reconcile-err-ring-create": "`error creating ring component '{0}' in path '{1}'.",
26+
"hld-reconcile-err-helm-add": "Error adding helm chart for service-config {0} to ring path '{1}'.",
27+
"hld-reconcile-err-static-create": "Error creating static component in path '{0}'.",
28+
"hld-reconcile-err-fab": "Fabrikate not installed. Please fetch and install the latest version: https://github.com/microsoft/fabrikate/releases",
29+
"hld-reconcile-err-path": "Could not validate {0} path.",
30+
"hld-reconcile-err-cmd-failed": "An error occurred executing command: {0}",
31+
"hld-reconcile-err-cmd-exe": "An error occurred while reconciling HLD.",
32+
2233
"infra-scaffold-cmd-failed": "Infra scaffold command was not successfully executed.",
2334
"infra-scaffold-cmd-src-missing": "Value for source is required because it cannot be constructed with properties in spk-config.yaml. Provide value for source.",
2435
"infra-scaffold-cmd-values-missing": "Values for name, version and/or 'template were missing. Provide value for values for them.",

0 commit comments

Comments
 (0)