Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/JSON/RPC/Dispatch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
30 changes: 26 additions & 4 deletions t/002_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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" );
Expand Down Expand Up @@ -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',
Expand Down
15 changes: 15 additions & 0 deletions t/JSON/RPC/Test/Handler/Sum.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
}
Expand All @@ -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;