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

Commit c139938

Browse files
edaenaEdaena Salinasdennisseahsamiyaakhtar
authored
Add error codes to service commands (#533)
Co-authored-by: Edaena Salinas <[email protected]> Co-authored-by: Dennis Seah <[email protected]> Co-authored-by: Samiya Akhtar <[email protected]>
1 parent 543e518 commit c139938

File tree

5 files changed

+26
-17
lines changed

5 files changed

+26
-17
lines changed

src/commands/service/create-revision.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { write } from "../../config";
21
import * as config from "../../config";
32
import * as bedrockYaml from "../../lib/bedrockYaml";
43
import * as azure from "../../lib/git/azure";

src/commands/service/create-revision.ts

+20-14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import { hasValue } from "../../lib/validator";
1717
import { logger } from "../../logger";
1818
import { BedrockFile } from "../../types";
1919
import decorator from "./create-revision.decorator.json";
20+
import { build as buildError, log as logError } from "../../lib/errorBuilder";
21+
import { errorStatusCode } from "../../lib/errorStatusCode";
2022

2123
export interface CommandOptions {
2224
sourceBranch: string | undefined;
@@ -69,12 +71,10 @@ export const getDefaultRings = (
6971
.filter((ring) => !!ring.isDefault)
7072
.map((ring) => ring.branch);
7173
if (defaultRings.length === 0) {
72-
throw Error(
73-
`Default branches/rings must either be specified in ${join(
74-
__dirname,
75-
"bedrock.yaml"
76-
)} or provided via --target-branch`
77-
);
74+
throw buildError(errorStatusCode.VALIDATION_ERR, {
75+
errorKey: "service-create-revision-cmd-err-default-branch-missing",
76+
values: [join(__dirname, "bedrock.yaml")],
77+
});
7878
}
7979
logger.info(
8080
`Creating pull request against branches: ${defaultRings.join(", ")}`
@@ -97,8 +97,9 @@ export const getSourceBranch = async (
9797
);
9898
sourceBranch = await getCurrentBranch();
9999
if (sourceBranch.length === 0) {
100-
throw Error(
101-
`Zero length branch string parsed from git client; cannot automate PR`
100+
throw buildError(
101+
errorStatusCode.VALIDATION_ERR,
102+
"service-create-revision-cmd-err-source-branch-missing"
102103
);
103104
}
104105
}
@@ -167,17 +168,22 @@ export const execute = async (
167168

168169
// Make sure the user isn't trying to make a PR for a branch against itself
169170
if (defaultRings.includes(values.sourceBranch)) {
170-
throw Error(
171-
`A pull request for a branch cannot be made against itself. Ensure your target branch(es) '${JSON.stringify(
172-
defaultRings
173-
)}' do not include your source branch '${values.sourceBranch}'`
174-
);
171+
throw buildError(errorStatusCode.VALIDATION_ERR, {
172+
errorKey: "service-create-revision-cmd-err-pr-source-branch",
173+
values: [JSON.stringify(defaultRings), values.sourceBranch],
174+
});
175175
}
176176

177177
await makePullRequest(defaultRings, values);
178178
await exitFn(0);
179179
} catch (err) {
180-
logger.error(err);
180+
logError(
181+
buildError(
182+
errorStatusCode.CMD_EXE_ERR,
183+
"service-create-revision-cmd-failed",
184+
err
185+
)
186+
);
181187
await exitFn(1);
182188
}
183189
};

src/commands/service/create.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
CommandValues,
2828
validateGitUrl,
2929
} from "./create";
30-
import { BedrockServiceConfig, HelmConfig } from "../../types";
30+
import { BedrockServiceConfig } from "../../types";
3131

3232
jest.mock("../../lib/gitutils");
3333

src/lib/i18n.json

+5
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@
257257
"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.",
258258
"project-init-cmd-failed": "Project init was not successfully executed.",
259259

260+
"service-create-revision-cmd-failed": "Service create revision was not successfully executed.",
261+
"service-create-revision-cmd-err-pr-source-branch": "A pull request for a branch cannot be made against itself. Ensure your target branch(es) '{0}' do not include your source branch '{1}'",
262+
"service-create-revision-cmd-err-default-branch-missing": "Default branch/ring was not found. Specify the default branch/ring in {0} or provide it via --target-branch",
263+
"service-create-revision-cmd-err-source-branch-missing": "A source branch was not provided.",
264+
260265
"validation-err-missing-vals": "These mandatory options were missing:\n {0}. Provide them.",
261266
"validation-err-org-name-missing": "Organization name was missing. Provide it.",
262267
"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.",

src/lib/ioUtil.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from "fs";
22
import os from "os";
33
import path from "path";
44
import uuid from "uuid/v4";
5-
import { logger } from "../logger";
65

76
/**
87
* Creates a random directory in tmp directory.

0 commit comments

Comments
 (0)