File Coverage

blib/lib/Interchange/Search/Solr/Response.pm
Criterion Covered Total %
statement 9 31 29.0
branch 0 12 0.0
condition n/a
subroutine 3 7 42.8
pod 4 4 100.0
total 16 54 29.6


line stmt bran cond sub pod time code
1             package Interchange::Search::Solr::Response;
2              
3 14     14   31002 use strict;
  14         38  
  14         412  
4 14     14   80 use warnings;
  14         30  
  14         324  
5 14     14   597 use Moo;
  14         12442  
  14         90  
6             extends 'WebService::Solr::Response';
7              
8             =head1 NAME
9              
10             Interchange::Search::Solr::Response
11              
12             =head2 DESCRIPTION
13              
14             L<WebService::Solr::Response> subclass for error handling.
15              
16             =head2 METHODS/ACCESSORS
17              
18             In addition to all the L<WebService::Solr::Response> methods this
19             class have the following methods:
20              
21             =head3 error
22              
23             An error string.
24              
25             =head3 is_empty_search
26              
27             Error code is C<empty_search>.
28              
29             =cut
30              
31             has error => (is => 'rw');
32              
33             sub is_empty_search {
34 0     0 1   my $self = shift;
35 0 0         if (my $error = $self->error) {
36 0           return $error eq 'empty_search';
37             }
38 0           return 0;
39             }
40              
41             =head3 success
42              
43             Returns 1 if the operation is successful, 0 otherwise.
44              
45             =cut
46              
47             sub success {
48 0     0 1   my $self = shift;
49 0           my $status = $self->solr_status;
50              
51 0 0         unless ( defined $status ) {
52 0           my $http_response = $self->raw_response;
53              
54 0 0         if ( $http_response->code != 200 ) {
55 0           $status = 1;
56             }
57             }
58              
59 0           return ! $status;
60             }
61              
62             =head3 exception_message
63              
64             Checks the response for a Solr exception (e.g undefined field foobar).
65             If found, it returns the exception message. Otherwise it returns
66             the generic HTTP response message.
67              
68             =cut
69              
70             sub exception_message {
71 0     0 1   my $self = shift;
72 0           my $http_response = $self->raw_response;
73              
74 0 0         if ($http_response->code == 400) {
    0          
75             # look at deserialized JSON
76 0           my $content = $self->content;
77              
78 0 0         if (exists $content->{error}->{msg}) {
79 0           return $content->{error}->{msg};
80             }
81             }
82             elsif ( $self->error ) {
83 0           return $self->error;
84             }
85              
86 0           return $http_response->message;
87             }
88              
89             =head3 as_string
90              
91             Returns the string representation of Solr's HTTP response.
92              
93             =cut
94              
95             sub as_string {
96 0     0 1   my $self = shift;
97 0           my $http_response = $self->raw_response;
98              
99 0           return $http_response->as_string;
100             }
101              
102             =head1 AUTHORS
103              
104              
105             Marco Pessotto, C<< <melmothx at gmail.com> >>
106              
107             Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
108              
109              
110             =head1 LICENSE AND COPYRIGHT
111              
112             Copyright 2015-2019 Marco Pessotto, Stefan Hornburg (Racke).
113              
114             This program is free software; you can redistribute it and/or modify it
115             under the terms of either: the GNU General Public License as published
116             by the Free Software Foundation; or the Artistic License.
117              
118             See http://dev.perl.org/licenses/ for more information.
119              
120             =cut
121              
122             1;