Code Tree
Code Tree

Reputation: 2209

catching GuzzleHttp \ Exception \ ConnectException and showing a custom message

I couldn't get a work around to catch this error and showing a custom message to user, I tried to use web3js with laravel https://github.com/IlyasDeckers/web3php

and when I tried to use this

      $eth = new \IlyasDeckers\Web3PHP\Ethereum(env('WEB3_URL'), env('WEB3_PORT'));
     try {
        $eth = $eth->eth_getBalance('0x8fbb99e9e73cd62bb3adea5365ff0f9d90c9e532', $block='latest', $decode_hex=false);
    }

    catch(ConnectException $e) {
    echo 'Message: ' .$e->getMessage();
    exit;
   }

I am getting the error which cannot be caught, can anyone help on this?

GuzzleHttp \ Exception \ ConnectException cURL error 7: Failed to connect to 127.0.0.1 port 8545: Connection refused (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

enter image description here

Upvotes: 1

Views: 4507

Answers (1)

delboy1978uk
delboy1978uk

Reputation: 12365

It sounds very much to me like you aren't running an Ethereum node. It's trying to connect to localhost on port 8545.

If you are running a node, check the port!

If you aren't running a node, either set one up, or find one that will allow you to connect (far less likely tbh).

UPDATE

I notice you are catching a ConnectException, the fully qualified classname being GuzzleHttp\Exception\ConnectException.

If you haven't imported the class name using a use statement, then you need to change your code to this:

catch (\GuzzleHttp\Exception\ConnectException) {

(note the leading backslash). However, this isn't the recommended way. It's better to import all your classes at the top of your script:

use GuzzleHttp\Exception\ConnectException;

That way, you can instantly see all classes used in the script, and you can refer to the class as just ConnectException throughout your code.

Upvotes: 3

Related Questions