File Coverage

blib/lib/OpenSearch/Client/Error.pm
Criterion Covered Total %
statement 43 58 74.1
branch 17 26 65.3
condition 6 13 46.1
subroutine 7 9 77.7
pod 0 4 0.0
total 73 110 66.3


line stmt bran cond sub pod time code
1             # OpenSearch::Client is an unofficial client for OpenSearch.
2             # It is derived from Search::Elasticsearch version 7.714
3             # License details from that work are contained in the NOTICE
4             # file distributed with this work.
5             #-----------------------------------------------------------------------
6             # OpenSearch::Client
7             #-----------------------------------------------------------------------
8             # Copyright 2026 Mark Dootson
9             #
10             # Licensed under the Apache License, Version 2.0 (the "License");
11             # you may not use this file except in compliance with the License.
12             # You may obtain a copy of the License at
13             #
14             # http://www.apache.org/licenses/LICENSE-2.0
15             #
16             # Unless required by applicable law or agreed to in writing, software
17             # distributed under the License is distributed on an "AS IS" BASIS,
18             # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19             # See the License for the specific language governing permissions and
20             # limitations under the License.
21              
22             package OpenSearch::Client::Error;
23             $OpenSearch::Client::Error::VERSION = '3.007002';
24             our $DEBUG = 0;
25              
26             @OpenSearch::Client::Error::Internal::ISA = __PACKAGE__;
27             @OpenSearch::Client::Error::Param::ISA = __PACKAGE__;
28             @OpenSearch::Client::Error::NoNodes::ISA = __PACKAGE__;
29             @OpenSearch::Client::Error::Unauthorized::ISA = __PACKAGE__;
30             @OpenSearch::Client::Error::Forbidden::ISA = __PACKAGE__;
31             @OpenSearch::Client::Error::Illegal::ISA = __PACKAGE__;
32             @OpenSearch::Client::Error::Request::ISA = __PACKAGE__;
33             @OpenSearch::Client::Error::Timeout::ISA = __PACKAGE__;
34             @OpenSearch::Client::Error::Cxn::ISA = __PACKAGE__;
35             @OpenSearch::Client::Error::Serializer::ISA = __PACKAGE__;
36              
37             @OpenSearch::Client::Error::Conflict::ISA
38             = ( 'OpenSearch::Client::Error::Request', __PACKAGE__ );
39              
40             @OpenSearch::Client::Error::Missing::ISA
41             = ( 'OpenSearch::Client::Error::Request', __PACKAGE__ );
42              
43             @OpenSearch::Client::Error::RequestTimeout::ISA
44             = ( 'OpenSearch::Client::Error::Request', __PACKAGE__ );
45              
46             @OpenSearch::Client::Error::ContentLength::ISA
47             = ( __PACKAGE__, 'OpenSearch::Client::Error::Request' );
48              
49             @OpenSearch::Client::Error::SSL::ISA
50             = ( __PACKAGE__, 'OpenSearch::Client::Error::Cxn' );
51              
52             @OpenSearch::Client::Error::BadGateway::ISA
53             = ( 'OpenSearch::Client::Error::Cxn', __PACKAGE__ );
54              
55             @OpenSearch::Client::Error::Unavailable::ISA
56             = ( 'OpenSearch::Client::Error::Cxn', __PACKAGE__ );
57              
58             @OpenSearch::Client::Error::GatewayTimeout::ISA
59             = ( 'OpenSearch::Client::Error::Cxn', __PACKAGE__ );
60              
61             use overload (
62 60         364 '""' => '_stringify',
63             'cmp' => '_compare',
64 60     60   273905 );
  60         110386  
65              
66 60     60   32667 use Data::Dumper();
  60         404854  
  60         42403  
67              
68             #===================================
69             sub new {
70             #===================================
71 121     121 0 2036 my ( $class, $type, $msg, $vars, $caller ) = @_;
72 121 100       303 return $type if ref $type;
73 94   100     261 $caller ||= 0;
74              
75 94         342 my $error_class = 'OpenSearch::Client::Error::' . $type;
76 94 50       346 $msg = 'Unknown error' unless defined $msg;
77              
78 94 50       277 local $DEBUG = 2 if $type eq 'Internal';
79              
80 94         281 my $stack = $class->_stack;
81              
82 94         764 my $self = bless {
83             type => $type,
84             text => $msg,
85             vars => $vars,
86             stack => $stack,
87             }, $error_class;
88              
89 94         890 return $self;
90             }
91              
92             #===================================
93             sub is {
94             #===================================
95 139     139 0 171 my $self = shift;
96 139         272 for (@_) {
97 161 100       801 return 1 if $self->isa("OpenSearch::Client::Error::$_");
98             }
99 43         139 return 0;
100             }
101              
102             #===================================
103             sub _stringify {
104             #===================================
105 176     176   1575 my $self = shift;
106 176         272 local $Data::Dumper::Terse = 1;
107 176         290 local $Data::Dumper::Indent = !!$DEBUG;
108              
109 176 100       470 unless ( $self->{msg} ) {
110 79         121 my $stack = $self->{stack};
111 79         127 my $caller = $stack->[0];
112             $self->{msg} = sprintf( "[%s] ** %s, called from sub %s at %s line %d.",
113 79         190 $self->{type}, $self->{text}, @{$caller}[ 3, 1, 2 ] );
  79         503  
114              
115 79 100       209 if ( $self->{vars} ) {
116             $self->{msg} .= sprintf( " With vars: %s\n",
117 61         271 Data::Dumper::Dumper $self->{vars} );
118             }
119              
120 79 50       4352 if ( @$stack > 1 ) {
121             $self->{msg}
122 0         0 .= sprintf( "Stacktrace:\n%s\n", $self->stacktrace($stack) );
123             }
124             }
125 176         953 return $self->{msg};
126              
127             }
128              
129             #===================================
130             sub _compare {
131             #===================================
132 0     0   0 my ( $self, $other, $swap ) = @_;
133 0         0 $self .= '';
134 0 0       0 ( $self, $other ) = ( $other, $self ) if $swap;
135 0         0 return $self cmp $other;
136             }
137              
138             #===================================
139             sub _stack {
140             #===================================
141 94     94   329 my $self = shift;
142 94   50     356 my $caller = shift() || 2;
143              
144 94         207 my @stack;
145 94         268 while ( my @caller = caller( ++$caller ) ) {
146 180 100       3935 next if $caller[0] eq 'Try::Tiny';
147              
148 137 50       572 if ( $caller[3] =~ /^(.+)::__ANON__\[(.+):(\d+)\]$/ ) {
    50          
149 0         0 @caller = ( $1, $2, $3, '(ANON)' );
150             }
151             elsif ( $caller[1] =~ /^\(eval \d+\)/ ) {
152 0         0 $caller[3] = "modified(" . $caller[3] . ")";
153             }
154              
155             next
156 137 50 33     437 if $caller[0] =~ /^OpenSearch::Client/
      66        
157             and ( $DEBUG < 2 or $caller[3] eq 'Try::Tiny::try' );
158 91         267 push @stack, [ @caller[ 0, 1, 2, 3 ] ];
159 91 50       461 last unless $DEBUG > 1;
160             }
161 94         326 return \@stack;
162             }
163              
164             #===================================
165             sub stacktrace {
166             #===================================
167 0     0 0 0 my $self = shift;
168 0   0     0 my $stack = shift || $self->_stack();
169              
170 0         0 my $o = sprintf "%s\n%-4s %-50s %-5s %s\n%s\n",
171             '-' x 80, '#', 'Package', 'Line', 'Sub-routine', '-' x 80;
172              
173 0         0 my $i = 1;
174 0         0 for (@$stack) {
175 0         0 $o .= sprintf "%-4d %-50s %4d %s\n", $i++, @{$_}[ 0, 2, 3 ];
  0         0  
176             }
177              
178 0         0 return $o .= ( '-' x 80 ) . "\n";
179             }
180              
181             #===================================
182             sub TO_JSON {
183             #===================================
184 1     1 0 103 my $self = shift;
185 1         3 return $self->_stringify;
186             }
187             1;
188              
189             __END__