ethers.js/docs.wrm/api/utils/logger.wrm

248 lines
10 KiB
Plaintext
Raw Normal View History

2020-02-25 22:57:11 +03:00
_section: Logging @<logging>
2020-02-18 01:56:13 +03:00
2020-02-25 22:57:11 +03:00
These are just a few simple logging utilities provided to simplify
and standardize the error facilities across the Ethers library.
2020-02-18 01:56:13 +03:00
2020-05-08 10:24:40 +03:00
The [[Logger]] library has zero dependencies and is intentionally
2020-02-25 22:57:11 +03:00
very light so it can be easily included in each library.
2020-02-18 01:56:13 +03:00
2020-05-08 10:24:40 +03:00
The [Censorship](Logger--censorship) functionality relies on one instance
2020-02-25 22:57:11 +03:00
of the Ethers library being included. In large bundled packages or when
``npm link`` is used, this may not be the case. If you require this
functionality, ensure that your bundling is configured properly.
2020-02-18 01:56:13 +03:00
2020-05-08 10:24:40 +03:00
_subsection: Logger @<Logger> @SRC<logger:class.Logger>
2020-02-18 01:56:13 +03:00
2020-02-25 22:57:11 +03:00
_property: new ethers.utils.Logger(version) @SRC<logger:constructor.Logger>
Create a new logger which will include //version// in all errors thrown.
2020-05-08 10:24:40 +03:00
_property: Logger.globalLogger() => [[Logger]] @SRC<logger>
2020-02-25 22:57:11 +03:00
Returns the singleton global logger.
_heading: Logging Output
_property: logger.debug(...args) => void @SRC<logger>
Log debugging information.
_property: logger.info(...args) => void @SRC<logger>
Log generic information.
_property: logger.warn(...args) => void @SRC<logger>
Log warnings.
_heading: Errors
2020-05-08 10:24:40 +03:00
These functions honor the current [Censorship](Logger--censorship) and help create
2020-02-25 22:57:11 +03:00
a standard error model for detecting and processing errors within Ethers.
_property: logger.makeError(message [ , code = UNKNOWN_ERROR [ , params ] ]) => Error @SRC<logger>
Create an Error object with //message// and an optional //code// and
additional //params// set. This is useful when an error is needed to be
rejected instead of thrown.
_property: logger.throwError(message [ , code = UNKNOWN_ERROR [ , params ] ]) => never @SRC<logger>
Throw an Error with //message// and an optional //code// and
additional //params// set.
_property: logger.throwArgumentError(message, name, value) => never @SRC<logger>
2021-02-05 02:59:51 +03:00
Throw an [INVALID_ARGUMENT](errors--invalid-argument) Error with //name// and //value//.
2020-02-25 22:57:11 +03:00
_heading: Usage Validation
There can be used to ensure various properties and actions are safe.
2021-02-05 02:59:51 +03:00
_property: logger.checkAbstract(target, kind) => void @<Logger-checkAbstract> @SRC<logger>
If //target// is //kind//, throws a [UNSUPPORTED_OPERATION](errors--unsupported-operation) error
otherwise performs the same operations as [checkNew](Logger-checkNew).
This is useful for ensuring abstract classes are not being instantiated.
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: logger.checkArgumentCount(count, expectedCount [ , message) => void @<Logger-checkArgumentCount> @SRC<logger>
If //count// is not equal to //expectedCount//, throws a [MISSING_ARGUMENT](errors--missing-argument)
or [UNEXPECTED_ARGUMENT](errors--unexpected-argument) error.
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: logger.checkNew(target, kind) => void @<Logger-checkNew> @SRC<logger>
2020-02-25 22:57:11 +03:00
If //target// is not a valid ``this`` or ``target`` value, throw a
2021-02-05 02:59:51 +03:00
[MISSING_NEW](errors--missing-new) error. This is useful to ensure
2020-02-25 22:57:11 +03:00
callers of a Class are using ``new``.
2021-02-05 02:59:51 +03:00
_property: logger.checkNormalize(message) => void @<Logger-checkNoralize> @SRC<logger>
2020-02-25 22:57:11 +03:00
Check that the environment has a correctly functioning [[link-js-normalize]]. If not, a
2021-02-05 02:59:51 +03:00
[UNSUPPORTED_OPERATION](errors--unsupported-operation) error is thrown.
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: logger.checkSafeUint53(value [, message ]) => void @<Logger-checkSafeUint53> @SRC<logger>
2020-02-25 22:57:11 +03:00
If //value// is not safe as a [JavaScript number](link-wiki-ieee754), throws a
2021-02-05 02:59:51 +03:00
[NUMERIC_FAULT](errors--numeric-fault) error.
2020-02-25 22:57:11 +03:00
2020-05-08 10:24:40 +03:00
_heading: Censorship @<Logger--censorship>
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.setCensorship(censor [ , permanent = false ]) => void @<Logger-setCensorship> @SRC<logger>
2020-02-25 22:57:11 +03:00
Set error censorship, optionally preventing errors from being uncensored.
In production applications, this prevents any error from leaking information
by masking the message and values of errors.
This can impact debugging, making it substantially more difficult.
2021-02-05 02:59:51 +03:00
_property: Logger.setLogLevel(logLevel) => void @<Logger-setLogLevel> @SRC<logger>
2020-05-08 10:24:40 +03:00
Set the log level, to suppress logging output below a [particular log level](Logger-levels).
2020-02-25 22:57:11 +03:00
_subsection: Errors @<errors>
Every error in Ethers has a ``code`` value, which is a string that will
match one of the following error codes.
2021-02-05 02:59:51 +03:00
_heading: Generic Error Codes @<errors-generic>
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.NOT_IMPLEMENTED @<errors--not-implemented>
The operation is not implemented. This may occur when calling a method
on a sub-class that has not fully implemented its abstract superclass.
2020-02-18 01:56:13 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.SERVER_ERROR @<errors--server-error>
2020-02-18 01:56:13 +03:00
There was an error communicating with a server.
2021-02-05 02:59:51 +03:00
This may occur for a number of reasons, for example:
- a [CORS](link-cors) issue; this is quite often the problem and also the
hardest to diagnose and fix, so it is very beneficial to familiarize
yourself with CORS; some backends allow you configure your CORS, such as
the geth command-line or conifguration files or the INFURA and Alchemy
dashboards by specifing allowed Origins, methods, etc.
- an SSL issue; for example, if you are trying to connect to a local node via
HTTP but are serving the content from a secure HTTPS website
- a link issue; a firewall is preventing the traffic from reaching the server
- a server issue; the server is down, or is returning 500 error codes
- a backend DDoS mitigation proxy; for example, Etherscan operates behind a
Cloudflare proxy, which will block traffic if the request is sent via
specific User Agents or the client fingerprint is detected as a bot in some
cases
_property: Logger.errors.TIMEOUT @<errors--timeout>
2020-02-18 01:56:13 +03:00
A timeout occurred.
2021-02-05 02:59:51 +03:00
_property: Logger.errors.UNKNOWN_ERROR @<errors--unknown-error>
2020-02-25 22:57:11 +03:00
A generic unknown error.
2021-02-05 02:59:51 +03:00
_property: Logger.errors.UNSUPPORTED_OPERATION @<errors--unsupported-operation>
2020-02-25 22:57:11 +03:00
The operation is not supported.
2021-02-05 02:59:51 +03:00
This can happen for a variety reasons, for example:
- Some backends do not support certain operations; such as passing a blockTag
to an [[EtherscanProvider]] for [call](Provider-call)
- A [[Contract]] object connected to [[Provider]] (instead of a [[Signer]]) cannot
[sign](Signer-signTransaction) or [send](Signer-sendTransaction) transactions
- a [[Contract]] connected to a [[Signer]] without a [[Provider]] is write-only
and cannot estimate gas or execute static calls
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_heading: Safety Error Codes @<errors-safety>
_property: Logger.errors.BUFFER_OVERRUN @<errors--buffer-overrun>
2020-02-18 01:56:13 +03:00
The amount of data needed is more than the amount of data required,
which would cause the data buffer to read past its end.
2021-02-05 02:59:51 +03:00
This can occur if a contract erroneously returns invalid ABI-encoded
data or RLP data is malformed.
_property: Logger.errors.NUMERIC_FAULT @<errors--numeric-fault>
2020-02-18 01:56:13 +03:00
There was an invalid operation done on numeric values.
Common cases of this occur when there is [[link-wiki-overflow]],
[[link-wiki-underflow]] in fixed numeric types or division by zero.
2021-02-05 02:59:51 +03:00
_heading: Usage Error Codes @<errors-usage>
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.INVALID_ARGUMENT @<errors--invalid-argument>
2020-02-18 01:56:13 +03:00
The type or value of an argument is invalid. This will generally also
include the ``name`` and ``value`` of the argument. Any function which
accepts sensitive data (such as a private key) will include the string
2021-02-05 02:59:51 +03:00
``"[\[REDACTED]\]"`` instead of the value passed in.
2020-02-18 01:56:13 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.MISSING_ARGUMENT @<errors--missing-argument>
2020-02-18 01:56:13 +03:00
An expected parameter was not specified.
2021-02-05 02:59:51 +03:00
_property: Logger.errors.MISSING_NEW @<errors--missing-new>
An object is a Class, but is not being called with ``new``.
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.UNEXPECTED_ARGUMENT @<errors--unexpected-argument>
2020-02-18 01:56:13 +03:00
Too many parameters we passed into a function.
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_heading: Ethereum Error Codes @<errors-ethereum>
2020-02-25 22:57:11 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.CALL_EXCEPTION @<errors--call-exception>
2020-02-18 01:56:13 +03:00
An attempt to call a blockchain contract (getter) resulted in a
2021-02-05 02:59:51 +03:00
revert or other error, such as insufficient gas (out-of-gas) or an
invalid opcode. This can also occur during gas estimation or if
waiting for a [[providers-TransactionReceipt]] which failed during execution.
Consult the contract to determine the cause, such as a failed condition
in a ``require`` statement. The ``reason`` property may provide more
context for the cause of this error.
2020-02-18 01:56:13 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.INSUFFICIENT_FUNDS @<errors--insufficient-funds>
2020-02-18 01:56:13 +03:00
The account is attempting to make a transaction which costs more than is
available.
A sending account must have enough ether to pay for the value, the gas limit
(at the gas price) as well as the intrinsic cost of data. The intrinsic cost
2021-02-05 02:59:51 +03:00
of data is 4 gas for each zero byte and 68 gas for each non-zero byte, as well
as 35000 gas if a transaction contains no ``to`` property and is therefore
expected to create a new account.
2020-02-18 01:56:13 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.NETWORK_ERROR @<errors--network>
2020-02-25 22:57:11 +03:00
An Ethereum network validation error, such as an invalid chain ID.
2021-02-05 02:59:51 +03:00
_property: Logger.errors.NONCE_EXPIRED @<errors--nonce-expired>
2020-02-18 01:56:13 +03:00
The nonce being specified has already been used in a mined transaction.
2021-02-05 02:59:51 +03:00
_property: Logger.errors.REPLACEMENT_UNDERPRICED @<errors--replacement-underpriced>
2020-02-18 01:56:13 +03:00
When replacing a transaction, by using a nonce which has already been sent to
the network, but which has not been mined yet the new transaction must specify
a higher gas price.
This error occurs when the gas price is insufficient to //bribe// the transaction
pool to prefer the new transaction over the old one. Generally, the new gas price
should be about 50% + 1 wei more, so if a gas price of 10 gwei was used, the
2021-02-05 02:59:51 +03:00
replacement should be 15.000000001 gwei. This is not enforced by the protocol, as
it deals with unmined transactions, and can be configured by each node, however
to ensure a transaction is propagated to a miner it is best practice to follow
the defaults most nodes have enabled.
2020-02-18 01:56:13 +03:00
2021-02-05 02:59:51 +03:00
_property: Logger.errors.UNPREDICTABLE_GAS_LIMIT @<errors--unpredicatable-gas-limit>
2020-02-18 01:56:13 +03:00
When estimating the required amount of gas for a transaction, a node is queried for
its best guess.
If a node is unable (or unwilling) to predict the cost, this error occurs.
The best remedy for this situation is to specify a gas limit in the transaction
manually.
This error can also indicate that the transaction is expected to fail regardless,
if for example an account with no tokens is attempting to send a token.
2020-05-08 10:24:40 +03:00
_subsection: Log Levels @<Logger-levels>
2020-02-18 01:56:13 +03:00
2020-02-25 22:57:11 +03:00
_property: Logger.levels.DEBUG
Log all output, including debugging information.
_property: Logger.levels.INFO
2020-11-23 07:03:50 +03:00
Only log output for informational, warnings and errors.
2020-02-25 22:57:11 +03:00
_property: Logger.levels.WARNING
Only log output for warnings and errors.
_property: Logger.levels.ERROR
Only log output for errors.
2020-02-18 01:56:13 +03:00
2020-02-25 22:57:11 +03:00
_property: Logger.levels.OFF
Do not output any logs.