Movatterモバイル変換


[0]ホーム

URL:


ethers
v5.7
Documentation
Getting Started
Ethereum Basics
Events
Gas
Security
Best Practices
Provider API Keys
Application Programming Interface
Providers
Provider
JsonRpcProvider
API Providers
Other Providers
Types
Signers
Contract Interaction
Contract
ContractFactory
Example: ERC-20 Contract
Utilities
Application Binary Interface
AbiCoder
ABI Formats
Fragments
Interface
Addresses
BigNumber
Byte Manipulation
Constants
Display Logic and Input
Encoding Utilities
FixedNumber
Hashing Algorithms
HD Wallet
Logging
Logger
Errors
Log Levels
Property Utilities
Signing Key
Strings
Transactions
Web Utilities
Wordlists
Other Libraries
Assembly
Ethers ASM Dialect
Utilities
Abstract Syntax Tree
Hardware Wallets
Experimental
Command Line Interfaces
Sandbox Utility
Assembler
Ethereum Naming Service
TypeScript
Making Your Own
Cookbook
React Native (and ilk)
Transactions
Migration Guide
Migration: From Web3.js
Migration: From Ethers v4
Testing
Contributing and Hacking
Other Resources
Flatworm Docs
License and Copyright
Single Page
Documentation  »  API  »  Utilities  »  Logging

Logging

These are just a few simple logging utilities provided to simplify and standardize the error facilities across the Ethers library.

TheLogger library has zero dependencies and is intentionally very light so it can be easily included in each library.

TheCensorship functionality relies on one instance of the Ethers library being included. In large bundled packages or whennpm link is used, this may not be the case. If you require this functionality, ensure that your bundling is configured properly.

Logger
source

newethers.utils.Logger(version)
source

Create a new logger which will includeversion in all errors thrown.

Logger.globalLogger()Logger
source

Returns the singleton global logger.

Logging Output

logger.debug( ...args)void
source

Log debugging information.

logger.info( ...args)void
source

Log generic information.

logger.warn( ...args)void
source

Log warnings.

Errors

These functions honor the currentCensorship and help create a standard error model for detecting and processing errors within Ethers.

logger.makeError(message[,code =UNKNOWN_ERROR[,params]])Error
source

Create an Error object withmessage and an optionalcode and additionalparams set. This is useful when an error is needed to be rejected instead of thrown.

logger.throwError(message[,code =UNKNOWN_ERROR[,params]])never
source

Throw an Error withmessage and an optionalcode and additionalparams set.

logger.throwArgumentError(message,name,value)never
source

Throw anINVALID_ARGUMENT Error withname andvalue.

Usage Validation

There can be used to ensure various properties and actions are safe.

logger.checkAbstract(target,kind)void
source

Iftarget iskind, throws aUNSUPPORTED_OPERATION error otherwise performs the same operations ascheckNew.

This is useful for ensuring abstract classes are not being instantiated.

logger.checkArgumentCount(count,expectedCount[,message)void
source

Ifcount is not equal toexpectedCount, throws aMISSING_ARGUMENT orUNEXPECTED_ARGUMENT error.

logger.checkNew(target,kind)void
source

Iftarget is not a validthis ortarget value, throw aMISSING_NEW error. This is useful to ensure callers of a Class are usingnew.

logger.checkNormalize(message)void
source

Check that the environment has a correctly functioningString.normalize. If not, aUNSUPPORTED_OPERATION error is thrown.

logger.checkSafeUint53(value[,message])void
source

Ifvalue is not safe as aJavaScript number, throws aNUMERIC_FAULT error.

Censorship

Logger.setCensorship(censor[,permanent =false])void
source

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.

Logger.setLogLevel(logLevel)void
source

Set the log level, to suppress logging output below aparticular log level.

Errors

Every error in Ethers has acode value, which is a string that will match one of the following error codes.

Generic Error Codes

Logger.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.

Logger.errors.SERVER_ERROR

There was an error communicating with a server.

This may occur for a number of reasons, for example:

  • aCORS 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

Logger.errors.TIMEOUT

A timeout occurred.

Logger.errors.UNKNOWN_ERROR

A generic unknown error.

Logger.errors.UNSUPPORTED_OPERATION

The operation is not supported.

This can happen for a variety reasons, for example:

Safety Error Codes

Logger.errors.BUFFER_OVERRUN

The amount of data needed is more than the amount of data required, which would cause the data buffer to read past its end.

This can occur if a contract erroneously returns invalid ABI-encoded data or RLP data is malformed.

Logger.errors.NUMERIC_FAULT

There was an invalid operation done on numeric values.

Common cases of this occur when there isoverflow,arithmetic underflow in fixed numeric types or division by zero.

Usage Error Codes

Logger.errors.INVALID_ARGUMENT

The type or value of an argument is invalid. This will generally also include thename andvalue of the argument. Any function which accepts sensitive data (such as a private key) will include the string"[[REDACTED]]" instead of the value passed in.

Logger.errors.MISSING_ARGUMENT

An expected parameter was not specified.

Logger.errors.MISSING_NEW

An object is a Class, but is not being called withnew.

Logger.errors.UNEXPECTED_ARGUMENT

Too many parameters we passed into a function.

Ethereum Error Codes

Logger.errors.CALL_EXCEPTION

An attempt to call a blockchain contract (getter) resulted in a 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 aTransactionReceipt which failed during execution.

Consult the contract to determine the cause, such as a failed condition in arequire statement. Thereason property may provide more context for the cause of this error.

Logger.errors.INSUFFICIENT_FUNDS

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 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 noto property and is therefore expected to create a new account.

Logger.errors.NETWORK_ERROR

An Ethereum network validation error, such as an invalid chain ID.

Logger.errors.NONCE_EXPIRED

The nonce being specified has already been used in a mined transaction.

Logger.errors.REPLACEMENT_UNDERPRICED

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 tobribe 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 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.

Logger.errors.TRANSACTION_REPLACED

When a transaction has been replaced by the user, by broadcasting a new transaction with the same nonce as an existing in-flight (unmined) transaction in the mempool, this error will occur while waiting if the transaction being waited for has become invalidated by that other transaction.

This can happen for several reasons, but most commonly because the user has increased the gas price (which changes the transaction hash) to "speed up" a transaction or if a user has "cancelled" the transaction in their client. In either case this is usually accomplished by bribing the miners with a higher gas priced transaction.

This error will have the additional properties,cancelled,hash,reason,receipt andreplacement.

See theTransactionResponse for thewait method for more details.

Logger.errors.UNPREDICTABLE_GAS_LIMIT

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.

Log Levels

Logger.levels.DEBUG

Log all output, including debugging information.

Logger.levels.INFO

Only log output for informational, warnings and errors.

Logger.levels.WARNING

Only log output for warnings and errors.

Logger.levels.ERROR

Only log output for errors.

Logger.levels.OFF

Do not output any logs.

HD Wallet
Property Utilities
The content of this site is licensed under theCreative Commons License. Generated on April 6, 2023, 1:54am.

[8]ページ先頭

©2009-2025 Movatter.jp