FreeDSx / SNMP

A Pure PHP SNMP Library.
MIT License
58 stars 15 forks source link

Finding issues with Exception class.. #21

Open Guyverix opened 1 year ago

Guyverix commented 1 year ago

I have begun working on using this class, and it works great. However I am running into trouble when invalid information is passed to the class. I specifically am testing an invalid IP address, as well as invalid community names. Currently I am getting fatal errors in PHP for both. This is against both PHP7.4, as well as PHP8.2 and am having a hell of a time getting the try catch block to work as you designed.

I have been attempting to alter the throw so it does not spew these errors but have not been having any luck there or in making a __construct() within the ConnectionException to attempt to grab the error. I do see the error information when I do a print_r and it returns the object, but suspect that I am not getting all the way to the \Exception class for some reason..

Any suggestions or guidance on how to fix this would be appreciated.. ( Still learning the more complex areas of PHP, and it is biting me in the rear end)

chubbard@lnms01:/opt/nmsApi/testing/freedsx$ /usr/bin/php7.4 ./testDisc.php 
PHP Fatal error:  Uncaught FreeDSx\Snmp\Exception\ConnectionException in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php:152
Stack trace:
#0 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(122): FreeDSx\Snmp\Protocol\ClientProtocolHandler->sendRequestGetResponse()
#1 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(233): FreeDSx\Snmp\Protocol\ClientProtocolHandler->handle()
#2 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(113): FreeDSx\Snmp\SnmpClient->send()
#3 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(126): FreeDSx\Snmp\SnmpClient->get()
#4 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(139): FreeDSx\Snmp\SnmpClient->getOid()
#5 /opt/nmsApi/testing/freedsx/testDisc.php(15): FreeDSx\Snmp\SnmpClient->getValue()
#6 {main}
  thrown in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php on line 152

chubbard@nms01:/opt/nmsApi/testing/freedsx$ /usr/bin/php8.2 ./testDisc.php 
PHP Fatal error:  Uncaught FreeDSx\Snmp\Exception\ConnectionException in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php:152
Stack trace:
#0 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(122): FreeDSx\Snmp\Protocol\ClientProtocolHandler->sendRequestGetResponse()
#1 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(233): FreeDSx\Snmp\Protocol\ClientProtocolHandler->handle()
#2 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(113): FreeDSx\Snmp\SnmpClient->send()
#3 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(126): FreeDSx\Snmp\SnmpClient->get()
#4 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(139): FreeDSx\Snmp\SnmpClient->getOid()
#5 /opt/nmsApi/testing/freedsx/testDisc.php(15): FreeDSx\Snmp\SnmpClient->getValue()
#6 {main}
  thrown in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php on line 152

This is what I am attempting to run:

cat testDisc.php 
<?php
require  __DIR__ . '/../../vendor/autoload.php';
use \FreeDSx\Snmp\SnmpClient;
$snmp = new SnmpClient([
    'host' => '192.168.15.589',
    'version' => 2,
    'community' => 'public2',
    'timeout_connect' => 1,
    'timeout_read' => 1,
]);
# Get a specific OID value as a string...
$data= $snmp->getValue('1.3.6.1.2.1.1.1.0');
echo $data . "\n\n";
?>
Guyverix commented 1 year ago

Continuing to poke at this, and reverted back to your original code, it looks like I was making matters worse due to my inexperience. I do still get the fatal errors, but it is returning the exception data in an almost usable way. I am just unsure what I can do about the Fatal error that is being spewed back..

/usr/bin/php8.2 ./blah.php 
PHP Fatal error:  Uncaught FreeDSx\Socket\Exception\ConnectionException: Unable to connect to 192.168.15.589: php_network_getaddresses: getaddrinfo for 192.168.15.589 failed: Name or service not known in /opt/nmsApi/vendor/freedsx/socket/src/FreeDSx/Socket/Socket.php:236
Stack trace:
#0 /opt/nmsApi/vendor/freedsx/socket/src/FreeDSx/Socket/Socket.php(269): FreeDSx\Socket\Socket->connect()
#1 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ProtocolTrait.php(145): FreeDSx\Socket\Socket::create()
#2 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(150): FreeDSx\Snmp\Protocol\ClientProtocolHandler->socket()
#3 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(122): FreeDSx\Snmp\Protocol\ClientProtocolHandler->sendRequestGetResponse()
#4 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(233): FreeDSx\Snmp\Protocol\ClientProtocolHandler->handle()
#5 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(113): FreeDSx\Snmp\SnmpClient->send()
#6 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(126): FreeDSx\Snmp\SnmpClient->get()
#7 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(139): FreeDSx\Snmp\SnmpClient->getOid()
#8 /opt/nmsApi/testing/freedsx/blah.php(14): FreeDSx\Snmp\SnmpClient->getValue()
#9 {main}

Next FreeDSx\Snmp\Exception\ConnectionException: Unable to connect to 192.168.15.589: php_network_getaddresses: getaddrinfo for 192.168.15.589 failed: Name or service not known in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ProtocolTrait.php:157
Stack trace:
#0 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(150): FreeDSx\Snmp\Protocol\ClientProtocolHandler->socket()
#1 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ClientProtocolHandler.php(122): FreeDSx\Snmp\Protocol\ClientProtocolHandler->sendRequestGetResponse()
#2 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(233): FreeDSx\Snmp\Protocol\ClientProtocolHandler->handle()
#3 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(113): FreeDSx\Snmp\SnmpClient->send()
#4 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(126): FreeDSx\Snmp\SnmpClient->get()
#5 /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/SnmpClient.php(139): FreeDSx\Snmp\SnmpClient->getOid()
#6 /opt/nmsApi/testing/freedsx/blah.php(14): FreeDSx\Snmp\SnmpClient->getValue()
#7 {main}
  thrown in /opt/nmsApi/vendor/freedsx/snmp/src/FreeDSx/Snmp/Protocol/ProtocolTrait.php on line 157
ChadSikorra commented 1 year ago

Hi @Guyverix! Are you saying that you want to catch the ConnectionException being thrown and do something with it? Or that you're not expecting it to give that message? 🤔 Only asking as you did mention giving it invalid data.

Guyverix commented 1 year ago

I am attempting to catch the exception. Both of the exception catches I expected to catch the problem spit the fatal error. I have tried many other catches as well, but am guessing one of these two would be the most likely to work..

    catch (FreeDSx\Socket\Exception\ConnectionException $e) {
    catch (ConnectionException $e) {

I am 100% certain that this is due to my lack of experience with try catch. I figured after 3 days of trying it would just be easier to ask what I was doing wrong :)

I am deliberately giving invalid data to write error correction in my application. Invalid IP addresses, wrong community strings, impossible versions, etc.