All the functions and methods of objects in the API modules throw errors by using the Perl die command. In order to catch any errors that may occur, the eval() function can be used and the “$@” variable inspected after the event. This is common Perl scripting practice. The error message is rendered in the locale set by the client session.
The example shown in the following script will abort if the router “gw1” does not exist in the topology at the line where the name of the vendor is queried, and the last line will not be executed.
use InCharge::session; $session = InCharge::session->init(); $vendor = $session ->object( "Router::gw1" ) ->get("Vendor”); print "Vendor is $vendor\n”;
To trap this possible error, the code can be modified as follows.
use InCharge::session; $session = InCharge::session->init(); $vendor = eval{ $session ->object( "Router::gw1" ) ->get(Vendor); }; if ( $@ ) { print "Error obtaining the Vendor property\n”; } else { print "Vendor is $vendor\n”; }
For more details about using this mechanism, refer to the section on the eval and die functions in the Perl function man pages.
All error messages thrown by the API start with a number in square brackets. This is the error code and classifies the error as being one of those listed in GUID-F210296A-C7B4-440A-A5B4-25E384F83CC7.html#GUID-F210296A-C7B4-440A-A5B4-25E384F83CC7___PAPI_PRIMITIVES_40052. The remainder of the error text gives a verbose description of the specific error that was thrown. Where additional numeric codes are relevant, these are included in a second or subsequent set of square brackets.
The following example script attempts a connection with a Domain Manager and prompts for a username and password if the connection fails due to an authentication error: code 4.
my $domain = "SAM1"; my $user = undef; my $passwd = undef; for ( ; ; ) { $session = eval{ InCharge::session->new( domain => $domain, username => $user, password => $passwd); } if ( $@ =~ m/^\[4\]/ ) { print "Login: "; chomp $user = <STDIN>; print "Password: "; chomp $passwd = <STDIN>; } elsif ( $@ ) { die $@; # Some other fault } else { last; # Success ! } }