File Coverage

lib/PAGI/App/NotFound.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition 6 6 100.0
subroutine 7 7 100.0
pod 0 2 0.0
total 38 40 95.0


line stmt bran cond sub pod time code
1             package PAGI::App::NotFound;
2             $PAGI::App::NotFound::VERSION = '0.002000';
3 1     1   423 use strict;
  1         2  
  1         32  
4 1     1   5 use warnings;
  1         1  
  1         49  
5 1     1   7 use Future::AsyncAwait;
  1         2  
  1         8  
6 1     1   739 use PAGI::Response;
  1         5  
  1         401  
7              
8             =head1 NAME
9              
10             PAGI::App::NotFound - Customizable 404 response
11              
12             =head1 SYNOPSIS
13              
14             # A fixed 404 is just a response value (preferred):
15             use PAGI::Response;
16             $router->mount('/missing' => PAGI::Response->text('Not Found')->status(404));
17              
18             # PAGI::App::NotFound is for a computed body or custom defaults
19             # (e.g. a Cascade fallback):
20             use PAGI::App::NotFound;
21             my $app = PAGI::App::NotFound->new(
22             status => 404,
23             content_type => 'text/html',
24             body => sub { my ($scope) = @_; render_404($scope) },
25             )->to_app;
26              
27             =cut
28              
29             sub new {
30 5     5 0 9268 my ($class, %args) = @_;
31              
32             return bless {
33             body => $args{body} // 'Not Found',
34             content_type => $args{content_type} // 'text/plain',
35 5   100     63 status => $args{status} // 404,
      100        
      100        
36             }, $class;
37             }
38              
39             sub to_app {
40 5     5 0 8 my ($self) = @_;
41              
42 5         10 my $body = $self->{body};
43 5         8 my $content_type = $self->{content_type};
44 5         9 my $status = $self->{status};
45              
46 5     5   44 return async sub {
47 5         10 my ($scope, $receive, $send) = @_;
48 5 100       11 my $b = ref $body eq 'CODE' ? $body->($scope) : $body;
49              
50 5         26 await PAGI::Response->new($scope)
51             ->status($status)
52             ->content_type($content_type)
53             ->send_raw($b)
54             ->respond($send);
55 5         31 };
56             }
57              
58             1;
59              
60             __END__