Elasticsearch/Role/Cxn.pm

filter: Statementstmt 77.1% 182 / 236 Branchbran 50.0% 46 / 92 Conditioncond 69.8% 37 / 53 MC/DCmcdc 43.3% 13 / 30 Subroutinesub 91.6% 33 / 36 Podpod 75.0% 9 / 12 totaltotal 69.7% 320 / 459 CC 69 SCAR 30.4
CC
69
Cov
70%
_munge_elasticsearch_exception
60.1
_compress_body
30.0
_decompress_body
27.5
CRAP 21.0
« Elasticsearch/Role/API.pm ↑ Summary Elasticsearch/Client/6_0/Bulk.pm »
1 package Search::Elasticsearch::Role::Cxn;
2 55 $Search::Elasticsearch::Role::Cxn::VERSION = '6.80';
3 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 55 ▶use Search::Elasticsearch::Util qw(parse_params throw to_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 55 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 55 ▶use URI();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 55 ▶use IO::Compress::Deflate();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 55 ▶use IO::Uncompress::Inflate();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 ▶use IO::Compress::Gzip();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 55 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 55 ▶use Search::Elasticsearch::Util qw(to_list);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 55 ▶use Net::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 55 requires qw(perform_request error_from_text handle);
17  
18 55 has 'host' => ( is => 'ro', required => 1 );
19 55 has 'port' => ( is => 'ro', required => 1 );
20 55 has 'uri' => ( is => 'ro', required => 1 );
21 55 has 'request_timeout' => ( is => 'ro', default => 30 );
22 55 has 'ping_timeout' => ( is => 'ro', default => 2 );
23 55 has 'sniff_timeout' => ( is => 'ro', default => 1 );
24 55 has 'sniff_request_timeout' => ( is => 'ro', default => 2 );
25 55 has 'next_ping' => ( is => 'rw', default => 0 );
26 55 has 'ping_failures' => ( is => 'rw', default => 0 );
27 55 has 'dead_timeout' => ( is => 'ro', default => 60 );
28 55 has 'max_dead_timeout' => ( is => 'ro', default => 3600 );
29 55 has 'serializer' => ( is => 'ro', required => 1 );
30 55 has 'logger' => ( is => 'ro', required => 1 );
31 175 ▶has 'handle_args' => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
32 172 ▶has 'default_qs_params' => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33 55 has 'scheme' => ( is => 'ro' );
34 55 has 'is_https' => ( is => 'ro' );
35 55 has 'userinfo' => ( is => 'ro' );
36 55 has 'max_content_length' => ( is => 'ro' );
37 55 has 'default_headers' => ( is => 'ro' );
38 55 has 'deflate' => ( is => 'ro' );
39 55 has 'gzip' => ( is => 'ro' );
40 55 has 'ssl_options' => ( is => 'ro', predicate => 'has_ssl_options' );
41 55 has 'handle' => ( is => 'lazy', clearer => 1 );
42 55 has '_pid' => ( is => 'rw', default => $$ );
43  
44 55 my %Code_To_Error = (
45 400 => 'Request',
46 401 => 'Unauthorized',
47 403 => 'Forbidden',
48 404 => 'Missing',
49 408 => 'RequestTimeout',
50 409 => 'Conflict',
51 413 => 'ContentLength',
52 502 => 'BadGateway',
53 503 => 'Unavailable',
54 504 => 'GatewayTimeout'
55 );
56  
57 #===================================
58 611 ▶sub stringify { shift->uri . '' }
Subroutine stringify: called CC=1 SCAR=0.0
Pod undocumented
59 #===================================
60  
61 #===================================
62 sub get_user_agent {
63 #===================================
64 176 ▶ return sprintf("elasticsearch-perl/%s (%s; perl %s)", $Search::Elasticsearch::VERSION, $^O, $]);
Subroutine get_user_agent: called CC=1 SCAR=0.0
Pod undocumented
65 }
66  
67 #===================================
68 sub BUILDARGS {
69 #===================================
70 175 ▶ my ( $class, $params ) = parse_params(@_);
Subroutine BUILDARGS: called CC=23 SCAR=31.6
Pod undocumented
71  
72 my $node = $params->{node}
73 175 ▶ || { host => 'localhost', port => '9200' };
Condition50%1/2
$params->{'node'} || {'host', 'localhost', 'port', '9200'}
l!l
1750
MC/DC0%0/1
$params->{'node'} || {'host', 'localhost', 'port', '9200'}
$params->{'node'}
Truth table50%1/2
A
A= $params->{'node'}
Aresult
11
00
74  
75 175 ▶ unless ( ref $node eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node eq "HASH") 170 5 175
76 170 ▶ $node = "[$node]" if Net::IP::ip_is_ipv6($node);
Branch: 1 branch
BranchTrueFalseTotal
if Net::IP::ip_is_ipv6($node) 2 168 170
77 170 ▶ unless ( $node =~ m{^http(s)?://} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node =~ m[^http(s)?://]) 124 46 170
78 124 ▶ $node = ( $params->{use_https} ? 'https://' : 'http://' ) . $node;
Branch: 1 branch
BranchTrueFalseTotal
$params->{'use_https'} ? : 1 123 124
79 }
80 170 ▶ if ( $params->{port} && $node !~ m{//[^/\[]+:\d+} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"port"} and not $node =~ m(//[^/\[]+:\d+)) 3 167 170
Condition100%3/3
$params->{"port"} and not $node =~ m(//[^/\[]+:\d+)
!ll&&!rl&&r
16613
MC/DC100%2/2
$params->{"port"} and not $node =~ m(//[^/\[]+:\d+)
$params->{"port"} not $node =~ m(//[^/\[]+:\d+)
Truth table100%3/3
A and not(B)
A= $params->{"port"}B= not $node =~ m(//[^/\[]+:\d+)
ABresult
0X0
100
111
81 3 $node =~ s{(//[^/]+)}{$1:$params->{port}};
82 }
83 170 my $uri = URI->new($node);
84 170 $node = {
85 scheme => $uri->scheme,
86 host => $uri->host,
87 port => $uri->port,
88 path => $uri->path,
89 userinfo => $uri->userinfo
90 };
91 }
92  
93 175 ▶ my $host = $node->{host} || 'localhost';
Condition100%2/2
$node->{'host'} || 'localhost'
l!l
1732
MC/DC100%1/1
$node->{'host'} || 'localhost'
$node->{'host'}
Truth table100%2/2
A
A= $node->{'host'}
Aresult
11
00
94 175 ▶ my $userinfo = $node->{userinfo} || $params->{userinfo} || '';
Condition100%5/5
$node->{'userinfo'} || $params->{'userinfo'} || ''
l!l
3172
$node->{"userinfo"} or $params->{"userinfo"}
l!l&&r!l&&!r
21172
MC/DC100%2/2
$node->{'userinfo'} || $params->{'userinfo'} || ''
$node->{"userinfo"} $params->{"userinfo"}
Truth table100%3/3
A or B
A= $node->{"userinfo"}B= $params->{"userinfo"}
ABresult
1X1
011
000
95 my $scheme
96 175 ▶ = $node->{scheme} || ( $params->{use_https} ? 'https' : 'http' );
Branch: 1 branch
BranchTrueFalseTotal
$params->{'use_https'} ? : 0 5 5
Condition66%2/3
$node->{'scheme'} || ($params->{'use_https'} ? 'https' : 'http')
l!l&&r!l&&!r
17050
MC/DC0%0/2
$node->{'scheme'} || ($params->{'use_https'} ? 'https' : 'http')
$node->{'scheme'} ($params->{'use_https'} ? 'https' : 'http')
Truth table66%2/3
A || B
A= $node->{'scheme'}B= ($params->{'use_https'} ? 'https' : 'http')
ABresult
1X1
011
000
97 my $port
98 = $node->{port}
99 || $params->{port}
100 175 ▶ || ( $scheme eq 'http' ? 80 : 443 );
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'http' ? : 4 0 4
Condition66%4/6
$node->{'port'} || $params->{'port'} || ($scheme eq 'http' ? 80 : 443)
l!l&&r!l&&!r
17140
$node->{"port"} or $params->{"port"}
l!l&&r!l&&!r
17104
MC/DC0%0/3
$node->{'port'} || $params->{'port'} || ($scheme eq 'http' ? 80 : 443)
$node->{"port"} $params->{"port"} ($scheme eq 'http' ? 80 : 443)
Truth table50%2/4
A or B || C
A= $node->{"port"}B= $params->{"port"}C= ($scheme eq 'http' ? 80 : 443)
ABCresult
1XX1
01X1
0011
0000
101 175 ▶ my $path = $node->{path} || $params->{path_prefix} || '';
Condition100%5/5
$node->{'path'} || $params->{'path_prefix'} || ''
l!l
10165
$node->{"path"} or $params->{"path_prefix"}
l!l&&r!l&&!r
91165
MC/DC100%2/2
$node->{'path'} || $params->{'path_prefix'} || ''
$node->{"path"} $params->{"path_prefix"}
Truth table100%3/3
A or B
A= $node->{"path"}B= $params->{"path_prefix"}
ABresult
1X1
011
000
102 175 $path =~ s{^/?}{/}g;
103 175 $path =~ s{/+$}{};
104  
105 175 ▶ my %default_headers = %{ $params->{default_headers} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$params->{"default_headers"} or {} 175 0 175
106  
107 175 ▶ if ($userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo) 3 172 175
108 3 require MIME::Base64;
109 3 my $auth = MIME::Base64::encode_base64( $userinfo, "" );
110 3 chomp $auth;
111 3 $default_headers{Authorization} = "Basic $auth";
112 }
113  
114 175 ▶ if ( $params->{gzip} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'gzip'}) { } 0 175 175
elsif ($params->{'deflate'}) { } 1 174 175
115 0 $default_headers{'Accept-Encoding'} = "gzip";
116 }
117  
118 elsif ( $params->{deflate} ) {
119 1 $default_headers{'Accept-Encoding'} = "deflate";
120 }
121  
122 175 $default_headers{'User-Agent'} = $class->get_user_agent();
123  
124 175 $params->{scheme} = $scheme;
125 175 $params->{is_https} = $scheme eq 'https';
126 175 $params->{host} = $host;
127 175 $params->{port} = $port;
128 175 $params->{path} = $path;
129 175 $params->{userinfo} = $userinfo;
130 175 ▶ $host = "[$host]" if Net::IP::ip_is_ipv6($host);
Branch: 1 branch
BranchTrueFalseTotal
if Net::IP::ip_is_ipv6($host) 6 169 175
131 175 $params->{uri} = URI->new("$scheme://$host:$port$path");
132 175 $params->{default_headers} = \%default_headers;
133  
134 175 return $params;
135 }
136  
137 #===================================
138 before 'handle' => sub {
139 #===================================
140 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
141 0 ▶ if ( $$ != $self->_pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ != $self->_pid) 0 0 0
142 0 $self->clear_handle;
143 0 $self->_pid($$);
144 }
145 55 };
146  
147 #===================================
148 182 ▶sub is_live { !shift->next_ping }
Subroutine is_live: called CC=1 SCAR=0.0
Pod documented
149 128 ▶sub is_dead { !!shift->next_ping }
Subroutine is_dead: called CC=1 SCAR=0.0
Pod documented
150 #===================================
151  
152 #===================================
153 sub mark_live {
154 #===================================
155 149 ▶ my $self = shift;
Subroutine mark_live: called CC=1 SCAR=0.0
Pod documented
156 149 $self->ping_failures(0);
157 149 $self->next_ping(0);
158 }
159  
160 #===================================
161 sub mark_dead {
162 #===================================
163 143 ▶ my $self = shift;
Subroutine mark_dead: called CC=1 SCAR=0.0
Pod documented
164 143 my $fails = $self->ping_failures;
165 143 $self->ping_failures( $fails + 1 );
166  
167 143 my $timeout
168 = min( $self->dead_timeout * 2**$fails, $self->max_dead_timeout );
169 143 my $next = $self->next_ping( time() + $timeout );
170  
171 143 $self->logger->infof( 'Marking [%s] as dead. Next ping at: %s',
172 $self->stringify, scalar localtime($next) );
173  
174 }
175  
176 #===================================
177 sub force_ping {
178 #===================================
179 121 ▶ my $self = shift;
Subroutine force_ping: called CC=1 SCAR=0.0
Pod documented
180 121 $self->ping_failures(0);
181 121 $self->next_ping(-1);
182 }
183  
184 #===================================
185 sub pings_ok {
186 #===================================
187 47 ▶ my $self = shift;
Subroutine pings_ok: called CC=1 SCAR=0.0
Pod documented
188 47 $self->logger->infof( 'Pinging [%s]', $self->stringify );
189 return try {
190 47 ▶ $self->perform_request(
Subroutine __ANON__: called CC=1 SCAR=0.0
191 { method => 'HEAD',
192 path => '/',
193 timeout => $self->ping_timeout,
194 }
195 );
196 33 $self->logger->infof( 'Marking [%s] as live', $self->stringify );
197 33 $self->mark_live;
198 33 1;
199 }
200 catch {
201 14 ▶ $self->logger->debug("$_");
Subroutine __ANON__: called CC=1 SCAR=0.0
202 14 $self->mark_dead;
203 14 0;
204 47 };
205 }
206  
207 #===================================
208 sub sniff {
209 #===================================
210 38 ▶ my $self = shift;
Subroutine sniff: called CC=1 SCAR=0.0
Pod documented
211 38 $self->logger->infof( 'Sniffing [%s]', $self->stringify );
212 return try {
213 $self->perform_request(
214 { method => 'GET',
215 path => '/_nodes/http',
216 qs => { timeout => $self->sniff_timeout . 's' },
217 timeout => $self->sniff_request_timeout,
218 }
219 38 ▶ )->{nodes};
Subroutine __ANON__: called CC=1 SCAR=0.0
220 }
221 catch {
222 13 ▶ $self->logger->debug($_);
Subroutine __ANON__: called CC=1 SCAR=0.0
223 13 return;
224 38 };
225 }
226  
227 #===================================
228 sub build_uri {
229 #===================================
230 5 ▶ my ( $self, $params ) = @_;
Subroutine build_uri: called CC=2 SCAR=6.9
Pod documented
231 5 my $uri = $self->uri->clone;
232 5 $uri->path( $uri->path . $params->{path} );
233 5 ▶ my %qs = ( %{ $self->default_qs_params }, %{ $params->{qs} || {} } );
Branch: 1 branch
BranchTrueFalseTotal
$params->{"qs"} or {} 3 2 5
234 5 $uri->query_form( \%qs );
235 5 return $uri;
236 }
237  
238 #===================================
239 before 'perform_request' => sub {
240 #===================================
241 233 ▶ my ( $self, $params ) = @_;
Subroutine __ANON__: called CC=4 SCAR=23.7
242 233 ▶ return unless defined $params->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"data"} 233 0 233
243  
244 0 $self->_compress_body($params);
245  
246 0 ▶ my $max = $self->max_content_length
Branch: 1 branch
BranchTrueFalseTotal
my $max = $self->max_content_length or return 0 0 0
247 or return;
248  
249 0 ▶ return if length( $params->{data} ) < $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $params->{"data"} < $max 0 0 0
250  
251 0 $self->logger->throw_error( 'ContentLength',
252 "Body is longer than max_content_length ($max)",
253 );
254 55 };
255  
256 #===================================
257 sub _compress_body {
258 #===================================
259 0 ▶ my ( $self, $params ) = @_;
Subroutine _compress_body: not called CC=5 SCAR=34.0
260 0 my $output;
261 0 ▶ if ( $self->gzip ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->gzip) { } 0 0 0
elsif ($self->deflate) { } 0 0 0
262 0 ▶ IO::Compress::Gzip::gzip( \( $params->{data} ), \$output )
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Gzip::gzip(\$params->{"data"}, \$output) or throw("Request", "Couldn't gzip request: $IO::Compress::Gzip::GzipError") 0 0 0
263 or throw( 'Request',
264 "Couldn't gzip request: $IO::Compress::Gzip::GzipError" );
265 0 $params->{data} = $output;
266 0 $params->{encoding} = 'gzip';
267 }
268 elsif ( $self->deflate ) {
269 0 ▶ IO::Compress::Deflate::deflate( \( $params->{data} ), \$output )
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Deflate::deflate(\$params->{"data"}, \$output) or throw("Request", "Couldn't deflate request: $IO::Compress::Deflate::DeflateError") 0 0 0
270 or throw( 'Request',
271 "Couldn't deflate request: $IO::Compress::Deflate::DeflateError" );
272 0 $params->{data} = $output;
273 0 $params->{encoding} = 'deflate';
274 }
275 }
276  
277 #===================================
278 sub _decompress_body {
279 #===================================
280 238 ▶ my ( $self, $body_ref, $headers ) = @_;
Subroutine _decompress_body: called CC=6 SCAR=33.1
281 238 ▶ if ( my $encoding = $headers->{'content-encoding'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $headers->{"content-encoding"}) 0 238 238
282 0 my $output;
283 0 ▶ if ( $encoding eq 'gzip' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($encoding eq 'gzip') { } 0 0 0
elsif ($encoding eq 'deflate') { } 0 0 0
284 0 ▶ IO::Uncompress::Gunzip::gunzip( $body_ref, \$output )
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Gunzip::gunzip($body_ref, \$output) or throw("Request", "Couldn't gunzip response: $IO::Uncompress::Gunzip::GunzipError") 0 0 0
285 or throw(
286 'Request',
287 "Couldn't gunzip response: $IO::Uncompress::Gunzip::GunzipError"
288 );
289 }
290 elsif ( $encoding eq 'deflate' ) {
291 0 ▶ IO::Uncompress::Inflate::inflate( $body_ref, \$output,
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Inflate::inflate($body_ref, \$output, "Transparent", 0) or throw("Request", "Couldn't inflate response: $IO::Uncompress::Inflate::InflateError") 0 0 0
292 Transparent => 0 )
293 or throw(
294 'Request',
295 "Couldn't inflate response: $IO::Uncompress::Inflate::InflateError"
296 );
297 }
298 else {
299 0 throw( 'Request', "Unknown content-encoding: $encoding" );
300 }
301 0 ${$body_ref} = $output;
302 }
303 }
304  
305 #===================================
306 sub process_response {
307 #===================================
308 238 ▶ my ( $self, $params, $code, $msg, $body, $headers ) = @_;
Subroutine process_response: called CC=21 SCAR=32.8
Pod documented
309 238 $self->_decompress_body( \$body, $headers );
310  
311 238 ▶ my ($mime_type) = split /\s*;\s*/, ( $headers->{'content-type'} || '' );
Condition100%2/2
$headers->{'content-type'} || ''
l!l
2344
MC/DC100%1/1
$headers->{'content-type'} || ''
$headers->{'content-type'}
Truth table100%2/2
A
A= $headers->{'content-type'}
Aresult
11
00
312  
313 238 ▶ my $is_encoded = $mime_type && $mime_type ne 'text/plain';
Condition100%3/3
$mime_type && $mime_type ne 'text/plain'
!ll&&!rl&&r
41233
MC/DC100%2/2
$mime_type && $mime_type ne 'text/plain'
$mime_type $mime_type ne 'text/plain'
Truth table100%3/3
A && B
A= $mime_typeB= $mime_type ne 'text/plain'
ABresult
0X0
100
111
314  
315 # Deprecation warnings
316 238 ▶ if ( my $warnings = $headers->{warning} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $warnings = $headers->{"warning"}) 0 238 238
317 0 my $warning_string = _parse_warnings($warnings);
318 0 my %temp = (%$params);
319 0 delete $temp{data};
320 0 $self->logger->deprecation( $warning_string, \%temp );
321 }
322  
323 # Request is successful
324  
325 238 ▶ if ( $code >= 200 and $code <= 209 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 200 and $code <= 209) 176 62 238
Condition66%2/3
$code >= 200 and $code <= 209
!ll&&!rl&&r
062176
MC/DC50%1/2
$code >= 200 and $code <= 209
$code >= 200 $code <= 209
Truth table66%2/3
A and B
A= $code >= 200B= $code <= 209
ABresult
0X0
100
111
326 176 ▶ if ( defined $body and length $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body) 141 35 176
Condition100%3/3
defined $body and length $body
!ll&&!rl&&r
341141
MC/DC100%2/2
defined $body and length $body
defined $body length $body
Truth table100%3/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
327 141 ▶ $body = $self->serializer->decode($body)
Branch: 1 branch
BranchTrueFalseTotal
if $is_encoded 140 1 141
328 if $is_encoded;
329 141 return $code, $body;
330 }
331 35 ▶ return ( $code, 1 ) if $params->{method} eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"method"} eq "HEAD" 34 1 35
332 1 return ( $code, '' );
333 }
334  
335 # Check if the error should be ignored
336 62 my @ignore = to_list( $params->{ignore} );
337 62 ▶ push @ignore, 404 if $params->{method} eq 'HEAD';
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"method"} eq "HEAD" 11 51 62
338 62 ▶ return ($code) if grep { $_ eq $code } @ignore;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $code;} @ignore 2 60 62
339  
340 # Determine error type
341 60 my $error_type = $Code_To_Error{$code};
342 60 ▶ unless ($error_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($error_type) 49 11 60
343 49 ▶ if ( defined $body and length $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body and length $body) 0 49 49
Condition33%1/3
defined $body and length $body
!ll&&!rl&&r
4900
MC/DC0%0/2
defined $body and length $body
defined $body length $body
Truth table33%1/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
344 0 $msg = $body;
345 0 $body = undef;
346 }
347 49 $error_type = $self->error_from_text( $code, $msg );
348 }
349  
350 60 ▶ delete $params->{data} if $params->{body};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"body"} 0 60 60
351 60 my %error_args = ( status_code => $code, request => $params );
352  
353 # Extract error message from the body, if present
354  
355 60 ▶ if ( $body = $self->serializer->decode($body) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body = $self->serializer->decode($body)) 2 58 60
356 2 $error_args{body} = $body;
357 2 ▶ $msg = $self->_munge_elasticsearch_exception($body) || $msg;
Condition33%1/3
$self->_munge_elasticsearch_exception($body) || $msg
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->_munge_elasticsearch_exception($body) || $msg
$self->_munge_elasticsearch_exception($body) $msg
Truth table33%1/3
A || B
A= $self->_munge_elasticsearch_exception($body)B= $msg
ABresult
1X1
011
000
358  
359 2 ▶ $error_args{current_version} = $1
Branch: 1 branch
BranchTrueFalseTotal
if $error_type eq "Conflict" and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/ 0 2 2
Condition33%1/3
$error_type eq "Conflict" and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
!ll&&!rl&&r
200
MC/DC0%0/2
$error_type eq "Conflict" and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
$error_type eq "Conflict" $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
Truth table33%1/3
A and B
A= $error_type eq "Conflict"B= $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/
ABresult
0X0
100
111
360 if $error_type eq 'Conflict'
361 and $msg =~ /: version conflict, current (?:version )?\[(\d+)\]/;
362 }
363 60 ▶ $msg ||= $error_type;
Condition33%1/3
$msg ||= $error_type
l!l&&r!l&&!r
6000
MC/DC0%0/2
$msg ||= $error_type
$msg $error_type
Truth table33%1/3
A ||= B
A= $msgB= $error_type
ABresult
1X1
011
000
364  
365 60 chomp $msg;
366 60 throw( $error_type, "[" . $self->stringify . "]-[$code] $msg",
367 \%error_args );
368 }
369  
370 #===================================
371 sub _parse_warnings {
372 #===================================
373 0 ▶ my @warnings = ref $_[0] eq 'ARRAY' ? @{ shift() } : shift();
Subroutine _parse_warnings: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
374 0 my @str;
375 0 for (@warnings) {
376 0 ▶ if ( $_ =~ /^\d+\s+\S+\s+"((?:\\"|[^"])+)"/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /^\d+\s+\S+\s+"((?:\\"|[^"])+)"/) { } 0 0 0
377 0 my $msg = $1;
378 0 $msg =~ s/\\"/"/g, push @str, $msg;
379 }
380 else {
381 0 push @str, $_;
382 }
383 }
384 0 return join "; ", @str;
385 }
386  
387 #===================================
388 sub _munge_elasticsearch_exception {
389 #===================================
390 2 ▶ my ( $self, $body ) = @_;
Subroutine _munge_elasticsearch_exception: called CC=10 SCAR=41.0
391 2 ▶ return $body unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 2 2
392 2 ▶ my $error = $body->{error} || return;
Condition50%1/2
$body->{'error'} || (return)
l!l
20
MC/DC0%0/1
$body->{'error'} || (return)
$body->{'error'}
Truth table50%1/2
A
A= $body->{'error'}
Aresult
11
00
393 2 ▶ return $error unless ref $error eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $error eq "HASH" 2 0 2
394  
395 0 ▶ my $root_causes = $error->{root_cause} || [];
Condition0%0/2
$error->{'root_cause'} || []
l!l
00
MC/DC0%0/1
$error->{'root_cause'} || []
$error->{'root_cause'}
Truth table0%0/2
A
A= $error->{'root_cause'}
Aresult
11
00
396 0 ▶ unless (@$root_causes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$root_causes) 0 0 0
397 0 ▶ my $msg = "[" . $error->{type} . "] " if $error->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $error->{"type"} 0 0 0
398 0 ▶ $msg .= $error->{reason} if $error->{reason};
Branch: 1 branch
BranchTrueFalseTotal
if $error->{"reason"} 0 0 0
399 0 return $msg;
400 }
401  
402 0 my $json = $self->serializer;
403 0 my @msgs;
404 0 for (@$root_causes) {
405 0 my %cause = (%$_);
406 my $msg
407 0 = "[" . ( delete $cause{type} ) . "] " . ( delete $cause{reason} );
408 0 ▶ if ( keys %cause ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %cause) 0 0 0
409 0 $msg .= ", with: " . $json->encode( \%cause );
410 }
411 0 push @msgs, $msg;
412 }
413 0 return ( join ", ", @msgs );
414 }
415  
416 55 1;
417  
418 # ABSTRACT: Provides common functionality to HTTP Cxn implementations
419  
420 __END__
« Elasticsearch/Role/API.pm ↑ Summary Elasticsearch/Client/6_0/Bulk.pm »