Dancer-Plugin-RPC
view release on metacpan or search on metacpan
lib/Dancer/RPCPlugin/DefaultRoute.pm view on Meta::CPAN
package Dancer::RPCPlugin::DefaultRoute;
use Dancer ':syntax';
use Dancer::RPCPlugin::ErrorResponse;
use RPC::XML;
use RPC::XML::ParserFactory;
use Exporter 'import';
our @EXPORT = qw(setup_default_route);
my $parser = RPC::XML::ParserFactory->new();
=head1 NAME
Dancer::RPCPlugin::DefaultRoute - Catch bad-requests and send error-response
=head1 SYNOPSIS
use Dancer::RPCPlugin::DefaultRoute;
setup_default_route();
=head1 DESCRIPTION
Implements default endpoint to generate -32601 'method not found'
or 'path not found' error_response for non existing endpoints
=head2 setup_default_route
Installs a Dancer route-handler for C<< any qr{.+} >> which tries to return an
appropriate error response to the requestor.
=head3 Responses
All responeses will have B<status: 200 OK>
The B<content-type> (and B<body>) of the request determine the error-response:
=over
=item B<text/xml>
If the B<body> is valid XMLRPC, the response is an XMLRPC-fault:
faultCode => -32601
faultString => "Method '%s' not found"
If the B<body> is I<not> valid XMLRPC, the response is an XMLRPC-fault:
faultCode => -32600
faultString => "Invaild xml-rpc. Not configming to spec: $@"
=item B<application/json>
If the B<body> is valid JSONRPC (ie. is has a I<'jsonrpc': '2.0'> field/value),
the response is a JSONRPC-error:
code => -32601
message => "Method '%s' not found"
If the B<body> is I<not> valid JSONRPC, the response is a generic json struct:
'error': {
'code': -32601,
'message': "Method '$request->path' not found"
}
=item B<other/content-type>
Any other content-type is outside the scope of the service. We can respond in
any way we like. For the moment it will be:
status(404)
content_type('text/plain')
body => "Error! '$request->path' was not found for '$request->content_type'"
=back
=cut
sub setup_default_route {
any qr{.+} => sub {
my $content_type = request->content_type =~ m{(?<ct> [\w-]+ / [\w-]+ )}x
? $+{ct}
: 'text/plain';
( run in 0.500 second using v1.01-cache-2.11-cpan-e93a5daba3e )