ethers.js/packages/logger/lib/index.js

371 lines
14 KiB
JavaScript
Raw Normal View History

2019-08-02 09:10:58 +03:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-08 02:24:04 +03:00
exports.Logger = exports.ErrorCode = exports.LogLevel = void 0;
2019-08-02 09:10:58 +03:00
var _permanentCensorErrors = false;
var _censorErrors = false;
2020-03-21 19:48:22 +03:00
var LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
2020-04-25 10:54:54 +03:00
var _logLevel = LogLevels["default"];
2019-08-02 09:10:58 +03:00
var _version_1 = require("./_version");
var _globalLogger = null;
function _checkNormalize() {
try {
var missing_1 = [];
// Make sure all forms of normalization are supported
["NFD", "NFC", "NFKD", "NFKC"].forEach(function (form) {
try {
if ("test".normalize(form) !== "test") {
throw new Error("bad normalize");
}
;
}
catch (error) {
missing_1.push(form);
}
});
if (missing_1.length) {
throw new Error("missing " + missing_1.join(", "));
}
if (String.fromCharCode(0xe9).normalize("NFD") !== String.fromCharCode(0x65, 0x0301)) {
throw new Error("broken implementation");
}
}
catch (error) {
return error.message;
}
return null;
}
var _normalizeError = _checkNormalize();
2020-04-25 10:54:54 +03:00
var LogLevel;
(function (LogLevel) {
LogLevel["DEBUG"] = "DEBUG";
LogLevel["INFO"] = "INFO";
LogLevel["WARNING"] = "WARNING";
LogLevel["ERROR"] = "ERROR";
LogLevel["OFF"] = "OFF";
})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
var ErrorCode;
(function (ErrorCode) {
///////////////////
// Generic Errors
// Unknown Error
ErrorCode["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
// Not Implemented
ErrorCode["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
// Unsupported Operation
// - operation
ErrorCode["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
// Network Error (i.e. Ethereum Network, such as an invalid chain ID)
2020-06-14 04:39:36 +03:00
// - event ("noNetwork" is not re-thrown in provider.ready; otherwise thrown)
2020-04-25 10:54:54 +03:00
ErrorCode["NETWORK_ERROR"] = "NETWORK_ERROR";
// Some sort of bad response from the server
ErrorCode["SERVER_ERROR"] = "SERVER_ERROR";
// Timeout
ErrorCode["TIMEOUT"] = "TIMEOUT";
///////////////////
// Operational Errors
// Buffer Overrun
ErrorCode["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
// Numeric Fault
// - operation: the operation being executed
// - fault: the reason this faulted
ErrorCode["NUMERIC_FAULT"] = "NUMERIC_FAULT";
///////////////////
// Argument Errors
// Missing new operator to an object
// - name: The name of the class
ErrorCode["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
ErrorCode["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
// Missing argument to a function:
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
ErrorCode["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
// Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
ErrorCode["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
///////////////////
// 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)")
ErrorCode["CALL_EXCEPTION"] = "CALL_EXCEPTION";
2021-10-16 09:29:27 +03:00
// Insufficient funds (< value + gasLimit * gasPrice)
2020-04-25 10:54:54 +03:00
// - transaction: the transaction attempted
ErrorCode["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
// Nonce has already been used
// - transaction: the transaction attempted
ErrorCode["NONCE_EXPIRED"] = "NONCE_EXPIRED";
// The replacement fee for the transaction is too low
// - transaction: the transaction attempted
ErrorCode["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
// The gas limit could not be estimated
// - transaction: the transaction passed to estimateGas
ErrorCode["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
2021-05-17 23:19:36 +03: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
// - receipt: the receipt of the replacement
ErrorCode["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
2022-08-18 21:48:39 +03:00
///////////////////
// Interaction Errors
// The user rejected the action, such as signing a message or sending
// a transaction
ErrorCode["ACTION_REJECTED"] = "ACTION_REJECTED";
2020-04-25 10:54:54 +03:00
})(ErrorCode = exports.ErrorCode || (exports.ErrorCode = {}));
;
2021-08-24 22:15:51 +03:00
var HEX = "0123456789abcdef";
2019-08-02 09:10:58 +03:00
var Logger = /** @class */ (function () {
function Logger(version) {
Object.defineProperty(this, "version", {
enumerable: true,
value: version,
writable: false
});
}
Logger.prototype._log = function (logLevel, args) {
2020-03-21 19:48:22 +03:00
var level = logLevel.toLowerCase();
if (LogLevels[level] == null) {
this.throwArgumentError("invalid log level name", "logLevel", logLevel);
}
2020-04-25 10:54:54 +03:00
if (_logLevel > LogLevels[level]) {
2019-08-02 09:10:58 +03:00
return;
}
console.log.apply(console, args);
};
Logger.prototype.debug = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this._log(Logger.levels.DEBUG, args);
};
Logger.prototype.info = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this._log(Logger.levels.INFO, args);
};
Logger.prototype.warn = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
this._log(Logger.levels.WARNING, args);
};
Logger.prototype.makeError = function (message, code, params) {
2020-02-18 04:22:33 +03:00
// Errors are being censored
2019-08-02 09:10:58 +03:00
if (_censorErrors) {
2020-02-18 04:22:33 +03:00
return this.makeError("censored error", code, {});
2019-08-02 09:10:58 +03:00
}
if (!code) {
code = Logger.errors.UNKNOWN_ERROR;
}
if (!params) {
params = {};
}
var messageDetails = [];
Object.keys(params).forEach(function (key) {
2021-08-24 22:15:51 +03:00
var value = params[key];
2019-08-02 09:10:58 +03:00
try {
2021-08-24 22:15:51 +03:00
if (value instanceof Uint8Array) {
var hex = "";
for (var i = 0; i < value.length; i++) {
hex += HEX[value[i] >> 4];
hex += HEX[value[i] & 0x0f];
}
messageDetails.push(key + "=Uint8Array(0x" + hex + ")");
}
else {
messageDetails.push(key + "=" + JSON.stringify(value));
}
2019-08-02 09:10:58 +03:00
}
catch (error) {
messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
}
});
2020-02-04 09:06:47 +03:00
messageDetails.push("code=" + code);
2019-08-02 09:10:58 +03:00
messageDetails.push("version=" + this.version);
var reason = message;
2022-03-09 10:56:08 +03:00
var url = "";
switch (code) {
case ErrorCode.NUMERIC_FAULT: {
url = "NUMERIC_FAULT";
var fault = message;
switch (fault) {
case "overflow":
case "underflow":
case "division-by-zero":
url += "-" + fault;
break;
case "negative-power":
case "negative-width":
url += "-unsupported";
break;
case "unbound-bitwise-result":
url += "-unbound-result";
break;
}
break;
}
case ErrorCode.CALL_EXCEPTION:
case ErrorCode.INSUFFICIENT_FUNDS:
case ErrorCode.MISSING_NEW:
case ErrorCode.NONCE_EXPIRED:
case ErrorCode.REPLACEMENT_UNDERPRICED:
case ErrorCode.TRANSACTION_REPLACED:
case ErrorCode.UNPREDICTABLE_GAS_LIMIT:
url = code;
break;
}
if (url) {
message += " [ See: https:/\/links.ethers.org/v5-errors-" + url + " ]";
}
2019-08-02 09:10:58 +03:00
if (messageDetails.length) {
message += " (" + messageDetails.join(", ") + ")";
}
// @TODO: Any??
var error = new Error(message);
error.reason = reason;
error.code = code;
Object.keys(params).forEach(function (key) {
error[key] = params[key];
});
return error;
};
Logger.prototype.throwError = function (message, code, params) {
throw this.makeError(message, code, params);
};
Logger.prototype.throwArgumentError = function (message, name, value) {
return this.throwError(message, Logger.errors.INVALID_ARGUMENT, {
argument: name,
value: value
});
};
2020-07-13 15:03:56 +03:00
Logger.prototype.assert = function (condition, message, code, params) {
if (!!condition) {
return;
}
this.throwError(message, code, params);
};
Logger.prototype.assertArgument = function (condition, message, name, value) {
if (!!condition) {
return;
}
this.throwArgumentError(message, name, value);
};
2019-08-02 09:10:58 +03:00
Logger.prototype.checkNormalize = function (message) {
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
});
}
};
Logger.prototype.checkSafeUint53 = function (value, message) {
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
});
}
};
Logger.prototype.checkArgumentCount = function (count, expectedCount, message) {
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
});
}
};
Logger.prototype.checkNew = function (target, kind) {
if (target === Object || target == null) {
this.throwError("missing new", Logger.errors.MISSING_NEW, { name: kind.name });
}
};
Logger.prototype.checkAbstract = function (target, kind) {
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 });
}
};
Logger.globalLogger = function () {
if (!_globalLogger) {
_globalLogger = new Logger(_version_1.version);
}
return _globalLogger;
};
Logger.setCensorship = function (censorship, permanent) {
2020-02-18 04:22:33 +03:00
if (!censorship && permanent) {
this.globalLogger().throwError("cannot permanently disable censorship", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "setCensorship"
});
}
2019-08-02 09:10:58 +03:00
if (_permanentCensorErrors) {
if (!censorship) {
return;
}
this.globalLogger().throwError("error censorship permanent", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "setCensorship"
});
}
_censorErrors = !!censorship;
_permanentCensorErrors = !!permanent;
};
2020-02-26 13:06:48 +03:00
Logger.setLogLevel = function (logLevel) {
2020-07-20 09:27:26 +03:00
var level = LogLevels[logLevel.toLowerCase()];
2020-02-26 13:06:48 +03:00
if (level == null) {
Logger.globalLogger().warn("invalid log level - " + logLevel);
return;
}
2020-04-25 10:54:54 +03:00
_logLevel = level;
2019-08-02 09:10:58 +03:00
};
2020-11-17 07:07:24 +03:00
Logger.from = function (version) {
return new Logger(version);
};
2020-04-25 10:54:54 +03:00
Logger.errors = ErrorCode;
Logger.levels = LogLevel;
2019-08-02 09:10:58 +03:00
return Logger;
}());
exports.Logger = Logger;
2020-07-13 15:03:56 +03:00
//# sourceMappingURL=index.js.map