Forum Discussion
May 30, 2003
As promised here are some perl subroutines to do the dirty work.
Calling code
...
if ( $soapResponse->fault )
{
&printFault($soapResponse->faultcode, $soapResponse->faultstring);
}
----------------------------------------------------------------------------
Print out a soap fault
----------------------------------------------------------------------------
sub printFault()
{
my ($faultCode, $faultString) = @_;
my ($primary, $secondary, $str) = &getFaultDetails($faultString);
print "Fault Code: $faultCode\n";
print "Fault Details:\n";
print " Primary Error Code: $primary\n";
print " Secondary Error Code: $secondary\n";
print " Error String: $str\n";
}
----------------------------------------------------------------------------
Extract the primary error code, secondary error code,
and error string
----------------------------------------------------------------------------
sub getFaultDetails()
{
my ($faultString) = @_;
my $primary_error_code;
my $secondary_error_code;
my $error_string;
Split details into lines
@lines = split("\n", $faultString);
foreach $line (@lines)
{
split line into name : value pairs and trim strings
@parts = split(":", $line);
$name = &trimString(@parts[0]);
$value = &trimString(@parts[1]);
set values depending on detail element names.
if ( "$name" eq "primary_error_code" )
{
$primary_error_code = $value;
}
elsif ( "$name" eq "secondary_error_code" )
{
$secondary_error_code = $value;
}
elsif ( $name eq "error_string" )
{
$error_string = $value;
}
}
return ($primary_error_code, $secondary_error_code, $error_string);
}
----------------------------------------------------------------------------
Helper to trim leading and tailing whitespace from a string
----------------------------------------------------------------------------
sub trimString()
{
my ($str) = @_;
$str =~ s/^ |\t|\n//;
$str =~ s/\s*$//;
return $str;
}
You should be able to adapt this to suit your needs.
-Joe