Subject: | Error handling in request_rate() - Bug report and fix |
Date: | Sun, 20 Aug 2006 00:20:06 -0700 |
To: | sherzodr [...] cpan.org, bug-Net-UPS [...] rt.cpan.org |
From: | Christian Marcotte <cpan [...] pickledbrain.com> |
Greetings!
Two scenario gave me the same error:
1) Submitting an empty list of packages to shop_for_rates()
2) Submitting an unsupported (or invalid) zip code to shop_for_rates()
(Try zip 96555 which belongs to the Armed Forces)
The system would get a perl runtime error:
"Can't use an undefined value as an ARRAY reference at ../modules/Net/UPS.pm line ..."
I made the fixes to the file UPS.pm (Net-UPS-0.04). See changes below.
Thanks again for a GREAT module!
Cheers!
- Christian
ORIGINAL line 237:
return [sort{$a->total_charges <=>$b->total_charges} @{$self->request_rate($from, $to, $packages,
$args)}];
REPLACED WITH:
# Scoob correction Aug 19th 2006 / cpan@pickledbrain.com
# There was a Perl run time error when no rates were found
# (empty package list, bad zip code etc...)
# request_rate() can now return undef in case of errors.
####
my $services_aref = $self->request_rate($from, $to, $packages, $args);
if (defined $services_aref) {
return [sort{$a->total_charges <=>$b->total_charges} @$services_aref];
} else {
return(undef); # No services were
}
ALSO, I have added the following lines to UPS.pm
near the beginning of request_rate() [line 253 of orig file]:
# Scoob correction Aug 19th 2006 / cpan@pickledbrain.com
# Test for empty packages array - Set error and ret undef if no pkgs
unless (scalar(@$packages)) {
return $self->set_error( "request_rate() was given an empty list of packages!" );
}
If you prefer this format, I have included the diff -c output:
===============================================================================
*** UPS.pm 2006-08-20 00:01:01.000000000 -0700
--- UPS.orig 2006-02-25 17:45:13.000000000 -0800
***************
*** 234,250 ****
$args ||= {};
$args->{mode} = "shop";
$args->{service}||= "GROUND";
!
! # Scoob correction Aug 19th 2006 / cpan@pickledbrain.com
! # There was a Perl run time error when no rates were found (empty pkg list, bad zip etc...)
! # request_rate() can now safely return undef (and set_error()) in case of errors.
! ####
! my $services_aref = $self->request_rate($from, $to, $packages, $args);
! if (defined $services_aref) {
! return [sort{$a->total_charges <=>$b->total_charges} @$services_aref];
! } else {
! return(undef); # No services were
! }
}
--- 234,240 ----
$args ||= {};
$args->{mode} = "shop";
$args->{service}||= "GROUND";
! return [sort{$a->total_charges <=>$b->total_charges} @{$self->request_rate($from, $to,
$packages, $args)}];
}
***************
*** 260,270 ****
ref($args) && (ref $args eq 'HASH')) {
croak "request_rate(): usage error";
}
- # Scoob correction Aug 19th 2006 / cpan@pickledbrain.com
- # Test for empty packages array - Set error and return undef if no packages
- unless (scalar(@$packages)) {
- return $self->set_error( "request_rate() was given an empty list of packages!" );
- }
if ( defined($args->{limit_to}) ) {
unless ( ref($args->{limit_to}) && ref($args->{limit_to}) eq 'ARRAY' ) {
croak "request_rate(): usage error. 'limit_to' should be of type ARRAY";
--- 250,255 ----