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

Commit b79cc61

Browse files
committed
fix: 🐛 improve throw error with PolymathError class
1 parent 29b825b commit b79cc61

File tree

7 files changed

+33
-28
lines changed

7 files changed

+33
-28
lines changed

src/PolymathAPI.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import ERC20 from './contract_wrappers/tokens/erc20_wrapper';
3939
import assert from './utils/assert';
4040
import PolyTokenFaucetWrapper from './contract_wrappers/tokens/poly_token_faucet_wrapper';
4141
import ContractFactory from './factories/contractFactory';
42+
import { ErrorCode } from './types';
43+
import { PolymathError } from './PolymathError';
4244

4345
/**
4446
* @param provider The web3 provider
@@ -213,7 +215,7 @@ export class PolymathAPI {
213215
public getPolyTokens = async (params: GetTokensParams): Promise<PolyResponse> => {
214216
const networkId = await this.web3Wrapper.getNetworkIdAsync();
215217
if (networkId === 1) {
216-
throw new Error('Only for testnet');
218+
throw new PolymathError({ message: 'Only for testnet', code: ErrorCode.PreconditionRequired });
217219
}
218220
const address = params.address !== undefined ? params.address : await this.getAccount();
219221
assert.isETHAddressHex('address', address);

src/contract_wrappers/contract_wrapper.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import {
2626
EventCallback,
2727
GetLogs,
2828
Subscribe,
29+
ErrorCode,
2930
} from '../types';
31+
import { PolymathError } from '../PolymathError';
3032
import assert from '../utils/assert';
3133

32-
const SUBSCRIPTION_NOT_FOUND = 'SUBSCRIPTION_NOT_FOUND';
33-
const SUBSCRIPTION_ALREADY_PRESENT = 'SUBSCRIPTION_ALREADY_PRESENT';
34-
3534
export default abstract class ContractWrapper {
3635
protected contract: Promise<BaseContract>;
3736

@@ -106,7 +105,7 @@ export default abstract class ContractWrapper {
106105

107106
protected unsubscribeInternal(filterToken: string, err?: Error): void {
108107
if (_.isUndefined(this._filters[filterToken])) {
109-
throw new Error(SUBSCRIPTION_NOT_FOUND);
108+
throw new PolymathError({ code: ErrorCode.NotFound });
110109
}
111110
if (!_.isUndefined(err)) {
112111
const callback = this._filterCallbacks[filterToken];
@@ -181,7 +180,7 @@ export default abstract class ContractWrapper {
181180

182181
private _startBlockAndLogStream(isVerbose: boolean): void {
183182
if (!_.isUndefined(this._blockAndLogStreamerIfExists)) {
184-
throw new Error(SUBSCRIPTION_ALREADY_PRESENT);
183+
throw new PolymathError({ code: ErrorCode.AlreadyExists });
185184
}
186185
this._blockAndLogStreamerIfExists = new BlockAndLogStreamer(
187186
this._blockstreamGetBlockOrNullAsync.bind(this),
@@ -227,7 +226,7 @@ export default abstract class ContractWrapper {
227226

228227
private _stopBlockAndLogStream(): void {
229228
if (_.isUndefined(this._blockAndLogStreamerIfExists)) {
230-
throw new Error(SUBSCRIPTION_NOT_FOUND);
229+
throw new PolymathError({ code: ErrorCode.NotFound });
231230
}
232231
this._blockAndLogStreamerIfExists.unsubscribeFromOnLogAdded(this._onLogAddedSubscriptionToken as string);
233232
this._blockAndLogStreamerIfExists.unsubscribeFromOnLogRemoved(this._onLogRemovedSubscriptionToken as string);

src/factories/moduleWrapperFactory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import ModuleFactoryWrapper from '../contract_wrappers/modules/module_factory_wr
1515
import VestingEscrowWalletWrapper from '../contract_wrappers/modules/wallet/vesting_escrow_wallet_wrapper';
1616
import ContractFactory from './contractFactory';
1717
import assert from '../utils/assert';
18-
import { ModuleName } from '../types';
18+
import { ModuleName, ErrorCode } from '../types';
19+
import { PolymathError } from '../PolymathError';
1920

2021
interface GetModuleParams {
2122
address: string;
@@ -198,7 +199,7 @@ export default class ModuleWrapperFactory {
198199
// Burn
199200
default:
200201
// TODO: Typed error here
201-
throw new Error();
202+
throw new PolymathError({ code: ErrorCode.NotFound });
202203
}
203204
};
204205
}

src/factories/tokenWrapperFactory.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import ERC20TokenWrapper from '../contract_wrappers/tokens/erc20_wrapper';
55
import ERC20DetailedTokenWrapper from '../contract_wrappers/tokens/erc20_detailed_wrapper';
66
import ContractFactory from './contractFactory';
77
import assert from '../utils/assert';
8+
import { ErrorCode } from '../types';
9+
import { PolymathError } from '../PolymathError';
810

911
/**
1012
* The SecurityTokenFactory class is a factory to generate new SecurityTokenWrappers.
@@ -48,9 +50,7 @@ export default class TokenWrapperFactory {
4850
if (await token.isValidContract()) {
4951
return token;
5052
}
51-
52-
// TODO: Replace this for a typed Error
53-
throw new Error();
53+
throw new PolymathError({ code: ErrorCode.NotFound });
5454
};
5555

5656
/**
@@ -67,8 +67,7 @@ export default class TokenWrapperFactory {
6767
this.contractFactory,
6868
);
6969
}
70-
// TODO: Replace this for a typed Error
71-
throw new Error();
70+
throw new PolymathError({ code: ErrorCode.NotFound });
7271
};
7372

7473
/**

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,5 @@ export enum ErrorCode {
366366
NotFound = 'NotFound',
367367
MismatchedLength = 'MismatchedLength',
368368
TickerExpired = 'TickerExpired',
369+
UnknownNetwork = 'UnknownNetwork',
369370
}

src/utils/addresses.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import _ from 'lodash';
2-
import { NetworkId } from '../types';
2+
import { NetworkId, ErrorCode } from '../types';
3+
import { PolymathError } from '../PolymathError';
34

45
const networkToAddresses: { [networkId: number]: string } = {
56
1: '0xdfabf3e4793cd30affb47ab6fa4cf4eef26bbc27',
@@ -9,10 +10,10 @@ const networkToAddresses: { [networkId: number]: string } = {
910

1011
function getContractAddressesForNetworkOrThrow(networkId: NetworkId): string {
1112
if (_.isUndefined(networkToAddresses[networkId])) {
12-
throw new Error(
13-
`Unknown network id (${networkId}).
14-
No known Polymath contracts have been deployed on this network.`,
15-
);
13+
throw new PolymathError({
14+
message: `Unknown network id (${networkId}). No known Polymath contracts have been deployed on this network.`,
15+
code: ErrorCode.UnknownNetwork,
16+
});
1617
}
1718
return networkToAddresses[networkId];
1819
}
@@ -23,11 +24,12 @@ function getContractAddressesForNetworkOrThrow(networkId: NetworkId): string {
2324
*/
2425
export default function getDefaultContractAddresses(networkId: NetworkId): string {
2526
if (!(networkId in NetworkId)) {
26-
throw new Error(
27-
`No default contract addresses found for the given network id (${networkId}).
28-
If you want to use ContractWrappers on this network,
29-
you must manually pass in the contract address(es) to the constructor.`,
30-
);
27+
throw new PolymathError({
28+
message: `No default contract addresses found for the given network id (${networkId}).
29+
If you want to use ContractWrappers on this network,
30+
you must manually pass in the contract address(es) to the constructor.`,
31+
code: ErrorCode.NotFound,
32+
});
3133
}
3234
return getContractAddressesForNetworkOrThrow(networkId);
3335
}

src/utils/convert.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ethers, BigNumber } from '@polymathnetwork/abi-wrappers';
2-
import { ModuleType, Partition, Perm, TransferResult } from '../types';
2+
import { ModuleType, Partition, Perm, TransferResult, ErrorCode } from '../types';
3+
import { PolymathError } from '../PolymathError';
34

45
const BASE = new BigNumber(10);
56

@@ -88,7 +89,7 @@ export function parsePartitionBytes32Value(value: string): Partition {
8889
case '0':
8990
return Partition.Undefined;
9091
default:
91-
throw new Error('Partition not recognized');
92+
throw new PolymathError({ message: 'Partition not recognized', code: ErrorCode.NotFound });
9293
}
9394
}
9495
export function parsePermBytes32Value(value: string): Perm {
@@ -98,7 +99,7 @@ export function parsePermBytes32Value(value: string): Perm {
9899
case Perm.Operator:
99100
return Perm.Operator;
100101
default:
101-
throw new Error('Permission not recognized');
102+
throw new PolymathError({ message: 'Partition not recognized', code: ErrorCode.NotFound });
102103
}
103104
}
104105
export function parseModuleTypeValue(value: BigNumber): ModuleType {
@@ -114,7 +115,7 @@ export function parseModuleTypeValue(value: BigNumber): ModuleType {
114115
case ModuleType.Burn:
115116
return ModuleType.Burn;
116117
default:
117-
throw new Error('Module Type not recognized');
118+
throw new PolymathError({ message: 'Module Type not recognized', code: ErrorCode.NotFound });
118119
}
119120
}
120121
export function parseTransferResult(value: BigNumber): TransferResult {

0 commit comments

Comments
 (0)