Skip to content

Commit

Permalink
Change croak to carp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nigel Horne committed Jan 18, 2018
1 parent e2c2240 commit 607ba63
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Revision history for Geo-Coder-XYZ
Give better error message on failure
Only give 'moreinfo' parameter when needed
return failure after croak
Change croak to carp

0.06 Mon Jul 24 08:31:32 EDT 2017
Fix RT#122441
Expand Down
27 changes: 19 additions & 8 deletions lib/Geo/Coder/XYZ.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ sub new {
@locations = $geocoder->geocode('Portland, USA');
diag 'There are Portlands in ', join (', ', map { $_->{'state'} } @locations);
=cut

sub geocode {
Expand All @@ -80,7 +80,7 @@ sub geocode {
}

my $location = $param{location}
or Carp::croak("Usage: geocode(location => \$location)");
or Carp::carp("Usage: geocode(location => \$location)");

if (Encode::is_utf8($location)) {
$location = Encode::encode_utf8($location);
Expand All @@ -102,12 +102,23 @@ sub geocode {
my $res = $self->{ua}->get($url);

if ($res->is_error) {
Carp::croak("geocode.xyz API returned error: on $url " . $res->status_line());
return 0;
Carp::carp("geocode.xyz API returned error: on $url " . $res->status_line());
return { };
}

my $json = JSON->new->utf8;
my $rc = $json->decode($res->content);
my $json = JSON->new()->utf8();
my $rc;
eval {
$rc = $json->decode($res->content());
};
if(!defined($rc)) {
if($@) {
Carp::carp("$url: $@");
return { };
}
Carp::carp("$url: can't decode the JSON ", $res->content());
return { };
}

if($rc->{'otherlocations'} && $rc->{'otherlocations'}->{'loc'} &&
(ref($rc->{'otherlocations'}->{'loc'}) eq 'ARRAY')) {
Expand Down Expand Up @@ -165,10 +176,10 @@ sub reverse_geocode {
}

my $latlng = $param{latlng}
or Carp::croak("Usage: reverse_geocode(latlng => \$latlng)");
or Carp::carp("Usage: reverse_geocode(latlng => \$latlng)");

return $self->geocode(location => $latlng, reverse => 1);
};
}

=head1 AUTHOR
Expand Down
4 changes: 2 additions & 2 deletions t/uk.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ UK: {
Test::LWP::UserAgent->import();

require Test::Carp;
Test::Carp->import();;
Test::Carp->import();

eval {
require Test::Number::Delta;
Expand Down Expand Up @@ -50,7 +50,7 @@ UK: {
delta_within($location->{longt}, -0.13, 1e-2);

my $address = $geocoder->reverse_geocode(latlng => '51.50,-0.13');
like($address->{'city'}, qr/^London$/i, 'test reverse');
is($address->{'city'}, 'London', 'test reverse');

my $ua = new_ok('Test::LWP::UserAgent');
$ua->map_response('geocode.xyz', new_ok('HTTP::Response' => [ '500' ]));
Expand Down

0 comments on commit 607ba63

Please sign in to comment.