Skip to content
Browse files

added better error handling

  • Loading branch information...
1 parent 97f6a63 commit c50049d06214fbdb716a4132b59685395eff738b @barbie barbie committed Apr 9, 2012
Showing with 62 additions and 34 deletions.
  1. +3 −0 Changes
  2. +2 −2 META.json
  3. +2 −2 META.yml
  4. +9 −10 README
  5. +20 −3 lib/CPAN/Testers/WWW/Reports/Query/Reports.pm
  6. +26 −17 t/10query.t
View
3 Changes
@@ -1,5 +1,8 @@
Revision history for CPAN-Testers-WWW-Reports-Query-Reports
+0.05 09/04/2012
+ - better error handling.
+
0.04 06/04/2012
- added ping test & skip if no network.
View
4 META.json
@@ -1,6 +1,6 @@
{
"name": "CPAN-Testers-WWW-Reports-Query-Reports",
- "version": "0.04",
+ "version": "0.05",
"abstract": "Retrieve CPAN Testers metadata direct from the CPAN Testers website",
"author": [
"Barbie (BARBIE) <barbie@cpan.org>"
@@ -44,7 +44,7 @@
"provides": {
"CPAN::Testers::WWW::Reports::Query::Reports": {
"file": "lib/CPAN/Testers/WWW/Reports/Query/Reports.pm",
- "version": "0.04"
+ "version": "0.05"
}
},
"no_index": {
View
4 META.yml
@@ -1,6 +1,6 @@
--- #YAML:1.0
name: CPAN::Testers::WWW::Reports::Query::Reports
-version: 0.04
+version: 0.05
abstract: Retrieve CPAN Testers metadata direct from the CPAN Testers website.
author:
- Barbie <barbie@cpan.org>
@@ -24,7 +24,7 @@ build_requires:
provides:
CPAN::Testers::WWW::Reports::Query::Reports:
file: lib/CPAN/Testers/WWW/Reports/Query/Reports.pm
- version: 0.04
+ version: 0.05
resources:
repository: http://github.com/barbie/cpan-testers-www-reports-query-reports
View
19 README
@@ -1,10 +1,6 @@
-User Contributed Documentation CPAN::Testers::WWW::Reports::Query::Reports(3)
-
-
NAME
- CPAN::Testers::WWW::Reports::Query::Reports - Retrieve CPAN Testers
- metadata direct from the CPAN Testers website.
-
+ CPAN::Testers::WWW::Reports::Query::Reports
+
DESCRIPTION
This module queries the CPAN Testers website and retrieves a data set.
For a date request, the data set returned relates to the ids that can
@@ -47,6 +43,9 @@ SYNOPSIS
# get the raw data for all results, or a specific version if supplied
my $data = $query->raw;
+ # get the last error
+ my $error = $query->error;
+
Caveat
When using the range parameter, note that at most 2500 records will be
returned. This is to avoid accidental request for all the records!
@@ -78,6 +77,10 @@ INTERFACE
Returns the raw content returned from the server.
+ · error
+
+ Returns the last recorded error.
+
BUGS, PATCHES & FIXES
There are no known bugs at the time of this release. However, if you
spot a bug or are experiencing difficulties, that is not explained
@@ -107,7 +110,3 @@ COPYRIGHT AND LICENSE
This module is free software; you can redistribute it and/or
modify it under the Artistic License 2.0.
-
-
-
-perl v5.10.1 CPAN::Testers::WWW::Reports::Query::Reports(3)
View
23 lib/CPAN/Testers/WWW/Reports/Query/Reports.pm
@@ -3,7 +3,7 @@ package CPAN::Testers::WWW::Reports::Query::Reports;
use strict;
use warnings;
-our $VERSION = '0.04';
+our $VERSION = '0.05';
#----------------------------------------------------------------------------
@@ -55,6 +55,9 @@ for that date. A range request will return the records for the requested IDs.
my $data = $query->raw;
+ # get the last error
+ my $error = $query->error;
+
=head2 Caveat
When using the range parameter, note that at most 2500 records will be
@@ -120,12 +123,13 @@ sub range {
sub _request {
my $self = shift;
my $param = shift || return;
+ $self->{error} = '';
my $url = join( '?', $URL, $param );
#print "URL: $url\n";
eval { $mech->get( $url ); };
if($@ || !$mech->success()) {
- die $@;
+ $self->{error} = $@;
return;
}
@@ -134,8 +138,17 @@ sub _request {
sub _parse {
my $self = shift;
+ my $data;
+ eval { $data = decode_json($self->{content}) };
+ return $data unless($@ || !$data);
+
+ $self->{error} = $@ || 'no data returned';
+ return;
+}
- return decode_json($self->{content});
+sub error {
+ my $self = shift;
+ return $self->{error};
}
q("With thanks to the 2012 QA Hackathon");
@@ -178,6 +191,10 @@ For the given range, returns the metadata records stored for those IDs.
Returns the raw content returned from the server.
+=item * error
+
+Returns the last recorded error.
+
=back
=head1 BUGS, PATCHES & FIXES
View
43 t/10query.t
@@ -2,7 +2,7 @@
use strict;
use lib qw(./lib);
-use Test::More tests => 13;
+use Test::More tests => 14;
use CPAN::Testers::WWW::Reports::Query::Reports;
use Data::Dumper;
@@ -20,6 +20,7 @@ my @args = (
},
{
range => '7211',
+ count => 1,
results => { '7211' => {
'version' => '1.25',
'dist' => 'GD',
@@ -86,32 +87,40 @@ my $query = CPAN::Testers::WWW::Reports::Query::Reports->new();
isa_ok($query,'CPAN::Testers::WWW::Reports::Query::Reports','.. got response');
SKIP: {
- skip "Network unavailable", 12 if(pingtest());
+ skip "Network unavailable", 13 if(pingtest());
for my $args (@args) {
if(defined $args->{date}) {
my $data = $query->date( $args->{date} );
- if($args->{results}) {
+ if($data && $args->{results}) {
is($data->{$_},$args->{results}{$_},".. got '$_' in date hash [$args->{date}]") for(keys %{$args->{results}});
+ } elsif($args->{results}) {
+ diag($query->error());
+ ok($query->error);
} else {
is($data, undef,".. got no results, as expected [$args->{date}]");
}
} elsif(defined $args->{range}) {
my $data = $query->range( $args->{range} );
- if($args->{results}) {
- #diag(Dumper( $data ));
- is_deeply($data->{$_},$args->{results}{$_},".. got '$_' in range hash [$args->{range}]")
- for(keys %{$args->{results}});
- }
- my @keys = sort { $a <=> $b } keys %$data;
- if($args->{start}) {
- is($keys[0], $args->{start},".. got start value [$args->{range}]");
- }
- if($args->{stop}) {
- is($keys[-1], $args->{stop},".. got stop value [$args->{range}]");
- }
- if($args->{count}) {
- cmp_ok(scalar @keys, '<=', $args->{count},".. counted number of records [$args->{range}]");
+ if($data) {
+ if($args->{results}) {
+ #diag(Dumper( $data ));
+ is_deeply($data->{$_},$args->{results}{$_},".. got '$_' in range hash [$args->{range}]")
+ for(keys %{$args->{results}});
+ }
+ my @keys = sort { $a <=> $b } keys %$data;
+ if($args->{start}) {
+ is($keys[0], $args->{start},".. got start value [$args->{range}]");
+ }
+ if($args->{stop}) {
+ is($keys[-1], $args->{stop},".. got stop value [$args->{range}]");
+ }
+ if($args->{count}) {
+ cmp_ok(scalar @keys, '<=', $args->{count},".. counted number of records [$args->{range}]");
+ }
+ } else {
+ diag($query->error());
+ ok($query->error);
}
} else {
ok(0,'missing date or range test');

0 comments on commit c50049d

Please sign in to comment.
Something went wrong with that request. Please try again.