Skip to content

Commit

Permalink
Better diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg authored and Dave Cross committed Dec 15, 2015
1 parent 35e5185 commit 85aa0ff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
18 changes: 17 additions & 1 deletion lib/WWW/Shorten/TinyURL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,30 @@ sub makealongerlink
{
my $tinyurl_url = shift
or croak 'No TinyURL key / URL passed to makealongerlink';
$_error_message = '';
my $ua = __PACKAGE__->ua();

$tinyurl_url = "http://tinyurl.com/$tinyurl_url"
unless $tinyurl_url =~ m!^http://!i;

my $resp = $ua->get($tinyurl_url);

return undef unless $resp->is_redirect;
unless ($resp->is_redirect) {
my $content = $resp->content;
if ($content =~ /Error/) {
if ($content =~ /<html/) {
$_error_message = 'Error is a html page';
} elsif (length($content) > 100) {
$_error_message = substr($content, 0, 100);
} else {
$_error_message = $content;
}
} else {
$_error_message = 'Unknown error';
}

return undef;
}
my $url = $resp->header('Location');
return $url;

Expand Down
6 changes: 4 additions & 2 deletions t/tinyurl.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ is ( makeashorterlink($url), $prefix.$code, 'make it shorter');
# Slight pause to increase the chance that all of TinyURL's servers
# know about the new link
sleep(5);
is ( makealongerlink($prefix.$code), $url, 'make it longer');
is ( makealongerlink($code), $url, 'make it longer by Id',);
is ( makealongerlink($prefix.$code), $url, 'make it longer')
or diag "\$_error_message = $_error_message";
is ( makealongerlink($code), $url, 'make it longer by Id',)
or diag "\$_error_message = $_error_message";

eval { makeashorterlink() };
ok($@, 'makeashorterlink fails with no args');
Expand Down

0 comments on commit 85aa0ff

Please sign in to comment.