Error checking and validation
For many methods and properties of the DevOps Plan Schema API, you must check the return value to validate whether or not the call returns an error.
- For calls to functions that return an object, you
need to check for the condition if the specified object does not exist.
For example, if you call the
Itemmethod of a collection object, if the object that you specify is not in the collection, the return value is:- For Perl, an undefined object. You can use
to detect this condition.if (!defined($result)) { ... };
- For Perl, an undefined object. You can use
- For calls to functions that have an error String return value, the value is empty
if there is no error, or a String containing the description of the error. You can
check the result of calling the method and if the value is not empty, you can
retrieve the error in a variable, as a String value.
For example the Entity object
SetFieldValuemethod is defined as returning a String value. It returns an empty String if changes to the field are permitted and the operation is successful; otherwise, if the operation fails, this method returns a String containing an explanation of the error.To trap the error, your code must check the return value. For example:
my $strRetVal = $entity->SetfieldValue("Invalid_field", "Invalid value"); if ($strRetVal ne "") { # handle the error }If an incorrect field is specified, an error is returned. For example:
The Defect SAMPL00000123 does not have a field named "Invalid_field".
You should also write code to handle potential exception failures.
Trap exceptions by executing API methods within an
eval{} statement
for Perl. For example, # trap exceptions and error message strings
# ...
eval { $RetVal = ${$CQEntity}->Validate(); };
# EXCEPTION information is in $@
# RetVal is either an empty string or contains a failure message string
if ($@){
print "Exception: '$@'\n";
# other exception handling goes here...
}
if ($RetVal eq "")
{# success...
}
else {
# failure...
# return the message string here...
}