blib/lib/Web/Authn/Exception.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 272 CC 16 SCAR 22.3
CC
16
Cov
0%
new
30.0
THAW
30.0
as_string
12.0
CRAP 9.3
1 ##----------------------------------------------------------------------------
2 ## WebAuthn - ~/lib/Web/Authn/Exception.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/09/09
7 ## Modified 2026/09/11
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Web::Authn::Exception;
14 BEGIN
15 {
16 use v5.16.0;
17 use strict;
18 use warnings;
19 warnings::register_categories( 'Web::Authn' );
20 use vars qw( $VERSION );
21 use overload (
22 '""' => \&as_string,
23 bool => sub{1},
24 fallback => 1
25 );
26 our $VERSION = 'v0.2.0';
27 };
28  
29 use strict;
30 use warnings;
31  
32 sub new
33 {
34 my $this = shift( @_ );
35 my $class = ref( $this ) || $this;
36 my %args;
37 if( @_ == 1 && ref( $_[0] ) eq 'HASH' )
38 {
39 %args = %{$_[0]};
40 }
41 elsif( @_ % 2 == 0 )
42 {
43 %args = @_;
44 }
45 else
46 {
47 $args{message} = shift( @_ );
48 }
49  
50 my $self = bless(
51 {
52 code => $args{code},
53 message => $args{message} // '',
54 package => $args{package} // '',
55 file => $args{file} // '',
56 line => $args{line} // '',
57 skip_frames => $args{skip_frames} // 0,
58 }, $class );
59  
60 # Auto-populate call location unless provided
61 unless( $self->{file} && $self->{line} )
62 {
63 my $skip = $self->{skip_frames} + 1;
64 my @info = caller( $skip );
65 if( @info )
66 {
67 $self->{package} ||= $info[0];
68 $self->{file} ||= $info[1];
69 $self->{line} ||= $info[2];
70 }
71 }
72 return( $self );
73 }
74  
75 sub as_string
76 {
77 my $self = shift( @_ );
78 my $msg = $self->{message} // '';
79 if( $self->{file} && $self->{line} )
80 {
81 $msg .= sprintf( " at %s line %d.\n", $self->{file}, $self->{line} )
82 unless( $msg =~ /\n\z/ );
83 }
84 return( $msg );
85 }
86  
87 sub code
88 {
89 my $self = shift( @_ );
90 $self->{code} = shift( @_ ) if( @_ );
91 return( $self->{code} );
92 }
93  
94 sub file { return( $_[0]->{file} ); }
95  
96 sub line { return( $_[0]->{line} ); }
97  
98 sub message { return( $_[0]->{message} ); }
99  
100 sub package { return( $_[0]->{package} ); }
101  
102 sub rethrow
103 {
104 my $self = shift( @_ );
105 return if( !ref( $self ) );
106 die( $self );
107 }
108  
109 sub throw
110 {
111 my $self = shift( @_ );
112 my $e;
113 if( @_ )
114 {
115 $e = $self->new( @_ );
116 }
117 else
118 {
119 $e = $self;
120 }
121 die( $e );
122 }
123  
124 sub FREEZE
125 {
126 my $self = CORE::shift( @_ );
127 my $serialiser = CORE::shift( @_ ) // '';
128 my $class = CORE::ref( $self );
129 my %hash = %$self;
130 # Return an array reference rather than a list so this works with Sereal and CBOR
131 # On or before Sereal version 4.023, Sereal did not support multiple values returned
132 CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
133 # But Storable want a list with the first element being the serialised element
134 CORE::return( $class, \%hash );
135 }
136  
137 # From perlfunc docmentation on "die":
138 # "If LIST was empty or made an empty string, and $@ contains an
139 # object reference that has a "PROPAGATE" method, that method will
140 # be called with additional file and line number parameters. The
141 # return value replaces the value in $@; i.e., as if "$@ = eval {
142 # $@->PROPAGATE(__FILE__, __LINE__) };" were called."
143 sub PROPAGATE
144 {
145 my( $self, $file, $line ) = @_;
146 if( defined( $file ) && defined( $line ) )
147 {
148 my $clone = $self->clone;
149 $clone->file( $file );
150 $clone->line( $line );
151 return( $clone );
152 }
153 return( $self );
154 }
155  
156 sub STORABLE_freeze { return( shift->FREEZE( @_ ) ); }
157  
158 sub STORABLE_thaw { return( shift->THAW( @_ ) ); }
159  
160 # NOTE: CBOR will call the THAW method with the stored classname as first argument, the constant string CBOR as second argument, and all values returned by FREEZE as remaining arguments.
161 # NOTE: Storable calls it with a blessed object it created followed with $cloning and any other arguments initially provided by STORABLE_freeze
162 sub THAW
163 {
164 my( $self, undef, @args ) = @_;
165 my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
166 my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
167 my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
168 my $new;
169 # Storable pattern requires to modify the object it created rather than returning a new one
170 if( CORE::ref( $self ) )
171 {
172 foreach( CORE::keys( %$hash ) )
173 {
174 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
175 }
176 $new = $self;
177 }
178 else
179 {
180 $new = CORE::bless( $hash => $class );
181 }
182 CORE::return( $new );
183 }
184  
185 sub TO_JSON { return( shift->as_string ); }
186  
187 # NOTE: Web::Authn::Exception::InvalidAuthentication class
188 package Web::Authn::Exception::InvalidAuthentication;
189 BEGIN { use parent -norequire, qw( Web::Authn::Exception ); };
190  
191 # NOTE: Web::Authn::Exception::InvalidCertificateChain class
192 package Web::Authn::Exception::InvalidCertificateChain;
193 BEGIN { use parent -norequire, qw( Web::Authn::Exception ); };
194  
195 # NOTE: Web::Authn::Exception::InvalidRegistration class
196 package Web::Authn::Exception::InvalidRegistration;
197 BEGIN { use parent -norequire, qw( Web::Authn::Exception ); };
198  
199 # NOTE: Web::Authn::Exception::InvalidStructure class
200 package Web::Authn::Exception::InvalidStructure;
201 BEGIN { use parent -norequire, qw( Web::Authn::Exception ); };
202  
203 # NOTE: Web::Authn::Exception::UnsupportedAlgorithm class
204 package Web::Authn::Exception::UnsupportedAlgorithm;
205 BEGIN { use parent -norequire, qw( Web::Authn::Exception ); };
206  
207 1;
208 # NOTE: POD
209 __END__