| 1 | package Net::Blossom::Error; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use strictures 2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Net::Blossom::_ConstructorArgs (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Class::Tiny qw(method url status reason x_reason), { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||
| 9 | 0 | ▶ | headers => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 10 | 13 | body => '', | |||||||||||||||||||||||||||
| 11 | 13 | }; | |||||||||||||||||||||||||||
| 12 | 13 | ▶ | use overload '""' => 'as_string', fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 15 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 15 | my $class = shift; | |||||||||||||||||||||||||||
| 16 | 15 | my %args = Net::Blossom::_ConstructorArgs::normalize(@_); | |||||||||||||||||||||||||||
| 17 | 15 | my %known = map { $_ => 1 } qw(method url status reason x_reason headers body); | |||||||||||||||||||||||||||
| 18 | 15 | my @unknown = grep { !exists $known{$_} } keys %args; | |||||||||||||||||||||||||||
| 19 | 15 | ▶ | croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 15 | return \%args; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 15 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||
| 25 | 15 | my ($self) = @_; | |||||||||||||||||||||||||||
| 26 | 15 | ▶ | $self->headers({}) unless defined $self->headers; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 15 | ▶ | $self->body('') unless defined $self->body; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 15 | _validate_required_scalar($self, $_) for qw(method url status reason); | |||||||||||||||||||||||||||
| 29 | 7 | _validate_status($self->status); | |||||||||||||||||||||||||||
| 30 | 6 | ▶ | croak "headers must be a hash reference" unless ref($self->headers) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 5 | ▶ | croak "body must be a scalar" if ref($self->body); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 4 | ▶ | croak "x_reason must be a scalar" if defined $self->x_reason && ref($self->x_reason); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $self->x_reason and ref $self->x_reason
MC/DC50%1/2
defined $self->x_reason and ref $self->x_reason
defined $self->x_reason
ref $self->x_reason
Truth table66%2/3
A and B
A= defined $self->x_reasonB= ref $self->x_reason
| |||||||||||||||||||||||||||||
| 33 | 3 | return; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $message = $self->status . ' ' . ($self->reason || 'HTTP error'); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->reason || 'HTTP error'
MC/DC0%0/1
$self->reason || 'HTTP error'
$self->reason
Truth table0%0/2
A
A= $self->reason
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $message .= ': ' . $self->x_reason if defined $self->x_reason && length $self->x_reason; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->x_reason and length $self->x_reason
MC/DC0%0/2
defined $self->x_reason and length $self->x_reason
defined $self->x_reason
length $self->x_reason
Truth table0%0/3
A and B
A= defined $self->x_reasonB= length $self->x_reason
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $message .= ' at ' . $self->method . ' ' . $self->url | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->method and defined $self->url
MC/DC0%0/2
defined $self->method and defined $self->url
defined $self->method
defined $self->url
Truth table0%0/3
A and B
A= defined $self->methodB= defined $self->url
| |||||||||||||||||||||||||||||
| 41 | if defined $self->method && defined $self->url; | ||||||||||||||||||||||||||||
| 42 | 0 | return $message; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 48 | ▶ | sub _validate_required_scalar { | ||||||||||||||||||||||||||
|
Subroutine
_validate_required_scalar: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 46 | 48 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 47 | 48 | ▶ | croak "$field is required" unless defined $self->$field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 40 | ▶ | croak "$field must be a scalar" if ref($self->$field); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 40 | ▶ | croak "$field is required" if $field =~ /\A(?:method|url)\z/ && !length $self->$field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$field =~ /\A(?:method|url)\z/ and not length $self->$field
MC/DC0%0/2
$field =~ /\A(?:method|url)\z/ and not length $self->$field
$field =~ /\A(?:method|url)\z/
not length $self->$field
Truth table66%2/3
A and not(B)
A= $field =~ /\A(?:method|url)\z/B= not length $self->$field
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 7 | ▶ | sub _validate_status { | ||||||||||||||||||||||||||
|
Subroutine
_validate_status: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 53 | 7 | my ($status) = @_; | |||||||||||||||||||||||||||
| 54 | 7 | ▶ | croak "status must be an HTTP status code" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | unless $status =~ /\A[1-5][0-9][0-9]\z/; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 13 | 1; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =pod | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 NAME | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Net::Blossom::Error - Blossom HTTP error object | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | my $error = eval { $client->get_blob($sha256); 1 } ? undef : $@; | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | if (ref($error) && $error->isa('Net::Blossom::Error')) { | ||||||||||||||||||||||||||||
| 71 | warn $error->status; | ||||||||||||||||||||||||||||
| 72 | warn "$error"; | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | C<Net::Blossom::Error> represents a non-success HTTP response from a Blossom | ||||||||||||||||||||||||||||
| 78 | server. C<Net::Blossom::Client> dies with this object for non-402 HTTP failures. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | The object stringifies to a compact diagnostic containing status, reason, | ||||||||||||||||||||||||||||
| 81 | optional C<X-Reason>, method, and URL. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 new | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | my $error = Net::Blossom::Error->new(%args); | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | Required arguments are C<method>, C<url>, C<status>, and C<reason>. C<status> | ||||||||||||||||||||||||||||
| 90 | must be a three-digit HTTP status code from C<100> through C<599>. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Optional C<x_reason> is the server's C<X-Reason> diagnostic. Optional C<headers> | ||||||||||||||||||||||||||||
| 93 | defaults to an empty hash reference. Optional C<body> defaults to the empty | ||||||||||||||||||||||||||||
| 94 | string. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | Unknown arguments or invalid values croak. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 ACCESSORS | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head2 method | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Returns the HTTP method. | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head2 url | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | Returns the request URL. | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head2 status | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | Returns the HTTP status code. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 reason | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Returns the HTTP reason phrase. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head2 x_reason | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Returns the optional C<X-Reason> diagnostic. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 headers | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Returns the response headers hash reference. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 body | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Returns the response body. | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 METHODS | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =head2 as_string | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | my $message = $error->as_string; | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | Returns the same diagnostic used by stringification. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 INTERNAL METHODS | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head2 BUILDARGS | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Normalizes constructor arguments for Class::Tiny. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head2 BUILD | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Validates the constructed object for Class::Tiny. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =cut | ||||||||||||||||||||||||||||