Skip to content
This repository was archived by the owner on Jul 6, 2022. It is now read-only.

Commit 0b4ba33

Browse files
committed
feat: 🎸 error handle implemented with PolymathError class
add PolymatError class to handle erros and retrieve ErrorCode type
1 parent 1766780 commit 0b4ba33

File tree

3 files changed

+58
-18
lines changed

3 files changed

+58
-18
lines changed

‎src/PolymathError.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ErrorCode } from './types';
2+
3+
export class PolymathError extends Error {
4+
public code: ErrorCode;
5+
6+
// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
7+
constructor({ message, code }: { message?: string; code: ErrorCode }) {
8+
super(message || `Unknown error, code: ${code}`);
9+
this.code = code;
10+
}
11+
}

‎src/types.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,17 @@ export type DividendCheckpointBaseContract = ERC20DividendCheckpointContract | E
335335

336336
export const PERCENTAGE_DECIMALS = new BigNumber(16);
337337
export const FULL_DECIMALS = new BigNumber(18);
338+
339+
export enum ErrorCode {
340+
UserDeniedAccess = 'UserDeniedAccess',
341+
InvalidAddress = 'InvalidAddress',
342+
InsufficientBalance = 'InsufficientBalance',
343+
InvalidSubscriptionToken = 'InvalidSubscriptionToken',
344+
DuplicatedStrings = 'DuplicatedStrings',
345+
TooFar = 'TooFar',
346+
TooEarly = 'TooEarly',
347+
InvalidData = 'InvalidData',
348+
MismatchedArrayLength = 'MismatchedArrayLength',
349+
InvalidVersion = 'InvalidVersion',
350+
InvalidPartition = 'InvalidPartition',
351+
}

‎src/utils/assert.ts‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { assert as sharedAssert } from '@0x/assert';
22
import { BigNumber } from '@polymathnetwork/abi-wrappers';
3-
import {Partition} from '../types';
3+
import { Partition, ErrorCode } from '../types';
4+
import { PolymathError } from '../PolymathError';
45

56
const ZERO = '0x0000000000000000000000000000000000000000';
67
const MAX_64_BYTES_DATE = new Date(18446744073709);
@@ -9,61 +10,75 @@ const BIG_NUMBER_ZERO = new BigNumber(0);
910

1011
const assert = {
1112
...sharedAssert,
13+
assert(condition: boolean, code: ErrorCode, message?: string): void {
14+
if (!condition) {
15+
throw new PolymathError({ message, code });
16+
}
17+
},
1218
isValidSubscriptionToken(variableName: string, subscriptionToken: string): void {
1319
const uuidRegex = new RegExp('^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$');
1420
const isValid = uuidRegex.test(subscriptionToken);
15-
sharedAssert.assert(isValid, `Expected ${variableName} to be a valid subscription token`);
21+
this.assert(
22+
isValid,
23+
ErrorCode.InvalidSubscriptionToken,
24+
`Expected ${variableName} to be a valid subscription token`,
25+
);
1626
},
1727
isNonZeroETHAddressHex(variableName: string, address: string): void {
1828
sharedAssert.isETHAddressHex(variableName, address);
19-
sharedAssert.assert(address !== ZERO, `'${variableName}' is not expected to be 0x0`);
29+
this.assert(address !== ZERO, ErrorCode.InvalidAddress, `'${variableName}' is not expected to be 0x0`);
2030
},
2131
areThereDuplicatedStrings(variableName: string, addresses: string[]): void {
2232
const result = addresses.length === new Set(addresses).size;
23-
sharedAssert.assert(result, `There are duplicates in ${variableName} array.`);
33+
this.assert(result, ErrorCode.DuplicatedStrings, `There are duplicates in ${variableName} array.`);
2434
},
2535
isLessThanMax64BytesDate(variableName: string, value: Date): void {
26-
sharedAssert.assert(value <= MAX_64_BYTES_DATE, `${variableName} date is too far in the future`);
36+
this.assert(value <= MAX_64_BYTES_DATE, ErrorCode.TooFar, `${variableName} date is too far in the future`);
2737
},
2838
isPercentage(variableName: string, value: BigNumber): void {
29-
sharedAssert.assert(
39+
this.assert(
3040
value.isLessThanOrEqualTo(MAX_PERCENTAGE),
41+
ErrorCode.InvalidData,
3142
`${variableName} is not expected to be greater than 100%`,
3243
);
3344
},
3445
isFutureDate(value: Date, message: string): void {
35-
sharedAssert.assert(value >= new Date(), message);
46+
this.assert(value >= new Date(), ErrorCode.TooEarly, message);
3647
},
3748
isPastDate(value: Date, message: string): void {
38-
sharedAssert.assert(value <= new Date(), message);
49+
this.assert(value <= new Date(), ErrorCode.TooFar, message);
3950
},
4051
isNotDateZero(value: Date, message: string): void {
41-
sharedAssert.assert(value !== new Date(0), message);
52+
this.assert(value !== new Date(0), ErrorCode.InvalidData, message);
4253
},
4354
isBigNumberZero(value: BigNumber, message: string): void {
44-
sharedAssert.assert(value.isEqualTo(BIG_NUMBER_ZERO), message);
55+
this.assert(value.isEqualTo(BIG_NUMBER_ZERO), ErrorCode.InvalidData, message);
4556
},
4657
isBigNumberGreaterThanZero(value: BigNumber, message: string): void {
47-
sharedAssert.assert(value >= BIG_NUMBER_ZERO, message);
48-
},
49-
areValidArrayLengths(value: any[][], message: string) { // eslint-disable-line
50-
sharedAssert.assert(
51-
value.every((x: any[]) => { // eslint-disable-line
58+
this.assert(value >= BIG_NUMBER_ZERO, ErrorCode.InvalidData, message);
59+
},
60+
areValidArrayLengths(value: any[][], message: string) {
61+
// eslint-disable-line
62+
this.assert(
63+
value.every((x: any[]) => {
64+
// eslint-disable-line
5265
return x.length === value[0].length;
5366
}),
67+
ErrorCode.MismatchedArrayLength,
5468
message,
5569
);
5670
},
5771
// eslint-enable no-any
5872
isValidVersion(version: string) {
59-
sharedAssert.assert(
73+
this.assert(
6074
/^(\d+\.)(\d+\.)(\d+)$/.test(version),
75+
ErrorCode.InvalidVersion,
6176
'Invalid package version. Right format: major.minor.patch',
6277
);
6378
},
6479
isValidPartition(partition: Partition) {
65-
sharedAssert.assert(partition === Partition.Unlocked, 'Invalid Partition');
66-
}
80+
this.assert(partition === Partition.Unlocked, ErrorCode.InvalidPartition, 'Invalid Partition');
81+
},
6782
};
6883

6984
export default assert;

0 commit comments

Comments
 (0)