Error Handling

 

If any request fails during SNMP communications, the response for that request is null. In such cases the error message are set.

 

The getErrorString() and getErrorCode() methods are provided in the SnmpTarget, SnmpRequestServer, SnmpTable, and SnmpPoller classes for handling the error messages. The getErrorString() method is used to get the error string corresponding to the error and getErrorCode() is used to get the error code corresponding to the error string. The getErrstat() method returns the standard SNMP error codes. The following are the error codes and their corresponding error status. The getError() method returns the error string corresponding to the error code present in the SNMP packet.

 

Error Code Error Status

0

noError

1

tooBig

2

noSuchName

3

badValue

4

readOnly

5

genErr

6

noAccess

7

wrongType

8

wrongLength

10

wrongValue

11

noCreation

12

inconsistentValue

13

resourceUnavailable

14

commitFailed

15

undoFailed

16

authorizationError

17

notWritable

18

inconsistentName

 

While sending a synchronous request using SnmpTarget Bean, an error that has occurred can be found by checking the response value. In case of timeouts or other conditions, the response for the request is null and the error messages are set. In such cases, the error string and the error code can be obtained.

 

SnmpTarget target=new SnmpTarget();

target.setTargetHost("localhost");

target.setObjectID("1.5.0");

String result=target.snmpGet();

if( result != null )

System.out.println("Result :"+result);

else

System.out.println("Error :"+target.getErrorString());

 

While sending an asynchronous request using SnmpRequestServer, the timeout events are not sent to the registered listeners unless the setSendTimeoutEvents(true) method is called. In SnmpRequestServer, the error string is set in the response-PDU. The error conditions can be handled as explained below.

 

public void setResult(com.adventnet.snmp.beans.ResultEvent e) {

SnmpPDU pdu=(SnmpPDU)e.getResponse();

if(pdu!=null){

System.out.println(pdu.printVarBinds());

}

else{

System.out.println(e.getErrorString());

}

}

 

The getErrindex() method returns the index at which the error has occurred. This index starts from one. All the error codes that are listed in the table are standard SNMP error codes. These error codes are defined in RFC1157 and RFC1905.

 

User-defined Error Codes

 

Error codes between 1 and 18 are reserved for standard errors. Besides the standard error codes listed in the above table, SNMP API supports user-defined error codes. The addUserError(int code, String error) method is used to add user-defined error codes and the corresponding error strings. If the specified error code already exists, this method overwrites the previous string with the recent one provided. In case of user-defined error codes, the getErrorCode() method returns -2.

 

The getErrorString(int) gets the error string corresponding to the specified error code. If the user-defined error string is not initialized, AdventNet-specific error is returned. If there is no AdventNet-specific error associated with the specified error code, null is returned. These methods are provided in ErrorMessages, SnmpTarget, and SnmpRequestServer classes.

 



Copyright © 1996-2006, AdventNet Inc. All Rights Reserved.