| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 17 | ▶ | $args{reason} = '' unless defined $args{reason}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 17 | ▶ | $args{headers} = {} unless defined $args{headers}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 15 | ▶ | croak "status must be an HTTP status code" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not ref $self->status and $self->status =~ /\A[1-5][0-9][0-9]\z/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | ▶ | croak "reason must be a scalar" if ref($self->reason); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 15 | ▶ | croak "reason must not contain CR or LF" if $self->reason =~ /[\r\n]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | my $header = $self->_headers->{lc $name}; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | return defined $header ? $header->{value} : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 17 | my %normalized; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 17 | for my $name (keys %$headers) { | |||||||||||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | croak "header names must be HTTP tokens" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $name and not ref $name and $name =~ /\A$TOKEN\z/
defined $name and not ref $name
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | ▶ | croak "header values must be scalars" if ref($headers->{$name}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | croak "header values must not contain CR or LF" | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if $headers->{$name} =~ /[\r\n]/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | my $lower = lc $name; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | croak "duplicate header: $name" if exists $normalized{$lower}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||