summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-01-11 10:52:13 +0100
committerfranck cuny <franck@lumberjaph.net>2010-01-11 10:52:13 +0100
commite6940b213c5e00fb938978a56e862bf1e957da60 (patch)
tree309fc19a1c16a686e4b03665edf17eb343e83107
parentChecking in changes prior to tagging of version 0.09. Changelog diff is: (diff)
downloadmoosex-net-api-e6940b213c5e00fb938978a56e862bf1e957da60.tar.gz
stringify error
-rw-r--r--lib/MooseX/Net/API.pm20
1 files changed, 16 insertions, 4 deletions
diff --git a/lib/MooseX/Net/API.pm b/lib/MooseX/Net/API.pm
index 937c150..f0a03aa 100644
--- a/lib/MooseX/Net/API.pm
+++ b/lib/MooseX/Net/API.pm
@@ -70,7 +70,8 @@ sub net_api_declare {
     }
 
     if ( !$options{format_mode} ) {
-        die MooseX::Net::API::Error->new( 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(
@@ -317,13 +318,14 @@ sub _request {
         $req->content($content);
     }
     else {
-        die MooseX::Net::API::Error->new( reason => "$method is not defined" );
+        die MooseX::Net::API::Error->new(
+            reason => "$method is not defined" );
     }
 
     $req->header( 'Content-Type' => $list_content_type->{$format} )
         if $self->api_format_mode eq 'content-type';
 
-    if ($do_auth || $options->{authentication}) {
+    if ( $do_auth || $options->{authentication} ) {
         if ($auth_method) {
             $req = $self->$auth_method($req);
         }
@@ -346,14 +348,24 @@ sub _do_authentication {
 package MooseX::Net::API::Error;
 
 use Moose;
+use JSON::XS;
+use Moose::Util::TypeConstraints;
 use overload '""' => \&error;
 
+subtype error => as 'Str';
+coerce error => from 'HashRef' => via { encode_json $_};
+
 has http_error => (
     is      => 'ro',
     isa     => 'HTTP::Response',
     handles => { http_message => 'message', http_code => 'code' }
 );
-has reason => ( is => 'ro', isa => 'Str|HashRef', predicate => 'has_reason' );
+has reason => (
+    is        => 'ro',
+    isa       => 'error',
+    predicate => 'has_reason',
+    coerce    => 1
+);
 
 sub error {
     my $self = shift;