Subject: | Does not fail gracefully when bit.ly not accessible |
Use WWW::Shorten::Bitly::makeashorterlink() via an HTTP proxy and if
the proxy cannot reach bit.ly then you get an error.
E.g. write a simple program bitly_test.pl that calls makeashorterlink
with your user and apikey, then run it like
$ http_proxy=http://NOTAREALDOMAIN perl bitly_test.pl
And you get:
garbage after JSON object, at character offset 4 ["Can't connect to
NOT..."] at /home/edwarp11/perl5/lib/perl5/JSON/Any.pm line 491.
In makeashorterlink() after the call
$bitly->{response} = $ua->get($biturl);
it needs to catch an HTTP GET failure.
Patch supplied with fix so it gives a meaningful error:
failed to get bit.ly link: 500 Can't connect to NOTAREALDOMAIN:80 (Bad
hostname 'NOTAREALDOMAIN') at
/home/edwarp11/perl5/lib/perl5/WWW/Shorten/Bitly.pm line 138.
Subject: | patch.txt |
--- /home/edwarp11/perl5/lib/perl5/WWW/Shorten/Bitly.pm 2009-06-22 15:34:19.000000000 +0100
+++ /tmp/Bitly.pm 2009-06-22 15:34:07.000000000 +0100
@@ -135,6 +135,8 @@
$bitly->{xml} = new XML::Simple(SuppressEmpty => 1);
my $biturl = "http://api.bit.ly/shorten?history=1&version=2.0.1&longUrl=" . $url . "&login=" . $user . "&apiKey=" . $apikey;
$bitly->{response} = $ua->get($biturl);
+ $bitly->{response}->is_success
+ || die 'failed to get bit.ly link: '.$bitly->{response}->status_line;
$bitly->{bitlyurl} = $bitly->{json}->jsonToObj($bitly->{response}->{_content})->{results}->{$url}->{shortUrl};
return unless $bitly->{response}->is_success;
return $bitly->{bitlyurl};