| 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
Condition66%2/3
builtin::blessed($class) || $class
MC/DC0%0/2
builtin::blessed($class) || $class
builtin::blessed($class)
$class
Truth table66%2/3
A || B
A= builtin::blessed($class)B= $class
| |||||||||||||||||||||||||||||
| 48 | 12 | ▶ | my $self = blessed($class) ? $class : $class->new(@args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | push @substrings, "Message=$message" if $message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | push @substrings, "Details=" . encode_json_text($details) if @$details; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | $string .= "(" . join(", ", @substrings) . ")" if @substrings; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||