Skip to content

Commit

Permalink
Check for 404s when downloading things
Browse files Browse the repository at this point in the history
Fixes #66
  • Loading branch information
doherty committed May 7, 2011
1 parent 78cfdf4 commit 5de299c
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/App/perlbrew.pm
Expand Up @@ -162,7 +162,8 @@ sub uniq(@) {

if (! @command) {
my @commands = (
[qw( curl --silent --location )],
# curl's --fail option makes the exit code meaningful
[qw( curl --silent --location --fail )],
[qw( wget --no-check-certificate --quiet -O - )],
);
for my $command (@commands) {
Expand All @@ -178,9 +179,16 @@ sub uniq(@) {

open my $fh, '-|', @command, $url
or die "open() for '@command $url': $!";

local $/;
my $body = <$fh>;
close $fh;
die 'Page not retrieved; HTTP error code 400 or above.'
if $command[0] eq 'curl' # Exit code is 22 on 404s etc
and $? >> 8 == 22; # exit code is packed into $?; see perlvar
die 'Server issued an error response.'
if $command[0] eq 'wget' # Exit code is 8 on 404s etc
and $? >> 8 == 8;

return $cb ? $cb->($body) : $body;
}
Expand Down

0 comments on commit 5de299c

Please sign in to comment.