2019-08-01 18:04:06 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
let _permanentCensorErrors = false;
|
|
|
|
let _censorErrors = false;
|
|
|
|
|
2020-03-17 13:57:53 -04:00
|
|
|
const LogLevels: { [ name: string ]: number } = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
|
2020-04-25 01:06:28 -04:00
|
|
|
let _logLevel = LogLevels["default"];
|
2019-08-01 18:04:06 -04:00
|
|
|
|
|
|
|
import { version } from "./_version";
|
|
|
|
|
|
|
|
let _globalLogger: Logger = null;
|
|
|
|
|
|
|
|
function _checkNormalize(): string {
|
|
|
|
try {
|
2019-11-01 23:33:51 +09:00
|
|
|
const missing: Array<string> = [ ];
|
2019-08-01 18:04:06 -04:00
|
|
|
|
|
|
|
// Make sure all forms of normalization are supported
|
|
|
|
["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => {
|
|
|
|
try {
|
|
|
|
if ("test".normalize(form) !== "test") {
|
|
|
|
throw new Error("bad normalize");
|
|
|
|
};
|
|
|
|
} catch(error) {
|
|
|
|
missing.push(form);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (missing.length) {
|
|
|
|
throw new Error("missing " + missing.join(", "));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) {
|
|
|
|
throw new Error("broken implementation")
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return error.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-01 23:33:51 +09:00
|
|
|
const _normalizeError = _checkNormalize();
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
export enum LogLevel {
|
|
|
|
DEBUG = "DEBUG",
|
|
|
|
INFO = "INFO",
|
|
|
|
WARNING = "WARNING",
|
|
|
|
ERROR = "ERROR",
|
|
|
|
OFF = "OFF"
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
export enum ErrorCode {
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
///////////////////
|
|
|
|
// Generic Errors
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Unknown Error
|
|
|
|
UNKNOWN_ERROR = "UNKNOWN_ERROR",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Not Implemented
|
|
|
|
NOT_IMPLEMENTED = "NOT_IMPLEMENTED",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Unsupported Operation
|
|
|
|
// - operation
|
|
|
|
UNSUPPORTED_OPERATION = "UNSUPPORTED_OPERATION",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Network Error (i.e. Ethereum Network, such as an invalid chain ID)
|
2020-06-13 21:26:50 -04:00
|
|
|
// - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown)
|
2020-04-25 01:06:28 -04:00
|
|
|
NETWORK_ERROR = "NETWORK_ERROR",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Some sort of bad response from the server
|
|
|
|
SERVER_ERROR = "SERVER_ERROR",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Timeout
|
|
|
|
TIMEOUT = "TIMEOUT",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
///////////////////
|
|
|
|
// Operational Errors
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Buffer Overrun
|
|
|
|
BUFFER_OVERRUN = "BUFFER_OVERRUN",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Numeric Fault
|
|
|
|
// - operation: the operation being executed
|
|
|
|
// - fault: the reason this faulted
|
|
|
|
NUMERIC_FAULT = "NUMERIC_FAULT",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
///////////////////
|
|
|
|
// Argument Errors
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Missing new operator to an object
|
|
|
|
// - name: The name of the class
|
|
|
|
MISSING_NEW = "MISSING_NEW",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// 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",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Missing argument to a function:
|
|
|
|
// - count: The number of arguments received
|
|
|
|
// - expectedCount: The number of arguments expected
|
|
|
|
MISSING_ARGUMENT = "MISSING_ARGUMENT",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Too many arguments
|
|
|
|
// - count: The number of arguments received
|
|
|
|
// - expectedCount: The number of arguments expected
|
|
|
|
UNEXPECTED_ARGUMENT = "UNEXPECTED_ARGUMENT",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
///////////////////
|
|
|
|
// Blockchain Errors
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// 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",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Insufficien funds (< value + gasLimit * gasPrice)
|
|
|
|
// - transaction: the transaction attempted
|
|
|
|
INSUFFICIENT_FUNDS = "INSUFFICIENT_FUNDS",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// Nonce has already been used
|
|
|
|
// - transaction: the transaction attempted
|
|
|
|
NONCE_EXPIRED = "NONCE_EXPIRED",
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
// 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",
|
2021-04-26 12:06:37 -04:00
|
|
|
|
|
|
|
// The transaction was replaced by one with a higher gas price
|
|
|
|
// - reason: "cancelled", "replaced" or "repriced"
|
|
|
|
// - cancelled: true if reason == "cancelled" or reason == "replaced")
|
|
|
|
// - hash: original transaction hash
|
|
|
|
// - replacement: the full TransactionsResponse for the replacement
|
2021-05-13 23:28:47 -04:00
|
|
|
// - receipt: the receipt of the replacement
|
2021-04-26 12:06:37 -04:00
|
|
|
TRANSACTION_REPLACED = "TRANSACTION_REPLACED",
|
2020-04-25 01:06:28 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export class Logger {
|
|
|
|
readonly version: string;
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
static errors = ErrorCode;
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
static levels = LogLevel;
|
2019-08-01 18:04:06 -04:00
|
|
|
|
|
|
|
constructor(version: string) {
|
|
|
|
Object.defineProperty(this, "version", {
|
|
|
|
enumerable: true,
|
|
|
|
value: version,
|
|
|
|
writable: false
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_log(logLevel: LogLevel, args: Array<any>): void {
|
2020-03-17 13:57:53 -04:00
|
|
|
const level = logLevel.toLowerCase();
|
|
|
|
if (LogLevels[level] == null) {
|
|
|
|
this.throwArgumentError("invalid log level name", "logLevel", logLevel);
|
|
|
|
}
|
2020-04-25 01:06:28 -04:00
|
|
|
if (_logLevel > LogLevels[level]) { return; }
|
2019-08-01 18:04:06 -04:00
|
|
|
console.log.apply(console, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(...args: Array<any>): void {
|
|
|
|
this._log(Logger.levels.DEBUG, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
info(...args: Array<any>): void {
|
|
|
|
this._log(Logger.levels.INFO, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(...args: Array<any>): void {
|
|
|
|
this._log(Logger.levels.WARNING, args);
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
makeError(message: string, code?: ErrorCode, params?: any): Error {
|
2020-02-17 20:22:33 -05:00
|
|
|
// Errors are being censored
|
2019-08-01 18:04:06 -04:00
|
|
|
if (_censorErrors) {
|
2020-02-17 20:22:33 -05:00
|
|
|
return this.makeError("censored error", code, { });
|
2019-08-01 18:04:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!code) { code = Logger.errors.UNKNOWN_ERROR; }
|
|
|
|
if (!params) { params = {}; }
|
|
|
|
|
2019-11-01 23:33:51 +09:00
|
|
|
const messageDetails: Array<string> = [];
|
2019-08-01 18:04:06 -04:00
|
|
|
Object.keys(params).forEach((key) => {
|
|
|
|
try {
|
|
|
|
messageDetails.push(key + "=" + JSON.stringify(params[key]));
|
|
|
|
} catch (error) {
|
|
|
|
messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
|
|
|
|
}
|
|
|
|
});
|
2020-02-03 23:05:04 -05:00
|
|
|
messageDetails.push(`code=${ code }`);
|
|
|
|
messageDetails.push(`version=${ this.version }`);
|
2019-08-01 18:04:06 -04:00
|
|
|
|
2019-11-01 23:33:51 +09:00
|
|
|
const reason = message;
|
2019-08-01 18:04:06 -04:00
|
|
|
if (messageDetails.length) {
|
|
|
|
message += " (" + messageDetails.join(", ") + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: Any??
|
2019-11-01 23:33:51 +09:00
|
|
|
const error: any = new Error(message);
|
2019-08-01 18:04:06 -04:00
|
|
|
error.reason = reason;
|
|
|
|
error.code = code
|
|
|
|
|
|
|
|
Object.keys(params).forEach(function(key) {
|
|
|
|
error[key] = params[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2020-04-25 01:06:28 -04:00
|
|
|
throwError(message: string, code?: ErrorCode, params?: any): never {
|
2019-08-01 18:04:06 -04:00
|
|
|
throw this.makeError(message, code, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
throwArgumentError(message: string, name: string, value: any): never {
|
|
|
|
return this.throwError(message, Logger.errors.INVALID_ARGUMENT, {
|
|
|
|
argument: name,
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-13 07:24:57 -04:00
|
|
|
assert(condition: any, message: string, code?: ErrorCode, params?: any): void {
|
|
|
|
if (!!condition) { return; }
|
|
|
|
this.throwError(message, code, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
assertArgument(condition: any, message: string, name: string, value: any): void {
|
|
|
|
if (!!condition) { return; }
|
|
|
|
this.throwArgumentError(message, name, value);
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
checkNormalize(message?: string): void {
|
|
|
|
if (message == null) { message = "platform missing String.prototype.normalize"; }
|
|
|
|
if (_normalizeError) {
|
|
|
|
this.throwError("platform missing String.prototype.normalize", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "String.prototype.normalize", form: _normalizeError
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSafeUint53(value: number, message?: string): void {
|
|
|
|
if (typeof(value) !== "number") { return; }
|
|
|
|
|
|
|
|
if (message == null) { message = "value not safe"; }
|
|
|
|
|
|
|
|
if (value < 0 || value >= 0x1fffffffffffff) {
|
|
|
|
this.throwError(message, Logger.errors.NUMERIC_FAULT, {
|
|
|
|
operation: "checkSafeInteger",
|
|
|
|
fault: "out-of-safe-range",
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value % 1) {
|
|
|
|
this.throwError(message, Logger.errors.NUMERIC_FAULT, {
|
|
|
|
operation: "checkSafeInteger",
|
|
|
|
fault: "non-integer",
|
|
|
|
value: value
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkArgumentCount(count: number, expectedCount: number, message?: string): void {
|
|
|
|
if (message) {
|
|
|
|
message = ": " + message;
|
|
|
|
} else {
|
|
|
|
message = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count < expectedCount) {
|
|
|
|
this.throwError("missing argument" + message, Logger.errors.MISSING_ARGUMENT, {
|
|
|
|
count: count,
|
|
|
|
expectedCount: expectedCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > expectedCount) {
|
|
|
|
this.throwError("too many arguments" + message, Logger.errors.UNEXPECTED_ARGUMENT, {
|
|
|
|
count: count,
|
|
|
|
expectedCount: expectedCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkNew(target: any, kind: any): void {
|
|
|
|
if (target === Object || target == null) {
|
|
|
|
this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkAbstract(target: any, kind: any): void {
|
|
|
|
if (target === kind) {
|
|
|
|
this.throwError(
|
|
|
|
"cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class",
|
|
|
|
Logger.errors.UNSUPPORTED_OPERATION,
|
|
|
|
{ name: target.name, operation: "new" }
|
|
|
|
);
|
|
|
|
} else if (target === Object || target == null) {
|
|
|
|
this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static globalLogger(): Logger {
|
|
|
|
if (!_globalLogger) { _globalLogger = new Logger(version); }
|
|
|
|
return _globalLogger;
|
|
|
|
}
|
|
|
|
|
|
|
|
static setCensorship(censorship: boolean, permanent?: boolean): void {
|
2020-02-17 20:22:33 -05:00
|
|
|
if (!censorship && permanent) {
|
|
|
|
this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "setCensorship"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
if (_permanentCensorErrors) {
|
|
|
|
if (!censorship) { return; }
|
|
|
|
this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "setCensorship"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_censorErrors = !!censorship;
|
|
|
|
_permanentCensorErrors = !!permanent;
|
|
|
|
}
|
2020-02-25 14:51:04 -05:00
|
|
|
|
|
|
|
static setLogLevel(logLevel: LogLevel): void {
|
2020-07-20 01:43:02 -04:00
|
|
|
const level = LogLevels[logLevel.toLowerCase()];
|
2020-02-25 14:51:04 -05:00
|
|
|
if (level == null) {
|
|
|
|
Logger.globalLogger().warn("invalid log level - " + logLevel);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-25 01:06:28 -04:00
|
|
|
_logLevel = level;
|
2020-02-25 14:51:04 -05:00
|
|
|
}
|
2020-11-14 17:42:36 -05:00
|
|
|
|
|
|
|
static from(version: string): Logger {
|
|
|
|
return new Logger(version);
|
|
|
|
}
|
2019-08-01 18:04:06 -04:00
|
|
|
}
|