| 1 | #!/usr/bin/perl | ||||||||||
| 2 | |||||||||||
| 3 | package JSON::RPC::Common::Procedure::Return; | ||||||||||
| 4 | 6 | $JSON::RPC::Common::Procedure::Return::VERSION = '0.11'; | |||||||||
| 5 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | # ABSTRACT: JSON-RPC procedure return class | ||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 6 | ▶ | use JSON::RPC::Common::TypeConstraints qw(JSONValue); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 6 | ▶ | use JSON::RPC::Common::Procedure::Return::Error; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 6 | ▶ | use namespace::clean -except => [qw(meta)]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 6 | with qw(JSON::RPC::Common::Message); | |||||||||
| 16 | |||||||||||
| 17 | 7 | ▶ | around new_from_data => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 18 | 7 | my $next = shift; | |||||||||
| 19 | 7 | my ( $class, %args ) = @_; | |||||||||
| 20 | |||||||||||
| 21 | 7 | ▶ | if ( defined(my $error = delete $args{error}) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 2 | $args{error} = $class->inflate_error($error, %args); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 7 | return $class->$next(%args); | |||||||||
| 26 | 6 | }; | |||||||||
| 27 | |||||||||||
| 28 | 6 | has version => ( | |||||||||
| 29 | isa => "Str", | ||||||||||
| 30 | is => "rw", | ||||||||||
| 31 | predicate => "has_version", | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | 6 | has result => ( | |||||||||
| 35 | isa => "Any", | ||||||||||
| 36 | is => "rw", | ||||||||||
| 37 | predicate => "has_result", | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | 6 | has id => ( | |||||||||
| 41 | isa => JSONValue, | ||||||||||
| 42 | is => "rw", | ||||||||||
| 43 | predicate => "has_id", | ||||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | 6 | has error_class => ( | |||||||||
| 47 | isa => "ClassName", | ||||||||||
| 48 | is => "rw", | ||||||||||
| 49 | default => "JSON::RPC::Common::Procedure::Return::Error", | ||||||||||
| 50 | ); | ||||||||||
| 51 | |||||||||||
| 52 | 6 | has error => ( | |||||||||
| 53 | isa => "JSON::RPC::Common::Procedure::Return::Error", | ||||||||||
| 54 | is => "rw", | ||||||||||
| 55 | predicate => "has_error", | ||||||||||
| 56 | ); | ||||||||||
| 57 | |||||||||||
| 58 | 0 | ▶ | sub deflate { | ||||||||
|
Subroutine
deflate: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 59 | 0 | my $self = shift; | |||||||||
| 60 | |||||||||||
| 61 | 0 | my $version = $self->version; | |||||||||
| 62 | |||||||||||
| 63 | 0 | ▶ | $version = "undefined" unless defined $version; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | 0 | croak "Deflating a procedure return of the class " . ref($self) . " is not supported (version is $version)"; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 5 | ▶ | sub deflate_error { | ||||||||
|
Subroutine
deflate_error: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 69 | 5 | my $self = shift; | |||||||||
| 70 | |||||||||||
| 71 | 5 | ▶ | if ( my $error = $self->error ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 3 | return $error->deflate; | |||||||||
| 73 | } else { | ||||||||||
| 74 | 2 | return undef; | |||||||||
| 75 | } | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 2 | ▶ | sub inflate_error { | ||||||||
|
Subroutine
inflate_error: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||
| 79 | 2 | my ( $self, $error ) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 2 | ▶ | my $error_class = ref $self | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | ? $self->error_class | ||||||||||
| 83 | : $self->meta->find_attribute_by_name("error_class")->default; | ||||||||||
| 84 | |||||||||||
| 85 | 2 | $error_class->inflate($error); | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | sub set_error { | ||||||||
|
Subroutine
set_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | 1 | my ( $self, @args ) = @_; | |||||||||
| 90 | |||||||||||
| 91 | 1 | $self->error( $self->create_error(@args) ); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 1 | ▶ | sub create_error { | ||||||||
|
Subroutine
create_error: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 95 | 1 | my ( $self, @args ) = @_; | |||||||||
| 96 | 1 | $self->error_class->new_dwim(@args); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||
| 100 | |||||||||||
| 101 | 6 | __PACKAGE__ | |||||||||
| 102 | |||||||||||
| 103 | __END__ | ||||||||||