Hydra/Exception.pm

filter: Statementstmt 100% 61 / 61 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 57.1% 4 / 7 totaltotal 90.3% 94 / 104 CC 11 SCAR 10.3
CC
11
Cov
94%
as_string
5.1
import
4.1
throw
4.0
CRAP 2.8
« Hydra/Client.pm ↑ Summary Hydra/Exception/HydraRequestError.pm »
1 package WebService::Hydra::Exception;
2  
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class WebService::Hydra::Exception;
9  
10 18 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use JSON::MaybeUTF8 qw(encode_json_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 18 our $VERSION = '0.005'; ## VERSION
15  
16 =head1 NAME
17  
18 WebService::Hydra::Exception - Base class for all Hydra Exceptions, loading all possible exception types.
19  
20 =head1 DESCRIPTION
21  
22 The base class cannot be instantiated directly, and it dynamically loads all exception types within WebService::Hydra::Exception::* namespace.
23  
24 =cut
25  
26 18 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Field definitions commonly inherited by all subclasses
29 13 field $message :param :reader = '';
30 13 ▶field $category :param :reader = '';
Subroutine message: called CC=1 SCAR=0.0
Pod undocumented
31 13 ▶field $details :param :reader = [];
Subroutine category: called CC=1 SCAR=0.0
Pod undocumented
32  
33 2 ▶BUILD {
Subroutine details: called CC=1 SCAR=0.0
Pod undocumented
34 die ref($self) . " is a base class and cannot be instantiated directly." if ref($self) eq __PACKAGE__;
35 }
36  
37 18 =head1 Methods
38  
39 =head2 throw
40  
41 Instantiates a new exception and throws it (using L<perlfunc/die>).
42  
43 =cut
44  
45 13 ▶sub throw {
Subroutine throw: called CC=4 SCAR=13.9
Pod documented
46 13 my ($class, @args) = @_;
47 13 ▶ die "$class is a base class and cannot be thrown directly." if (blessed($class) || $class) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($class) || $class) eq __PACKAGE__ 1 12 13
Condition66%2/3
builtin::blessed($class) || $class
l!l&&r!l&&!r
1120
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class) $class
Truth table66%2/3
A || B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
48 12 ▶ my $self = blessed($class) ? $class : $class->new(@args);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($class) ? : 11 1 12
49 12 die $self;
50 }
51  
52 =head2 as_string
53  
54 Returns a string representation of the exception.
55  
56 =cut
57  
58 3 ▶method as_string {
Subroutine as_string: called CC=5 SCAR=16.2
Pod documented
59 3 my $string = blessed($self);
60 3 my @substrings = ();
61 3 ▶ push @substrings, "Category=$category" if $category;
Branch: 1 branch
BranchTrueFalseTotal
if $category 2 1 3
62 3 ▶ push @substrings, "Message=$message" if $message;
Branch: 1 branch
BranchTrueFalseTotal
if $message 3 0 3
63 3 ▶ push @substrings, "Details=" . encode_json_text($details) if @$details;
Branch: 1 branch
BranchTrueFalseTotal
if @$details 1 2 3
64 3 ▶ $string .= "(" . join(", ", @substrings) . ")" if @substrings;
Branch: 1 branch
BranchTrueFalseTotal
if @substrings 3 0 3
65 3 return $string;
66 18 }
67  
68 =head2 as_json
69  
70 Returns a JSON string representation of the exception.
71  
72 =cut
73  
74 2 ▶method as_json {
Subroutine as_json: called CC=1 SCAR=0.0
Pod documented
75 2 my $data = {
76 Exception => blessed($self),
77 Category => $self->category,
78 Message => $self->message,
79 Details => $self->details,
80 };
81 2 return encode_json_text($data);
82 18 }
83  
84 =head2 log
85  
86 Logs the exception using Log::Any and increments a stats counter.
87  
88 =cut
89  
90 1 ▶method log {
Subroutine log: called CC=1 SCAR=0.0
Pod documented
91 1 $log->errorf("Exception: %s", $self->as_string);
92 1 my $stats_name = blessed($self);
93 1 $stats_name =~ s/::/./g;
94 18 }
95  
96 # Exception class names explicitly listed
97 18 my @all_exceptions = qw(
98 HydraServiceUnreachable
99 FeatureUnavailable
100 HydraRequestError
101 InvalidLoginChallenge
102 InvalidLogoutChallenge
103 InvalidLoginRequest
104 TokenExchangeFailed
105 InvalidIdToken
106 InvalidConsentChallenge
107 InternalServerError
108 RevokeLoginSessionsFailed
109 InvalidToken
110 InvalidClaims
111 );
112  
113 =head2 import
114  
115 The import method dynamically loads specific exceptions, or all by default.
116  
117 =cut
118  
119 14 ▶sub import {
Subroutine import: called CC=4 SCAR=14.1
120 14 my ($class, @exceptions) = @_;
121  
122 # If no specific exceptions are given, load all exceptions
123 14 ▶ @exceptions = @exceptions ? @exceptions : @all_exceptions;
Branch: 1 branch
BranchTrueFalseTotal
@exceptions ? : 0 14 14
124  
125 14 for my $exception (@exceptions) {
126 # Construct the module name: WebService::Hydra::Exception::ExceptionName
127 182 my $module_name = "WebService::Hydra::Exception::$exception";
128  
129 182 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &load($module_name); 1 } } or warn "Failed to load module ${module_name}: $@" 182 0 182
130 182 load $module_name; # Load the exception module
131 182 1;
132 } or warn "Failed to load module $module_name: $@";
133 }
134 }
135  
136 18 1;
« Hydra/Client.pm ↑ Summary Hydra/Exception/HydraRequestError.pm »