Handle JSONRPC error codes in web3 providers (#491)

This commit is contained in:
Kirill Fedoseev 2020-11-12 02:20:03 +03:00 committed by GitHub
parent 1122daf9a1
commit 5327688a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

@ -1,6 +1,9 @@
const fetch = require('node-fetch')
const promiseRetry = require('promise-retry')
// From EIP-1474 and Infura documentation
const JSONRPC_ERROR_CODES = [-32603, -32002, -32005]
const defaultOptions = {
requestTimeout: 0,
retry: {
@ -61,6 +64,12 @@ function send(url, payload, options) {
}
})
.then(response => response.json())
.then(response => {
if (response.error && JSONRPC_ERROR_CODES.includes(response.error.code)) {
throw new Error(response.error.message)
}
return response
})
}
async function trySend(payload, urls, initialIndex, options) {

@ -13,7 +13,6 @@ function RedundantHttpListProvider(urls, options = {}) {
this.urls = urls
this.options = { ...defaultOptions, ...options }
this.currentIndex = 0
}
RedundantHttpListProvider.prototype.send = async function send(payload, callback) {