File Coverage

blib/lib/Plack/Util/PeriAHS.pm
Criterion Covered Total %
statement 22 23 95.6
branch 4 6 66.6
condition 2 5 40.0
subroutine 6 6 100.0
pod 1 1 100.0
total 35 41 85.3


line stmt bran cond sub pod time code
1             package Plack::Util::PeriAHS;
2              
3             our $DATE = '2017-07-10'; # DATE
4             our $VERSION = '0.61'; # VERSION
5              
6 2     2   41 use 5.010;
  2         8  
7 2     2   13 use strict;
  2         5  
  2         48  
8 2     2   43 use warnings;
  2         27  
  2         67  
9 2     2   11 use Log::ger;
  2         5  
  2         20  
10              
11             our @ISA = qw(Exporter);
12             our @EXPORT_OK = qw(errpage);
13              
14 2     2   2469 use JSON::MaybeXS;
  2         5  
  2         519  
15              
16             my $json = JSON::MaybeXS->new->allow_nonref;
17              
18             sub errpage {
19 11     11 1 37 my ($env, $rres) = @_;
20              
21             my $fmt = $env->{'riap.request'}{fmt} //
22 11   33     80 $env->{"periahs.default_fmt"} // 'json';
      50        
23 11         23 my $pres;
24              
25 11 50       69 if ($fmt =~ /^html$/i) {
    100          
26 0         0 $pres = [
27             200,
28             ["Content-Type" => "text/html"],
29             ["<h1>Error $rres->[0]</h1>\n\n$rres->[1]\n"],
30             ];
31             } elsif ($fmt =~ /text$/i) {
32 1 50       11 $pres = [
33             200,
34             ["Content-Type" => "text/plain"],
35             ["Error $rres->[0]: ".$rres->[1].($rres->[1] =~ /\n$/ ? "":"\n")],
36             ];
37             } else {
38 10         122 $pres = [
39             200,
40             ["Content-Type" => "application/json"],
41             [$json->encode($rres)]
42             ];
43             }
44              
45 11         56 log_trace("Returning error page: %s", $pres);
46 11         121 $pres;
47             }
48              
49             1;
50             # ABSTRACT: Utility routines
51              
52             __END__
53              
54             =pod
55              
56             =encoding UTF-8
57              
58             =head1 NAME
59              
60             Plack::Util::PeriAHS - Utility routines
61              
62             =head1 VERSION
63              
64             This document describes version 0.61 of Plack::Util::PeriAHS (from Perl distribution Perinci-Access-HTTP-Server), released on 2017-07-10.
65              
66             =head1 FUNCTIONS
67              
68             =head2 errpage($env, $resp)
69              
70             Render enveloped response $resp (as specified in L<Rinci::function>) as an error
71             page PSGI response, either in HTML/JSON/plaintext (according to C<<
72             $env->{"riap.request"}{fmt} >>). Will default to JSON if C<fmt> is unsupported.
73              
74             $env is PSGI environment.
75              
76             =head1 HOMEPAGE
77              
78             Please visit the project's homepage at L<https://metacpan.org/release/Perinci-Access-HTTP-Server>.
79              
80             =head1 SOURCE
81              
82             Source repository is at L<https://github.com/perlancar/perl-Perinci-Access-HTTP-Server>.
83              
84             =head1 BUGS
85              
86             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Access-HTTP-Server>
87              
88             When submitting a bug or request, please include a test-file or a
89             patch to an existing test-file that illustrates the bug or desired
90             feature.
91              
92             =head1 AUTHOR
93              
94             perlancar <perlancar@cpan.org>
95              
96             =head1 COPYRIGHT AND LICENSE
97              
98             This software is copyright (c) 2017, 2015, 2014, 2013, 2012, 2011 by perlancar@cpan.org.
99              
100             This is free software; you can redistribute it and/or modify it under
101             the same terms as the Perl 5 programming language system itself.
102              
103             =cut