Skip to content

Commit

Permalink
Deprecating errors for logger.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Aug 1, 2019
1 parent e8f28b5 commit 0b224e8
Show file tree
Hide file tree
Showing 68 changed files with 727 additions and 342 deletions.
2 changes: 1 addition & 1 deletion packages/abi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
"@ethersproject/bignumber": ">5.0.0-beta.0",
"@ethersproject/bytes": ">5.0.0-beta.0",
"@ethersproject/constants": ">5.0.0-beta.0",
"@ethersproject/errors": ">5.0.0-beta.0",
"@ethersproject/hash": ">5.0.0-beta.0",
"@ethersproject/keccak256": ">5.0.0-beta.0",
"@ethersproject/logger": ">5.0.0-beta.0",
"@ethersproject/properties": ">5.0.0-beta.0",
"@ethersproject/strings": ">5.0.0-beta.0"
},
Expand Down
24 changes: 9 additions & 15 deletions packages/abi/src.ts/abi-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI

import { arrayify, BytesLike } from "@ethersproject/bytes";
import * as errors from "@ethersproject/errors";
import { defineReadOnly } from "@ethersproject/properties";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

import { Coder, Reader, Writer } from "./coders/abstract-coder";
import { AddressCoder } from "./coders/address";
import { ArrayCoder } from "./coders/array";
Expand All @@ -30,7 +33,7 @@ export class AbiCoder {
readonly coerceFunc: CoerceFunc;

constructor(coerceFunc?: CoerceFunc) {
errors.checkNew(new.target, AbiCoder);
logger.checkNew(new.target, AbiCoder);
defineReadOnly(this, "coerceFunc", coerceFunc || null);
}

Expand Down Expand Up @@ -60,10 +63,7 @@ export class AbiCoder {
if (match) {
let size = parseInt(match[2] || "256");
if (size === 0 || size > 256 || (size % 8) !== 0) {
errors.throwError("invalid " + match[1] + " bit length", errors.INVALID_ARGUMENT, {
arg: "param",
value: param
});
logger.throwArgumentError("invalid " + match[1] + " bit length", "param", param);
}
return new NumberCoder(size / 8, (match[1] === "int"), param.name);
}
Expand All @@ -73,18 +73,12 @@ export class AbiCoder {
if (match) {
let size = parseInt(match[1]);
if (size === 0 || size > 32) {
errors.throwError("invalid bytes length", errors.INVALID_ARGUMENT, {
arg: "param",
value: param
});
logger.throwArgumentError("invalid bytes length", "param", param);
}
return new FixedBytesCoder(size, param.name);
}

return errors.throwError("invalid type", errors.INVALID_ARGUMENT, {
arg: "type",
value: param.type
});
return logger.throwError("invalid type", "type", param.type);
}

_getWordSize(): number { return 32; }
Expand All @@ -99,7 +93,7 @@ export class AbiCoder {

encode(types: Array<string | ParamType>, values: Array<any>): string {
if (types.length !== values.length) {
errors.throwError("types/values length mismatch", errors.INVALID_ARGUMENT, {
logger.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
Expand Down
15 changes: 7 additions & 8 deletions packages/abi/src.ts/coders/abstract-coder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import { arrayify, BytesLike, concat, hexlify } from "@ethersproject/bytes";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import * as errors from "@ethersproject/errors";
import { defineReadOnly } from "@ethersproject/properties";

import { Logger } from "@ethersproject/logger";
import { version } from "../_version";
const logger = new Logger(version);

export type CoerceFunc = (type: string, value: any) => any;

export abstract class Coder {
Expand Down Expand Up @@ -34,11 +37,7 @@ export abstract class Coder {
}

_throwError(message: string, value: any): void {
errors.throwError(message, errors.INVALID_ARGUMENT, {
argument: this.localName,
coder: this,
value: value
});
logger.throwArgumentError(message, this.localName, value);
}

abstract encode(writer: Writer, value: any): number;
Expand Down Expand Up @@ -77,7 +76,7 @@ export class Writer {
_getValue(value: BigNumberish): Uint8Array {
let bytes = arrayify(BigNumber.from(value));
if (bytes.length > this.wordSize) {
errors.throwError("value out-of-bounds", errors.BUFFER_OVERRUN, {
logger.throwError("value out-of-bounds", Logger.errors.BUFFER_OVERRUN, {
length: this.wordSize,
offset: bytes.length
});
Expand Down Expand Up @@ -136,7 +135,7 @@ export class Reader {
_peekBytes(offset: number, length: number): Uint8Array {
let alignedLength = Math.ceil(length / this.wordSize) * this.wordSize;
if (this._offset + alignedLength > this._data.length) {
errors.throwError("data out-of-bounds", errors.BUFFER_OVERRUN, {
logger.throwError("data out-of-bounds", Logger.errors.BUFFER_OVERRUN, {
length: this._data.length,
offset: this._offset + alignedLength
});
Expand Down
16 changes: 6 additions & 10 deletions packages/abi/src.ts/coders/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"use strict";

import * as errors from "@ethersproject/errors";
import { Logger } from "@ethersproject/logger";
import { version } from "../_version";
const logger = new Logger(version);

import { Coder, Reader, Writer } from "./abstract-coder";
import { AnonymousCoder } from "./anonymous";
Expand All @@ -18,17 +20,11 @@ export function pack(writer: Writer, coders: Array<Coder>, values: Array<any>):
values = arrayValues;

} else {
errors.throwError("invalid tuple value", errors.INVALID_ARGUMENT, {
coderType: "tuple",
value: values
});
logger.throwArgumentError("invalid tuple value", "tuple", values);
}

if (coders.length !== values.length) {
errors.throwError("types/value length mismatch", errors.INVALID_ARGUMENT, {
coderType: "tuple",
value: values
});
logger.throwArgumentError("types/value length mismatch", "tuple", values);
}

let staticWriter = new Writer(writer.wordSize);
Expand Down Expand Up @@ -136,7 +132,7 @@ export class ArrayCoder extends Coder {
writer.writeValue(value.length);
}

errors.checkArgumentCount(count, value.length, " in coder array" + (this.localName? (" "+ this.localName): ""));
logger.checkArgumentCount(count, value.length, "coder array" + (this.localName? (" "+ this.localName): ""));

let coders = [];
for (let i = 0; i < value.length; i++) { coders.push(this.coder); }
Expand Down
20 changes: 11 additions & 9 deletions packages/abi/src.ts/fragments.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"use strict";

import { BigNumber } from "@ethersproject/bignumber";
import * as errors from "@ethersproject/errors";
import { defineReadOnly } from "@ethersproject/properties";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

export interface JsonFragmentType {
name?: string;
Expand Down Expand Up @@ -58,7 +60,7 @@ function checkModifier(type: string, name: string): boolean {
if (name === "payable") { return true; }
}
if (ModifiersBytes[name] || name === "payable") {
errors.throwArgumentError("invalid modifier", "name", name);
logger.throwArgumentError("invalid modifier", "name", name);
}
return false;
}
Expand Down Expand Up @@ -299,7 +301,7 @@ export class ParamType {
format(format?: string): string {
if (!format) { format = FormatTypes.sighash; }
if (!FormatTypes[format]) {
errors.throwArgumentError("invalid format type", "format", format);
logger.throwArgumentError("invalid format type", "format", format);
}

if (format === FormatTypes.json) {
Expand Down Expand Up @@ -426,7 +428,7 @@ export abstract class Fragment {
return null;
}

return errors.throwArgumentError("invalid fragment object", "value", value);
return logger.throwArgumentError("invalid fragment object", "value", value);
}

static fromString(value: string): Fragment {
Expand Down Expand Up @@ -457,7 +459,7 @@ export class EventFragment extends Fragment {
format(format?: string): string {
if (!format) { format = FormatTypes.sighash; }
if (!FormatTypes[format]) {
errors.throwArgumentError("invalid format type", "format", format);
logger.throwArgumentError("invalid format type", "format", format);
}

if (format === FormatTypes.json) {
Expand Down Expand Up @@ -522,7 +524,7 @@ export class EventFragment extends Fragment {
case "":
break;
default:
errors.warn("unknown modifier: " + modifier);
logger.warn("unknown modifier: " + modifier);
}
});

Expand Down Expand Up @@ -638,7 +640,7 @@ export class ConstructorFragment extends Fragment {
format(format?: string): string {
if (!format) { format = FormatTypes.sighash; }
if (!FormatTypes[format]) {
errors.throwArgumentError("invalid format type", "format", format);
logger.throwArgumentError("invalid format type", "format", format);
}

if (format === FormatTypes.json) {
Expand All @@ -652,7 +654,7 @@ export class ConstructorFragment extends Fragment {
}

if (format === FormatTypes.sighash) {
errors.throwError("cannot format a constructor for sighash", errors.UNSUPPORTED_OPERATION, {
logger.throwError("cannot format a constructor for sighash", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "format(sighash)"
});
}
Expand Down Expand Up @@ -722,7 +724,7 @@ export class FunctionFragment extends ConstructorFragment {
format(format?: string): string {
if (!format) { format = FormatTypes.sighash; }
if (!FormatTypes[format]) {
errors.throwArgumentError("invalid format type", "format", format);
logger.throwArgumentError("invalid format type", "format", format);
}

if (format === FormatTypes.json) {
Expand Down
22 changes: 12 additions & 10 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { arrayify, BytesLike, concat, hexDataSlice, hexlify, hexZeroPad, isHexString } from "@ethersproject/bytes";
import { id } from "@ethersproject/hash";
import { keccak256 } from "@ethersproject/keccak256"
import * as errors from "@ethersproject/errors";
import { defineReadOnly, Description, getStatic } from "@ethersproject/properties";

import { AbiCoder, defaultAbiCoder } from "./abi-coder";
import { ConstructorFragment, EventFragment, Fragment, FunctionFragment, JsonFragment, ParamType } from "./fragments";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

export class LogDescription extends Description {
readonly eventFragment: EventFragment;
Expand Down Expand Up @@ -58,7 +60,7 @@ export class Interface {
static _isInterface: boolean;

constructor(fragments: string | Array<Fragment | JsonFragment | string>) {
errors.checkNew(new.target, Interface);
logger.checkNew(new.target, Interface);

let abi: Array<Fragment | JsonFragment | string> = [ ];
if (typeof(fragments) === "string") {
Expand All @@ -84,7 +86,7 @@ export class Interface {
switch (fragment.type) {
case "constructor":
if (this.deploy) {
errors.warn("duplicate definition - constructor");
logger.warn("duplicate definition - constructor");
return;
}
defineReadOnly(this, "deploy", fragment);
Expand All @@ -101,7 +103,7 @@ export class Interface {

let signature = fragment.format();
if (bucket[signature]) {
errors.warn("duplicate definition - " + signature);
logger.warn("duplicate definition - " + signature);
return;
}

Expand All @@ -114,7 +116,7 @@ export class Interface {
Object.keys(bucket).forEach((signature) => {
let fragment = bucket[signature];
if (count[fragment.name] !== 1) {
errors.warn("duplicate definition - " + fragment.name);
logger.warn("duplicate definition - " + fragment.name);
return;
}
bucket[fragment.name] = fragment;
Expand Down Expand Up @@ -233,7 +235,7 @@ export class Interface {
break;
}

return errors.throwError("call revert exception", errors.CALL_EXCEPTION, {
return logger.throwError("call revert exception", Logger.errors.CALL_EXCEPTION, {
method: functionFragment.format(),
errorSignature: errorSignature,
errorArgs: [ reason ],
Expand All @@ -247,7 +249,7 @@ export class Interface {
}

if (values.length > eventFragment.inputs.length) {
errors.throwError("too many arguments for " + eventFragment.format(), errors.UNEXPECTED_ARGUMENT, {
logger.throwError("too many arguments for " + eventFragment.format(), Logger.errors.UNEXPECTED_ARGUMENT, {
argument: "values",
value: values
})
Expand All @@ -262,7 +264,7 @@ export class Interface {

if (!param.indexed) {
if (value != null) {
errors.throwArgumentError("cannot filter non-indexed parameters; must be null", ("contract." + param.name), value);
logger.throwArgumentError("cannot filter non-indexed parameters; must be null", ("contract." + param.name), value);
}
return;
}
Expand All @@ -274,7 +276,7 @@ export class Interface {
} else if (param.type === "bytes") {
topics.push(keccak256(hexlify(value)));
} else if (param.type.indexOf("[") !== -1 || param.type.substring(0, 5) === "tuple") {
errors.throwArgumentError("filtering with tuples or arrays not supported", ("contract." + param.name), value);
logger.throwArgumentError("filtering with tuples or arrays not supported", ("contract." + param.name), value);
} else {
// Check addresses are valid
if (param.type === "address") { this._abiCoder.encode( [ "address" ], [ value ]); }
Expand All @@ -298,7 +300,7 @@ export class Interface {
if (topics != null && !eventFragment.anonymous) {
let topicHash = this.getEventTopic(eventFragment);
if (!isHexString(topics[0], 32) || topics[0].toLowerCase() !== topicHash) {
errors.throwError("fragment/topic mismatch", errors.INVALID_ARGUMENT, { argument: "topics[0]", expected: topicHash, value: topics[0] });
logger.throwError("fragment/topic mismatch", Logger.errors.INVALID_ARGUMENT, { argument: "topics[0]", expected: topicHash, value: topics[0] });
}
topics = topics.slice(1);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/abstract-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"@ethersproject/bignumber": ">5.0.0-beta.0",
"@ethersproject/bytes": ">5.0.0-beta.0",
"@ethersproject/errors": ">5.0.0-beta.0",
"@ethersproject/logger": ">5.0.0-beta.0",
"@ethersproject/networks": ">5.0.0-beta.0",
"@ethersproject/properties": ">5.0.0-beta.0",
"@ethersproject/transactions": ">5.0.0-beta.0",
Expand Down
12 changes: 7 additions & 5 deletions packages/abstract-provider/src.ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { BytesLike, isHexString } from "@ethersproject/bytes";
import * as errors from "@ethersproject/errors";
import { checkAbstract } from "@ethersproject/errors";
import { Network } from "@ethersproject/networks";
import { Description, defineReadOnly } from "@ethersproject/properties";
import { Transaction } from "@ethersproject/transactions";
import { OnceBlockable } from "@ethersproject/web";

import { Logger } from "@ethersproject/logger";
import { version } from "./_version";
const logger = new Logger(version);

///////////////////////////////
// Exported Types
Expand Down Expand Up @@ -143,7 +145,7 @@ export class BlockForkEvent extends ForkEvent {

constructor(blockhash: string, expiry?: number) {
if (!isHexString(blockhash, 32)) {
errors.throwArgumentError("invalid blockhash", "blockhash", blockhash);
logger.throwArgumentError("invalid blockhash", "blockhash", blockhash);
}

super({
Expand All @@ -160,7 +162,7 @@ export class TransactionForkEvent extends ForkEvent {

constructor(hash: string, expiry?: number) {
if (!isHexString(hash, 32)) {
errors.throwArgumentError("invalid transaction hash", "hash", hash);
logger.throwArgumentError("invalid transaction hash", "hash", hash);
}

super({
Expand All @@ -178,10 +180,10 @@ export class TransactionOrderForkEvent extends ForkEvent {

constructor(beforeHash: string, afterHash: string, expiry?: number) {
if (!isHexString(beforeHash, 32)) {
errors.throwArgumentError("invalid transaction hash", "beforeHash", beforeHash);
logger.throwArgumentError("invalid transaction hash", "beforeHash", beforeHash);
}
if (!isHexString(afterHash, 32)) {
errors.throwArgumentError("invalid transaction hash", "afterHash", afterHash);
logger.throwArgumentError("invalid transaction hash", "afterHash", afterHash);
}

super({
Expand Down
Loading

0 comments on commit 0b224e8

Please sign in to comment.