Skip to content

Commit 2ac6dff

Browse files
committed
Throw an error when unreachable code is reached
1 parent f97d9eb commit 2ac6dff

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

src/command/create.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { H256 } from "codechain-sdk/lib/core/classes";
33
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
44
import * as _ from "lodash";
55

6+
import { CLIError, CLIErrorType } from "../error";
67
import { AccountType } from "../types";
78
import { getAddressFromPublic } from "../util";
89

@@ -14,13 +15,17 @@ export async function createKey(
1415
const publicKey = await cckey[accountType].createKey({
1516
passphrase
1617
});
17-
if (accountType === "platform") {
18-
const accountId = getAccountIdFromPublic(publicKey);
19-
cckey.mapping.add({ key: accountId, value: publicKey });
20-
}
21-
if (accountType === "asset") {
22-
const hash = H256.ensure(blake256(publicKey)).value;
23-
cckey.mapping.add({ key: hash, value: publicKey });
18+
switch (accountType) {
19+
case "platform":
20+
const accountId = getAccountIdFromPublic(publicKey);
21+
cckey.mapping.add({ key: accountId, value: publicKey });
22+
break;
23+
case "asset":
24+
const hash = H256.ensure(blake256(publicKey)).value;
25+
cckey.mapping.add({ key: hash, value: publicKey });
26+
break;
27+
default:
28+
throw new CLIError(CLIErrorType.Unreachable);
2429
}
2530

2631
console.log(getAddressFromPublic(accountType, publicKey));

src/command/import.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { H256 } from "codechain-sdk/lib/core/classes";
44
import { blake256, getAccountIdFromPublic } from "codechain-sdk/lib/utils";
55
import * as _ from "lodash";
66

7+
import { CLIError, CLIErrorType } from "../error";
78
import { AccountType } from "../types";
89
import { getAddressFromPublic } from "../util";
910

@@ -17,13 +18,17 @@ export async function importKey(
1718
secret,
1819
passphrase
1920
});
20-
if (accountType === "platform") {
21-
const accountId = getAccountIdFromPublic(publicKey);
22-
cckey.mapping.add({ key: accountId, value: publicKey });
23-
}
24-
if (accountType === "asset") {
25-
const hash = H256.ensure(blake256(publicKey)).value;
26-
cckey.mapping.add({ key: hash, value: publicKey });
21+
switch (accountType) {
22+
case "platform":
23+
const accountId = getAccountIdFromPublic(publicKey);
24+
cckey.mapping.add({ key: accountId, value: publicKey });
25+
break;
26+
case "asset":
27+
const hash = H256.ensure(blake256(publicKey)).value;
28+
cckey.mapping.add({ key: hash, value: publicKey });
29+
break;
30+
default:
31+
throw new CLIError(CLIErrorType.Unreachable);
2732
}
2833

2934
console.log(getAddressFromPublic(accountType, publicKey));

src/error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export enum CLIErrorType {
55
InvalidAction,
66
OptionRequired,
77
NoSuchAddress,
8+
Unreachable,
89
Unknown
910
}
1011

@@ -26,6 +27,8 @@ function getErrorMessage(type: CLIErrorType, args: any = {}) {
2627
return `Option --${args.optionName} is required`;
2728
case CLIErrorType.NoSuchAddress:
2829
return `${args.address} is not found`;
30+
case CLIErrorType.Unreachable:
31+
return "Unreachable code reached";
2932
case CLIErrorType.Unknown:
3033
return `Internal error ${args.message}`;
3134
default:

0 commit comments

Comments
 (0)