Server/Error.pm

filter: Statementstmt 90.0% 54 / 60 Branchbran 65.6% 21 / 32 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 83.3% 10 / 12 Podpod 100% 6 / 6 totaltotal 75.8% 94 / 124 CC 21 SCAR 19.3
CC
21
Cov
77%
_normalize_headers
12.7
BUILD
8.3
BUILDARGS
4.0
CRAP 6.9
« Server/AuthorizationResult.pm ↑ Summary Server/BlobResult.pm »
1 package Net::Blossom::Server::Error;
2  
3 4 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Net::Blossom::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Net::Blossom::Server::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Class::Tiny qw(status _headers), {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 reason => '',
11 4 };
12  
13 4 my $TOKEN = qr/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/;
14  
15 17 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=4 SCAR=13.9
Pod documented
16 17 my $class = shift;
17 17 my %args = Net::Blossom::_ConstructorArgs::normalize(@_);
18 17 my %known = map { $_ => 1 } qw(status reason headers);
19 17 my @unknown = grep { !exists $known{$_} } keys %args;
20 17 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 17 17
21  
22 17 ▶ $args{reason} = '' unless defined $args{reason};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"reason"} 2 15 17
23 17 ▶ $args{headers} = {} unless defined $args{headers};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"headers"} 13 4 17
24 17 $args{_headers} = _normalize_headers($args{headers});
25 15 delete $args{headers};
26  
27 15 return \%args;
28 }
29  
30 15 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=21.2
Pod documented
31 15 my ($self) = @_;
32 15 ▶ croak "status is required" unless defined $self->status;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->status 0 15 15
33 15 ▶ croak "status must be an HTTP status code"
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $self->status and $self->status =~ /\A[1-5][0-9][0-9]\z/ 0 15 15
Condition33%1/3
not ref $self->status and $self->status =~ /\A[1-5][0-9][0-9]\z/
!ll&&!rl&&r
0015
MC/DC0%0/2
not ref $self->status and $self->status =~ /\A[1-5][0-9][0-9]\z/
not ref $self->status $self->status =~ /\A[1-5][0-9][0-9]\z/
Truth table33%1/3
not(A) and B
A= not ref $self->statusB= $self->status =~ /\A[1-5][0-9][0-9]\z/
ABresult
0X0
100
111
34 unless !ref($self->status) && $self->status =~ /\A[1-5][0-9][0-9]\z/;
35 15 ▶ $self->reason('') unless defined $self->reason;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->reason 0 15 15
36 15 ▶ croak "reason must be a scalar" if ref($self->reason);
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->reason 0 15 15
37 15 ▶ croak "reason must not contain CR or LF" if $self->reason =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->reason =~ /[\r\n]/ 2 13 15
38 13 return;
39 }
40  
41 13 ▶sub throw {
Subroutine throw: called CC=1 SCAR=0.0
Pod documented
42 13 my $class = shift;
43 13 die $class->new(@_);
44 }
45  
46 0 ▶sub headers {
Subroutine headers: not called CC=1 SCAR=6.9
Pod documented
47 0 my ($self) = @_;
48 0 return { map { $self->_headers->{$_}{name} => $self->_headers->{$_}{value} } keys %{$self->_headers} };
49 }
50  
51 2 ▶sub header {
Subroutine header: called CC=3 SCAR=11.0
Pod documented
52 2 my ($self, $name) = @_;
53 2 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 2 2
54 2 my $header = $self->_headers->{lc $name};
55 2 ▶ return defined $header ? $header->{value} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $header ? : 1 1 2
56 }
57  
58 0 ▶sub as_response {
Subroutine as_response: not called CC=1 SCAR=6.9
Pod documented
59 0 my ($self) = @_;
60 0 return Net::Blossom::Server::Response->error(
61 $self->status,
62 $self->reason,
63 headers => $self->headers,
64 );
65 }
66  
67 17 ▶sub _normalize_headers {
Subroutine _normalize_headers: called CC=10 SCAR=25.4
68 17 my ($headers) = @_;
69 17 ▶ croak "headers must be a hash reference" unless ref($headers) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $headers eq "HASH" 0 17 17
70  
71 17 my %normalized;
72 17 for my $name (keys %$headers) {
73 3 ▶ croak "header names must be HTTP tokens"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and $name =~ /\A$TOKEN\z/ 0 3 3
Condition33%2/6
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
!ll&&!rl&&r
003
defined $name and not ref $name
!ll&&!rl&&r
003
MC/DC0%0/3
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
defined $name not ref $name $name =~ /\A$TOKEN\z/
Truth table25%1/4
A and not(B) and C
A= defined $nameB= not ref $nameC= $name =~ /\A$TOKEN\z/
ABCresult
0XX0
10X0
1100
1111
74 unless defined $name && !ref($name) && $name =~ /\A$TOKEN\z/;
75 3 ▶ croak "header values must be defined" unless defined $headers->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers->{$name} 0 3 3
76 3 ▶ croak "header values must be scalars" if ref($headers->{$name});
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers->{$name} 0 3 3
77 croak "header values must not contain CR or LF"
78 3 ▶ if $headers->{$name} =~ /[\r\n]/;
Branch: 1 branch
BranchTrueFalseTotal
if $headers->{$name} =~ /[\r\n]/ 2 1 3
79  
80 1 my $lower = lc $name;
81 1 ▶ croak "duplicate header: $name" if exists $normalized{$lower};
Branch: 1 branch
BranchTrueFalseTotal
if exists $normalized{$lower} 0 1 1
82 $normalized{$lower} = {
83 name => $name,
84 1 value => $headers->{$name},
85 };
86 }
87  
88 15 return \%normalized;
89 }
90  
91 4 1;
92  
93 =pod
94  
95 =head1 NAME
96  
97 Net::Blossom::Server::Error - Typed Blossom server error
98  
99 =head1 SYNOPSIS
100  
101 use Net::Blossom::Server::Error;
102  
103 Net::Blossom::Server::Error->throw(
104 status => 401,
105 reason => 'Unauthorized',
106 headers => { 'WWW-Authenticate' => 'Nostr' },
107 );
108  
109 =head1 DESCRIPTION
110  
111 C<Net::Blossom::Server::Error> represents an expected server-side failure that
112 gateway adapters can turn into an HTTP response. It is used for authorization
113 failures and other controlled errors where the status code is part of the API.
114  
115 =head1 CONSTRUCTOR
116  
117 =head2 new
118  
119 my $error = Net::Blossom::Server::Error->new(%args);
120  
121 Required C<status> must be an HTTP status code from C<100> through C<599>.
122  
123 Optional C<reason> defaults to the empty string and must not contain CR or LF.
124 Optional C<headers> defaults to an empty hash reference. Header values must be
125 defined scalars and must not contain CR or LF.
126  
127 Unknown arguments or invalid values croak.
128  
129 =head1 ACCESSORS
130  
131 =head2 status
132  
133 Returns the HTTP status code.
134  
135 =head2 reason
136  
137 Returns the reason string.
138  
139 =head1 METHODS
140  
141 =head2 throw
142  
143 Net::Blossom::Server::Error->throw(%args);
144  
145 Constructs and dies with a typed error object.
146  
147 =head2 headers
148  
149 my $headers = $error->headers;
150  
151 Returns a copy hash reference of response headers.
152  
153 =head2 header
154  
155 my $value = $error->header($name);
156  
157 Returns a header value using case-insensitive lookup.
158  
159 =head2 as_response
160  
161 my $response = $error->as_response;
162  
163 Returns a C<Net::Blossom::Server::Response> for the error.
164  
165 =head1 INTERNAL METHODS
166  
167 =head2 BUILDARGS
168  
169 Normalizes constructor arguments for Class::Tiny.
170  
171 =head2 BUILD
172  
173 Validates the constructed object for Class::Tiny.
174  
175 =cut
« Server/AuthorizationResult.pm ↑ Summary Server/BlobResult.pm »