diff options
author | franck cuny <franck@lumberjaph.net> | 2010-01-04 11:49:54 +0100 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-01-04 11:49:54 +0100 |
commit | 7ed09ce1ba1ec3a16d1670fe2a71e19d4d54a96e (patch) | |
tree | ef6ee9bf2a134c4e37333f20bdbc11a339e60094 | |
parent | start to rewrite tests (diff) | |
download | moosex-net-api-7ed09ce1ba1ec3a16d1670fe2a71e19d4d54a96e.tar.gz |
fix error reporting
-rw-r--r-- | Makefile.PL | 1 | ||||
-rw-r--r-- | lib/MooseX/Net/API.pm | 20 |
2 files changed, 16 insertions, 5 deletions
diff --git a/Makefile.PL b/Makefile.PL index 258c368..f181e8c 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -6,6 +6,7 @@ requires 'Moose'; requires 'Try::Tiny'; requires 'URI'; requires 'HTTP::Request'; +requires 'HTTP::Response'; requires 'LWP::UserAgent'; requires 'MooseX::Types::Moose'; requires 'JSON::XS'; diff --git a/lib/MooseX/Net/API.pm b/lib/MooseX/Net/API.pm index ac5d4cb..63802db 100644 --- a/lib/MooseX/Net/API.pm +++ b/lib/MooseX/Net/API.pm @@ -55,7 +55,7 @@ sub net_api_declare { reason => "format is missing in your api declaration" ); } elsif ( !$list_content_type->{ $options{format} } ) { - die MooseX::Net::API::Error->( + die MooseX::Net::API::Error->new( reason => "format is not recognised. It must be " . join( " or ", keys %$list_content_type ) ); } @@ -70,7 +70,7 @@ sub net_api_declare { } if ( !$options{format_mode} ) { - die MooseX::Net::API::Error->( reason => "format_mode is not set" ); + die MooseX::Net::API::Error->new( reason => "format_mode is not set" ); } elsif ( $options{format_mode} !~ /^(?:append|content\-type)$/ ) { die MooseX::Net::API::Error->new( @@ -92,7 +92,7 @@ sub net_api_declare { else { my $method = $options{useragent}; if ( ref $method ne 'CODE' ) { - die MooseX::Net::API::Error->( + die MooseX::Net::API::Error->new( reason => "useragent must be a CODE ref" ); } else { @@ -273,7 +273,7 @@ sub _add_useragent { if ( !$code ) { try { require LWP::UserAgent; } catch { - MooseX::Net::API::Error->new( reason => + die MooseX::Net::API::Error->new( reason => "no useragent defined and LWP::UserAgent is not available" ); }; @@ -339,12 +339,22 @@ sub _do_authentication { package MooseX::Net::API::Error; use Moose; +use overload '""' => \&error; + has http_error => ( is => 'ro', isa => 'HTTP::Response', handles => { http_message => 'message', http_code => 'code' } ); -has reason => ( is => 'ro', isa => 'Str|HashRef' ); +has reason => ( is => 'ro', isa => 'Str|HashRef', predicate => 'has_reason' ); + +sub error { + my $self = shift; + return + ( $self->has_reason && $self->reason ) + || ( $self->http_message . ": " . $self->http_code ) + || 'unknown'; +} 1; |