diff --git a/web3.js/src/connection.js b/web3.js/src/connection.js index f3efb1ed27..51e2f6f65a 100644 --- a/web3.js/src/connection.js +++ b/web3.js/src/connection.js @@ -509,7 +509,11 @@ function createRpcRequest(url): RpcRequest { try { const res = await fetch(url, options); const text = await res.text(); - callback(null, text); + if (res.ok) { + callback(null, text); + } else { + callback(new Error(`${res.status} ${res.statusText}: ${text}`)); + } } catch (err) { callback(err); } diff --git a/web3.js/test/__mocks__/node-fetch.js b/web3.js/test/__mocks__/node-fetch.js index 3d4f738123..dbc0866768 100644 --- a/web3.js/test/__mocks__/node-fetch.js +++ b/web3.js/test/__mocks__/node-fetch.js @@ -74,6 +74,9 @@ const mock: JestMockFn = jest.fn((fetchUrl, fetchOptions) => { mockResponse, ); return { + ok: true, + status: 200, + statusText: 'OK', text: () => { return Promise.resolve(JSON.stringify(response)); },