szpal
szpal

Reputation: 647

PHP snmp2_set error messages (return values)

In my PHP code i allways prefer the 'native' PHP snmp functions like snmp2_set().

But unfortunately the above function does not provide sufficient feedback.

From the manual:

Return Values:

Returns TRUE on success or FALSE on failure.

In case of unsuccessful execution in my apache2 error.log i see the detailed error/warning messages, like this:

PHP Warning:  snmp2_set(): Error in packet at 'enterprises.9.9.116.1.4.1.1.2.8048': wrongValue (The set value is illegal or unsupported in some way) in /var/www/modules/myfunc.php on line 2238 ...

Perhaps it is possible that a similar message is returned from the snmp2_set() function?

Upvotes: 0

Views: 763

Answers (1)

Aleksander Wons
Aleksander Wons

Reputation: 3967

The is no way to get this message from that function directly because this is how it works in PHP. A lot of methods will only return true/false and then raise a warning with an exact message.

You can still get access to this message by calling error_get_last(). It will return you an array with details about the error (http://php.net/manual/en/function.error-get-last.php).

Having said that, I would recommend to handle all warnings/notices as exceptions by registering own error handler that will convert such warnings/notices to exception which you can later catch in your code:

function myErrorHandler($errno , $errstr){
    throw new MyException($errstr, $errno);
}

set_error_handler("myErrorHandler");

and then:

try {
    snmp2_set(/* ... */);
} catch (MyException $e) {
    $e->getMessage(); // Here you will have your error message
}

Upvotes: 1

Related Questions