1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#!/usr/bin/perl
package JSON::RPC::Common::Procedure::Return;
$JSON::RPC::Common::Procedure::Return::VERSION = '0.11';
use Moose;
# ABSTRACT: JSON-RPC procedure return class
use Carp qw(croak);
use JSON::RPC::Common::TypeConstraints qw(JSONValue);
use JSON::RPC::Common::Procedure::Return::Error;
use namespace::clean -except => [qw(meta)];
with qw(JSON::RPC::Common::Message);
around new_from_data => sub {
my $next = shift;
my ( $class, %args ) = @_;
if ( defined(my $error = delete $args{error}) ) {
$args{error} = $class->inflate_error($error, %args);
}
return $class->$next(%args);
};
has version => (
isa => "Str",
is => "rw",
predicate => "has_version",
);
has result => (
isa => "Any",
is => "rw",
predicate => "has_result",
);
has id => (
isa => JSONValue,
is => "rw",
predicate => "has_id",
);
has error_class => (
isa => "ClassName",
is => "rw",
default => "JSON::RPC::Common::Procedure::Return::Error",
);
has error => (
isa => "JSON::RPC::Common::Procedure::Return::Error",
is => "rw",
predicate => "has_error",
);
sub deflate {
my $self = shift;
my $version = $self->version;
$version = "undefined" unless defined $version;
croak "Deflating a procedure return of the class " . ref($self) . " is not supported (version is $version)";
}
sub deflate_error {
my $self = shift;
if ( my $error = $self->error ) {
return $error->deflate;
} else {
return undef;
}
}
sub inflate_error {
my ( $self, $error ) = @_;
my $error_class = ref $self
? $self->error_class
: $self->meta->find_attribute_by_name("error_class")->default;
$error_class->inflate($error);
}
sub set_error {
my ( $self, @args ) = @_;
$self->error( $self->create_error(@args) );
}
sub create_error {
my ( $self, @args ) = @_;
$self->error_class->new_dwim(@args);
}
__PACKAGE__->meta->make_immutable;
__PACKAGE__
__END__
=pod
=head1 NAME
JSON::RPC::Common::Procedure::Return - JSON-RPC procedure return class
=head1 VERSION
version 0.11
=head1 SYNOPSIS
use JSON::RPC::Common::Procedure::Return;
# create a return from a call, retaining the ID
my $return = $call->return_result("foo");
# inflate gets a version specific class
my $return = JSON::RPC::Common::Procedure::Return->inflate(
version => "2.0",
result => "foo",
id => $id,
);
# you can specify a return with an error, it's just an attribute
my $return = JSON::RPC::Common::Procedure::Return->new(
error => ...,
);
=head1 DESCRIPTION
This class abstracts JSON-RPC procedure returns (results).
Version specific implementation are provided as well.
=head1 ATTRIBUTES
=over 4
=item id
The ID of the call this is a result for.
Results with no ID are typically error results for parse fails, when the call
ID could never be determined.
=item result
The JSON data that is the result of the call, if any.
=item error
The error, if any. This is a L<JSON::RPC::Common::Procedure::Return::Error>
object (or a version specific subclass).
=item error_class
The error class to use when instantiating errors.
=back
=head1 METHODS
=over 4
=item inflate
=item deflate
Go to and from JSON data.
=item inflate_error
=item deflate_error
Helpers for managing the error sub object.
=item set_error
Calls C<create_error> with it's arguments and sets the error to that.
E.g.
$res->set_error("foo");
$res->error->message; # "foo"
=item create_error
Instantiate a new error of class L<error_class> using
L<JSON::RPC::Common::Procedure::Return::Error/new_dwim>.
=back
=head1 AUTHOR
Yuval Kogman <nothingmuch@woobling.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Yuval Kogman and others.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|