File Coverage

blib/lib/Stancer/Exceptions/Http.pm
Criterion Covered Total %
statement 48 48 100.0
branch 18 18 100.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 76 76 100.0


line stmt bran cond sub pod time code
1             package Stancer::Exceptions::Http;
2              
3 48     48   179052 use 5.020;
  48         195  
4 48     48   286 use strict;
  48         99  
  48         1485  
5 48     48   290 use warnings;
  48         135  
  48         4213  
6              
7             # ABSTRACT: Abstract exception for every HTTP errors
8             our $VERSION = '1.0.3'; # VERSION
9              
10 48     48   4668 use Stancer::Core::Types qw(InstanceOf);
  48         191  
  48         4597  
11 48     48   5503 use HTTP::Status qw(status_message);
  48         53384  
  48         11169  
12              
13 48     48   432 use Moo;
  48         112  
  48         390  
14              
15             extends 'Stancer::Exceptions::Throwable';
16              
17 48     48   23529 use namespace::clean;
  48         172  
  48         458  
18              
19             has '+log_level' => (
20             default => 'warning',
21             );
22              
23             has '+message' => (
24             default => sub {
25             my $this = shift;
26             my $code = $this->status;
27              
28             return status_message($code) if defined $code;
29             return $this->_default_message;
30             },
31             lazy => 1,
32             );
33              
34             has '_default_message' => (
35             is => 'ro',
36             default => 'HTTP error',
37             );
38              
39              
40             has request => (
41             is => 'ro',
42             isa => InstanceOf['HTTP::Request'],
43             );
44              
45              
46             has response => (
47             is => 'ro',
48             isa => InstanceOf['HTTP::Response'],
49             );
50              
51              
52              
53             has status => (
54             is => 'ro',
55             builder => sub {
56 12     12   829626 my $this = shift;
57 12         62 my @parts = split m/::/sm, ref $this;
58 12         31 my $class = $parts[-1];
59              
60 12         114 $class =~ s/([[:upper:]])/_$1/xgsm;
61              
62 12         37 my $constant = 'HTTP' . uc $class;
63              
64 12 100       202 return HTTP::Status->$constant if HTTP::Status->can($constant);
65             },
66             );
67              
68              
69             sub factory {
70 88     88 1 122713 my ($class, $status, @args) = @_;
71 88         154 my $data;
72              
73 88 100       297 if (scalar @args == 1) {
74 23         34 $data = $args[0];
75             } else {
76 65         161 $data = {@args};
77             }
78              
79 88         233 $data->{status} = $status;
80              
81 88         2943 my $instance = Stancer::Exceptions::Http->new($data);
82              
83 88         8525 require Stancer::Exceptions::Http::BadRequest;
84 88         372 require Stancer::Exceptions::Http::ClientSide;
85 88         2776 require Stancer::Exceptions::Http::Conflict;
86 88         2827 require Stancer::Exceptions::Http::InternalServerError;
87 88         2808 require Stancer::Exceptions::Http::NotFound;
88 88         267 require Stancer::Exceptions::Http::ServerSide;
89 88         2604 require Stancer::Exceptions::Http::Unauthorized;
90              
91 88 100       2232 $instance = Stancer::Exceptions::Http::ClientSide->new($data) if $status >= 400;
92 88 100       3570 $instance = Stancer::Exceptions::Http::ServerSide->new($data) if $status >= 500;
93              
94 88 100       3210 $instance = Stancer::Exceptions::Http::BadRequest->new($data) if $status == 400;
95 88 100       3531 $instance = Stancer::Exceptions::Http::Unauthorized->new($data) if $status == 401;
96 88 100       3381 $instance = Stancer::Exceptions::Http::NotFound->new($data) if $status == 404;
97 88 100       521 $instance = Stancer::Exceptions::Http::Conflict->new($data) if $status == 409;
98 88 100       3621 $instance = Stancer::Exceptions::Http::InternalServerError->new($data) if $status == 500;
99              
100 88         2557 return $instance;
101             }
102              
103             1;
104              
105             __END__
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             Stancer::Exceptions::Http - Abstract exception for every HTTP errors
114              
115             =head1 VERSION
116              
117             version 1.0.3
118              
119             =head1 ATTRIBUTES
120              
121             =head2 C<request>
122              
123             Read-only HTTP::Request instance.
124              
125             The request that resulted that error.
126              
127             =head2 C<response>
128              
129             Read-only HTTP::Response instance.
130              
131             The response that resulted that error.
132              
133             =head2 C<status>
134              
135             Read-only integer.
136              
137             HTTP status code
138              
139             =head1 METHODS
140              
141             =head2 C<< Stancer::Exceptions::Http->factory( I<$status> ) >>
142              
143             =head2 C<< Stancer::Exceptions::Http->factory( I<$status>, I<%args> ) >>
144              
145             =head2 C<< Stancer::Exceptions::Http->factory( I<$status>, I<\%args> ) >>
146              
147             Return an instance of HTTP exception depending on C<$status>.
148              
149             =head1 USAGE
150              
151             =head2 Logging
152              
153              
154              
155             We use the L<Log::Any> framework for logging events.
156             You may tell where it should log using any available L<Log::Any::Adapter> module.
157              
158             For example, to log everything to a file you just have to add a line to your script, like this:
159             #! /usr/bin/env perl
160             use Log::Any::Adapter (File => '/var/log/payment.log');
161             use Stancer::Exceptions::Http;
162              
163             You must import C<Log::Any::Adapter> before our libraries, to initialize the logger instance before use.
164              
165             You can choose your log level on import directly:
166             use Log::Any::Adapter (File => '/var/log/payment.log', log_level => 'info');
167              
168             Read the L<Log::Any> documentation to know what other options you have.
169              
170             =cut
171              
172             =head1 SECURITY
173              
174             =over
175              
176             =item *
177              
178             Never, never, NEVER register a card or a bank account number in your database.
179              
180             =item *
181              
182             Always uses HTTPS in card/SEPA in communication.
183              
184             =item *
185              
186             Our API will never give you a complete card/SEPA number, only the last four digits.
187             If you need to keep track, use these last four digit.
188              
189             =back
190              
191             =cut
192              
193             =head1 BUGS
194              
195             Please report any bugs or feature requests on the bugtracker website
196             L<https://gitlab.com/wearestancer/library/lib-perl/-/issues> or by email to
197             L<bug-stancer@rt.cpan.org|mailto:bug-stancer@rt.cpan.org>.
198              
199             When submitting a bug or request, please include a test-file or a
200             patch to an existing test-file that illustrates the bug or desired
201             feature.
202              
203             =head1 AUTHOR
204              
205             Joel Da Silva <jdasilva@cpan.org>
206              
207             =head1 COPYRIGHT AND LICENSE
208              
209             This software is Copyright (c) 2018-2024 by Stancer / Iliad78.
210              
211             This is free software, licensed under:
212              
213             The Artistic License 2.0 (GPL Compatible)
214              
215             =cut