diff --git a/lib/JSON/RPC/Dispatch.pm b/lib/JSON/RPC/Dispatch.pm index 60e88f3..65e8bee 100644 --- a/lib/JSON/RPC/Dispatch.pm +++ b/lib/JSON/RPC/Dispatch.pm @@ -214,8 +214,9 @@ sub handle_psgi { if (!$is_notification) { my $error = {code => RPC_INTERNAL_ERROR} ; if (ref $e eq "HASH") { - $error->{message} = $e->{message}, - $error->{data} = $e->{data}, + $error->{message} = $e->{message}; + $error->{data} = $e->{data} if defined $e->{data}; + $error->{code} = $e->{code} if defined $e->{code}; } else { $error->{message} = $e, } diff --git a/t/002_basic.t b/t/002_basic.t index fe46b62..d52a678 100644 --- a/t/002_basic.t +++ b/t/002_basic.t @@ -9,7 +9,7 @@ BEGIN { use_ok "JSON::RPC::Dispatch"; use_ok "JSON::RPC::Constants", ':all'; use_ok "JSON::RPC::Test"; - use_ok "t::JSON::RPC::Test::Handler::Sum"; + use_ok "t::JSON::RPC::Test::Handler::Sum", qw( CUSTOM_ERROR_CODE ); } subtest 'defaults' => sub { @@ -42,6 +42,10 @@ subtest 'normal dispatch' => sub { handler => "Sum", action => "tidy_error", } ); + $router->connect( custom_error => { + handler => "Sum", + action => "custom_error", + } ); $router->connect( 'sum_obj' => { handler => t::JSON::RPC::Test::Handler::Sum->new, @@ -244,7 +248,7 @@ subtest 'normal dispatch' => sub { subtest 'JSONRPC via GET' => sub { $request_get->($cb) }; subtest 'JSONRPC via POST' => sub { $request_post->($cb) }; subtest 'JSONRPC via POST (Batch)' => sub { $request_post_batch->($cb) }; - subtest 'JSONRPC Error' => sub { + subtest 'JSONRPC Error' => sub { my ($post_content, $req, $res, $json); my $headers = HTTP::Headers->new( Content_Type => 'application/json',); my $uri = URI->new( "http://localhost" ); @@ -299,8 +303,26 @@ subtest 'normal dispatch' => sub { } is $json->{error}->{message}, 'short description of the error'; is $json->{error}->{data}, 'additional information about the error'; + + $id = time(); + $post_content = $coder->encode( + { + jsonrpc => '2.0', + id => $id, + method => 'custom_error', + params => "foo", + } + ); + $req = HTTP::Request->new( POST => $uri, $headers, $post_content); + $res = $cb->($req); + $json = $coder->decode( $res->decoded_content ); + if (! is $json->{error}->{code}, CUSTOM_ERROR_CODE, "error code is CUSTOM_ERROR_CODE") { + diag explain $json; + } + is $json->{error}->{message}, 'short description of the error', "error message matches"; + is_deeply $json->{error}->{data}, { some => 'data' }, "error data matches"; }; - subtest 'JSONRPC Notification handling' => sub { + subtest 'JSONRPC Notification handling' => sub { my ($post_content, $req, $res, $json); my $headers = HTTP::Headers->new( Content_Type => 'application/json',); my $uri = URI->new( "http://localhost" ); @@ -341,7 +363,7 @@ subtest 'normal dispatch' => sub { is scalar @$json, 1, "Notification and a NULL id call: response has one element"; ok (exists $json->[0]->{id} && !defined $json->[0]->{id}, "Notification and a NULL id call: response element has NULL id"); ok ($json->[0]->{error}, "Notification and a NULL id call: response element has an error"); - + $post_content = $coder->encode( [ { jsonrpc => '2.0', diff --git a/t/JSON/RPC/Test/Handler/Sum.pm b/t/JSON/RPC/Test/Handler/Sum.pm index f26dec5..640f929 100644 --- a/t/JSON/RPC/Test/Handler/Sum.pm +++ b/t/JSON/RPC/Test/Handler/Sum.pm @@ -2,6 +2,11 @@ package t::JSON::RPC::Test::Handler::Sum; use strict; use Class::Accessor::Lite new => 1; +use base 'Exporter'; + +our @EXPORT_OK = qw( CUSTOM_ERROR_CODE ); +use constant CUSTOM_ERROR_CODE => -32000; + sub blowup { die "I blew up!"; } @@ -24,4 +29,14 @@ sub tidy_error { }; } +sub custom_error { + die { + code => CUSTOM_ERROR_CODE, + message => "short description of the error", + data => { + some => 'data' + } + }; +} + 1;