From 14e6811bf7d7c38a3b5714dededcc883c185d814 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Sat, 25 Apr 2020 01:06:28 -0400 Subject: [PATCH] More draconian Typing. --- packages/abi/src.ts/abi-coder.ts | 2 +- packages/json-wallets/src.ts/crowdsale.ts | 2 +- packages/json-wallets/src.ts/keystore.ts | 2 +- packages/logger/src.ts/index.ts | 165 +++++++++++----------- packages/properties/src.ts/index.ts | 2 +- 5 files changed, 88 insertions(+), 85 deletions(-) diff --git a/packages/abi/src.ts/abi-coder.ts b/packages/abi/src.ts/abi-coder.ts index 376f739bab..0c29405dbf 100644 --- a/packages/abi/src.ts/abi-coder.ts +++ b/packages/abi/src.ts/abi-coder.ts @@ -78,7 +78,7 @@ export class AbiCoder { return new FixedBytesCoder(size, param.name); } - return logger.throwError("invalid type", "type", param.type); + return logger.throwArgumentError("invalid type", "type", param.type); } _getWordSize(): number { return 32; } diff --git a/packages/json-wallets/src.ts/crowdsale.ts b/packages/json-wallets/src.ts/crowdsale.ts index b6833a373f..0b4e6f2198 100644 --- a/packages/json-wallets/src.ts/crowdsale.ts +++ b/packages/json-wallets/src.ts/crowdsale.ts @@ -16,7 +16,7 @@ const logger = new Logger(version); import { getPassword, looseArrayify, searchPath } from "./utils"; -interface _CrowdsaleAccount { +export interface _CrowdsaleAccount { address: string; privateKey: string; diff --git a/packages/json-wallets/src.ts/keystore.ts b/packages/json-wallets/src.ts/keystore.ts index 940ce6b6a8..716e70e42b 100644 --- a/packages/json-wallets/src.ts/keystore.ts +++ b/packages/json-wallets/src.ts/keystore.ts @@ -26,7 +26,7 @@ function hasMnemonic(value: any): value is { mnemonic: Mnemonic } { return (value != null && value.mnemonic && value.mnemonic.phrase); } -interface _KeystoreAccount { +export interface _KeystoreAccount { address: string; privateKey: string; mnemonic?: Mnemonic; diff --git a/packages/logger/src.ts/index.ts b/packages/logger/src.ts/index.ts index 1902143008..d174494ade 100644 --- a/packages/logger/src.ts/index.ts +++ b/packages/logger/src.ts/index.ts @@ -4,14 +4,12 @@ let _permanentCensorErrors = false; let _censorErrors = false; const LogLevels: { [ name: string ]: number } = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 }; -let LogLevel = LogLevels["default"]; +let _logLevel = LogLevels["default"]; import { version } from "./_version"; let _globalLogger: Logger = null; -export type LogLevel = "DEBUG" | "INFO" | "WARNING" | "ERROR" | "OFF"; - function _checkNormalize(): string { try { const missing: Array = [ ]; @@ -43,105 +41,110 @@ function _checkNormalize(): string { const _normalizeError = _checkNormalize(); -export class Logger { - readonly version: string; +export enum LogLevel { + DEBUG = "DEBUG", + INFO = "INFO", + WARNING = "WARNING", + ERROR = "ERROR", + OFF = "OFF" +} + - static errors = { +export enum ErrorCode { - /////////////////// - // Generic Errors + /////////////////// + // Generic Errors - // Unknown Error - UNKNOWN_ERROR: "UNKNOWN_ERROR", + // Unknown Error + UNKNOWN_ERROR = "UNKNOWN_ERROR", - // Not Implemented - NOT_IMPLEMENTED: "NOT_IMPLEMENTED", + // Not Implemented + NOT_IMPLEMENTED = "NOT_IMPLEMENTED", - // Unsupported Operation - // - operation - UNSUPPORTED_OPERATION: "UNSUPPORTED_OPERATION", + // Unsupported Operation + // - operation + UNSUPPORTED_OPERATION = "UNSUPPORTED_OPERATION", - // Network Error (i.e. Ethereum Network, such as an invalid chain ID) - NETWORK_ERROR: "NETWORK_ERROR", + // Network Error (i.e. Ethereum Network, such as an invalid chain ID) + NETWORK_ERROR = "NETWORK_ERROR", - // Some sort of bad response from the server - SERVER_ERROR: "SERVER_ERROR", + // Some sort of bad response from the server + SERVER_ERROR = "SERVER_ERROR", - // Timeout - TIMEOUT: "TIMEOUT", + // Timeout + TIMEOUT = "TIMEOUT", - /////////////////// - // Operational Errors + /////////////////// + // Operational Errors - // Buffer Overrun - BUFFER_OVERRUN: "BUFFER_OVERRUN", + // Buffer Overrun + BUFFER_OVERRUN = "BUFFER_OVERRUN", - // Numeric Fault - // - operation: the operation being executed - // - fault: the reason this faulted - NUMERIC_FAULT: "NUMERIC_FAULT", + // Numeric Fault + // - operation: the operation being executed + // - fault: the reason this faulted + NUMERIC_FAULT = "NUMERIC_FAULT", - /////////////////// - // Argument Errors + /////////////////// + // Argument Errors - // Missing new operator to an object - // - name: The name of the class - MISSING_NEW: "MISSING_NEW", + // Missing new operator to an object + // - name: The name of the class + MISSING_NEW = "MISSING_NEW", - // Invalid argument (e.g. value is incompatible with type) to a function: - // - argument: The argument name that was invalid - // - value: The value of the argument - INVALID_ARGUMENT: "INVALID_ARGUMENT", + // Invalid argument (e.g. value is incompatible with type) to a function: + // - argument: The argument name that was invalid + // - value: The value of the argument + INVALID_ARGUMENT = "INVALID_ARGUMENT", - // Missing argument to a function: - // - count: The number of arguments received - // - expectedCount: The number of arguments expected - MISSING_ARGUMENT: "MISSING_ARGUMENT", + // Missing argument to a function: + // - count: The number of arguments received + // - expectedCount: The number of arguments expected + MISSING_ARGUMENT = "MISSING_ARGUMENT", - // Too many arguments - // - count: The number of arguments received - // - expectedCount: The number of arguments expected - UNEXPECTED_ARGUMENT: "UNEXPECTED_ARGUMENT", + // Too many arguments + // - count: The number of arguments received + // - expectedCount: The number of arguments expected + UNEXPECTED_ARGUMENT = "UNEXPECTED_ARGUMENT", - /////////////////// - // Blockchain Errors + /////////////////// + // Blockchain Errors - // Call exception - // - transaction: the transaction - // - address?: the contract address - // - args?: The arguments passed into the function - // - method?: The Solidity method signature - // - errorSignature?: The EIP848 error signature - // - errorArgs?: The EIP848 error parameters - // - reason: The reason (only for EIP848 "Error(string)") - CALL_EXCEPTION: "CALL_EXCEPTION", + // Call exception + // - transaction: the transaction + // - address?: the contract address + // - args?: The arguments passed into the function + // - method?: The Solidity method signature + // - errorSignature?: The EIP848 error signature + // - errorArgs?: The EIP848 error parameters + // - reason: The reason (only for EIP848 "Error(string)") + CALL_EXCEPTION = "CALL_EXCEPTION", - // Insufficien funds (< value + gasLimit * gasPrice) - // - transaction: the transaction attempted - INSUFFICIENT_FUNDS: "INSUFFICIENT_FUNDS", + // Insufficien funds (< value + gasLimit * gasPrice) + // - transaction: the transaction attempted + INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS", - // Nonce has already been used - // - transaction: the transaction attempted - NONCE_EXPIRED: "NONCE_EXPIRED", + // Nonce has already been used + // - transaction: the transaction attempted + NONCE_EXPIRED = "NONCE_EXPIRED", - // The replacement fee for the transaction is too low - // - transaction: the transaction attempted - REPLACEMENT_UNDERPRICED: "REPLACEMENT_UNDERPRICED", + // The replacement fee for the transaction is too low + // - transaction: the transaction attempted + REPLACEMENT_UNDERPRICED = "REPLACEMENT_UNDERPRICED", + + // The gas limit could not be estimated + // - transaction: the transaction passed to estimateGas + UNPREDICTABLE_GAS_LIMIT = "UNPREDICTABLE_GAS_LIMIT", +}; + +export class Logger { + readonly version: string; - // The gas limit could not be estimated - // - transaction: the transaction passed to estimateGas - UNPREDICTABLE_GAS_LIMIT: "UNPREDICTABLE_GAS_LIMIT", - }; + static errors = ErrorCode; - static levels: { [ name: string ]: LogLevel } = { - DEBUG: "DEBUG", - INFO: "INFO", - WARNING: "WARNING", - ERROR: "ERROR", - OFF: "OFF" - }; + static levels = LogLevel; constructor(version: string) { Object.defineProperty(this, "version", { @@ -156,7 +159,7 @@ export class Logger { if (LogLevels[level] == null) { this.throwArgumentError("invalid log level name", "logLevel", logLevel); } - if (LogLevel > LogLevels[level]) { return; } + if (_logLevel > LogLevels[level]) { return; } console.log.apply(console, args); } @@ -172,7 +175,7 @@ export class Logger { this._log(Logger.levels.WARNING, args); } - makeError(message: string, code?: string, params?: any): Error { + makeError(message: string, code?: ErrorCode, params?: any): Error { // Errors are being censored if (_censorErrors) { return this.makeError("censored error", code, { }); @@ -209,7 +212,7 @@ export class Logger { return error; } - throwError(message: string, code?: string, params?: any): never { + throwError(message: string, code?: ErrorCode, params?: any): never { throw this.makeError(message, code, params); } @@ -320,6 +323,6 @@ export class Logger { Logger.globalLogger().warn("invalid log level - " + logLevel); return; } - LogLevel = level; + _logLevel = level; } } diff --git a/packages/properties/src.ts/index.ts b/packages/properties/src.ts/index.ts index 46f08f8634..7dd657d1f9 100644 --- a/packages/properties/src.ts/index.ts +++ b/packages/properties/src.ts/index.ts @@ -123,7 +123,7 @@ export function deepCopy(object: T): Similar { } export class Description { - constructor(info: T) { + constructor(info: { [ K in keyof T ]: T[K] }) { for (const key in info) { (this)[key] = deepCopy(info[key]); }