status_code
attribute of the response object. This attribute holds the status code that the server returned for the HTTP request.
Let’s consider a scenario where we’re interested in getting the logs of the latest block. We can do this using the following Python code:
response_code
variable. Now that we know how to retrieve the status code of a response, we can move on to handling these codes and analyzing error responses.
eth_getLogs
request that contains an error content in the output:
error
field, which contains further information about the error that occurred. We can extract this information in our Python code like this:
error
field. If it does, we store the content of this field in the error_content
variable. This information can be used to implement a retry logic and keep a record of whenever those errors happen in time.
retries
variable). For each iteration of the loop, which represents an attempt to fetch the logs, the code performs the following steps:
POST
request is sent to the Ethereum node with the defined headers and payload.delay
variable) before proceeding to the next iteration of the loop. This delay provides a pause before retrying, which can be helpful in cases where the server might be temporarily overloaded or experiencing other transient issues.error
key. If error
is present, the code prints a message with the error details and the current attempt number, waits for the specified delay period, and proceeds to the next iteration of the loop. This handles cases where the request was technically successful, but the response indicates an error condition that might be resolved with a retry.error
key in the response but the result
is empty, the code prints a message indicating this fact and the current attempt number, waits for the specified delay period, and proceeds to the next iteration of the loop. This handles situations where the request was successful and didn’t result in an error but didn’t provide any logs to process.